(hexColor: string)
| 1 | export const applyThemeColor = (hexColor: string) => { |
| 2 | const root = document.documentElement; |
| 3 | |
| 4 | // Convert hex to RGB |
| 5 | const r = parseInt(hexColor.slice(1, 3), 16) / 255; |
| 6 | const g = parseInt(hexColor.slice(3, 5), 16) / 255; |
| 7 | const b = parseInt(hexColor.slice(5, 7), 16) / 255; |
| 8 | |
| 9 | // Find the min and max values to determine hue |
| 10 | const max = Math.max(r, g, b); |
| 11 | const min = Math.min(r, g, b); |
| 12 | |
| 13 | // Calculate lightness |
| 14 | const l = (max + min) / 2; |
| 15 | |
| 16 | let h = 0; |
| 17 | let s = 0; |
| 18 | |
| 19 | if (max !== min) { |
| 20 | // Calculate saturation |
| 21 | s = l > 0.5 ? (max - min) / (2 - max - min) : (max - min) / (max + min); |
| 22 | |
| 23 | // Calculate hue |
| 24 | if (max === r) { |
| 25 | h = (g - b) / (max - min) + (g < b ? 6 : 0); |
| 26 | } else if (max === g) { |
| 27 | h = (b - r) / (max - min) + 2; |
| 28 | } else { |
| 29 | h = (r - g) / (max - min) + 4; |
| 30 | } |
| 31 | h = Math.round(h * 60); |
| 32 | } |
| 33 | |
| 34 | // Calculate saturation percentage |
| 35 | s = Math.round(s * 100); |
| 36 | |
| 37 | // Calculate lightness percentage |
| 38 | const lPercentage = Math.round(l * 100); |
| 39 | |
| 40 | // Update CSS Variables |
| 41 | root.style.setProperty("--primary", `hsl(${h} ${s}% ${lPercentage}%)`); |
| 42 | root.style.setProperty("--ring", `hsl(${h} ${s}% ${lPercentage}%)`); |
| 43 | }; |
no outgoing calls
no test coverage detected