fix menu color

This commit is contained in:
hamid zarghami
2026-01-04 17:08:50 +03:30
parent 86aebd7a9f
commit 82fddde315
4 changed files with 103 additions and 101 deletions
+9 -26
View File
@@ -1,39 +1,22 @@
'use client'
import { useEffect } from 'react'
import { hexToOklch, calculateBrightness } from '@/lib/helpers/colorUtils'
export function ThemeColorSetter() {
useEffect(() => {
try {
const savedColor = localStorage.getItem('theme-primary-color')
if (savedColor) {
const hex = savedColor.replace('#', '')
const r = parseInt(hex.substring(0, 2), 16) / 255
const g = parseInt(hex.substring(2, 4), 16) / 255
const b = parseInt(hex.substring(4, 6), 16) / 255
const lightness = 0.2126 * r + 0.7152 * g + 0.0722 * b
const max = Math.max(r, g, b)
const min = Math.min(r, g, b)
const chroma = (max - min) * 0.15
let hue = 0
if (chroma !== 0) {
if (max === r) {
hue = ((g - b) / (max - min) + (g < b ? 6 : 0)) * 60
} else if (max === g) {
hue = ((b - r) / (max - min) + 2) * 60
} else {
hue = ((r - g) / (max - min) + 4) * 60
}
const oklchColor = hexToOklch(savedColor)
if (oklchColor) {
document.documentElement.style.setProperty('--primary', oklchColor)
const brightness = calculateBrightness(savedColor)
const foregroundOklch = brightness > 0.5 ? 'oklch(0 0 0)' : 'oklch(1 0 0)'
document.documentElement.style.setProperty('--primary-foreground', foregroundOklch)
}
const oklchColor = `oklch(${lightness.toFixed(3)} ${chroma.toFixed(3)} ${hue.toFixed(1)})`
document.documentElement.style.setProperty('--primary', oklchColor)
const brightness = (r * 299 + g * 587 + b * 114) / 1000
const foregroundOklch = brightness > 0.5 ? 'oklch(0 0 0)' : 'oklch(1 0 0)'
document.documentElement.style.setProperty('--primary-foreground', foregroundOklch)
}
} catch (e) {
// Silent fail