🎨 theme-aware accent color with pinned lightness variants
Browse files at this commit67af0d436cb5abf3168ef30d9e31aa6bdaf48174Parent: 5be4692- feat
- 🎨 theme-aware accent color with pinned lightness variants
3 changed files +83 -25
- src/index.css +12 -6
- src/stores/preferences-context.test.tsx +9 -4
- src/stores/preferences-context.tsx +62 -15
src/index.cssmodified+12 -6
| @@ -328,7 +328,10 @@ | ||
| 328 | 328 | @layer base { |
| 329 | 329 | /* Hue-neutral gray scale + Carbon-style semantic colors. */ |
| 330 | 330 | :root { |
| 331 | - --ui-accent: 0 0% 13%; | |
| 331 | + /* --accent-light / --accent-light-fg are set inline by preferences-context | |
| 332 | + when the user picks an accent color; the fallback here is the neutral | |
| 333 | + default for everyone else. */ | |
| 334 | + --ui-accent: var(--accent-light, 0 0% 13%); | |
| 332 | 335 | --background: 0 0% 98%; |
| 333 | 336 | --foreground: 0 0% 8%; |
| 334 | 337 | --card: 0 0% 100%; |
| @@ -336,7 +339,7 @@ | ||
| 336 | 339 | --popover: 0 0% 100%; |
| 337 | 340 | --popover-foreground: 0 0% 8%; |
| 338 | 341 | --primary: var(--ui-accent); |
| 339 | - --primary-foreground: 0 0% 98%; | |
| 342 | + --primary-foreground: var(--accent-light-fg, 0 0% 98%); | |
| 340 | 343 | --secondary: 0 0% 94%; |
| 341 | 344 | --secondary-foreground: 0 0% 16%; |
| 342 | 345 | --muted: 0 0% 96%; |
| @@ -347,7 +350,7 @@ | ||
| 347 | 350 | --destructive-foreground: 0 0% 100%; |
| 348 | 351 | --border: 0 0% 89%; |
| 349 | 352 | --input: 0 0% 82%; |
| 350 | - --ring: 0 0% 45%; | |
| 353 | + --ring: var(--accent-light, 0 0% 45%); | |
| 351 | 354 | --radius: 0.5rem; |
| 352 | 355 | --success: 137 63% 39%; |
| 353 | 356 | --warning: 38 92% 50%; |
| @@ -358,7 +361,10 @@ | ||
| 358 | 361 | } |
| 359 | 362 | |
| 360 | 363 | [data-kb-theme="dark"] { |
| 361 | - --ui-accent: 0 0% 92%; | |
| 364 | + /* --accent-dark / --accent-dark-fg are set inline by preferences-context — | |
| 365 | + a separate, lighter-lightness variant of the same picked hue so it stays | |
| 366 | + legible against this theme's near-black background. */ | |
| 367 | + --ui-accent: var(--accent-dark, 0 0% 92%); | |
| 362 | 368 | --background: 0 0% 4%; |
| 363 | 369 | --foreground: 0 0% 98%; |
| 364 | 370 | --card: 0 0% 8%; |
| @@ -366,7 +372,7 @@ | ||
| 366 | 372 | --popover: 0 0% 8%; |
| 367 | 373 | --popover-foreground: 0 0% 98%; |
| 368 | 374 | --primary: var(--ui-accent); |
| 369 | - --primary-foreground: 0 0% 6%; | |
| 375 | + --primary-foreground: var(--accent-dark-fg, 0 0% 6%); | |
| 370 | 376 | --secondary: 0 0% 16%; |
| 371 | 377 | --secondary-foreground: 0 0% 98%; |
| 372 | 378 | --muted: 0 0% 13%; |
| @@ -377,7 +383,7 @@ | ||
| 377 | 383 | --destructive-foreground: 0 0% 100%; |
| 378 | 384 | --border: 0 0% 17%; |
| 379 | 385 | --input: 0 0% 24%; |
| 380 | - --ring: 0 0% 45%; | |
| 386 | + --ring: var(--accent-dark, 0 0% 45%); | |
| 381 | 387 | --success: 137 58% 58%; |
| 382 | 388 | --warning: 38 92% 54%; |
| 383 | 389 | --info: 219 98% 60%; |
src/stores/preferences-context.test.tsxmodified+9 -4
| @@ -46,16 +46,21 @@ test("preferences stay usable when browser storage writes fail", () => { | ||
| 46 | 46 | setItem.mockRestore(); |
| 47 | 47 | }); |
| 48 | 48 | |
| 49 | -test("palette color applies, persists, and resets", () => { | |
| 49 | +test("palette color applies theme-aware light/dark variants, persists, and resets", () => { | |
| 50 | 50 | localStorage.removeItem("hezarfen.paletteColor"); |
| 51 | 51 | render(() => <PreferencesProvider><PalettePreferenceProbe /></PreferencesProvider>); |
| 52 | 52 | |
| 53 | 53 | fireEvent.click(screen.getByRole("button")); |
| 54 | 54 | expect(localStorage.getItem("hezarfen.paletteColor")).toBe("#fefae0"); |
| 55 | - expect(document.documentElement.style.getPropertyValue("--ui-accent")).toBe("52 94% 94%"); | |
| 56 | - expect(document.documentElement.style.getPropertyValue("--primary-foreground")).toBe("210 10.8% 14.5%"); | |
| 55 | + // Same hue for both themes, but lightness is pinned per theme (not the | |
| 56 | + // swatch's own near-white lightness) so the accent stays legible either way. | |
| 57 | + expect(document.documentElement.style.getPropertyValue("--accent-light")).toBe("52 88% 40%"); | |
| 58 | + expect(document.documentElement.style.getPropertyValue("--accent-light-fg")).toBe("210 10.8% 14.5%"); | |
| 59 | + expect(document.documentElement.style.getPropertyValue("--accent-dark")).toBe("52 88% 68%"); | |
| 60 | + expect(document.documentElement.style.getPropertyValue("--accent-dark-fg")).toBe("210 10.8% 14.5%"); | |
| 57 | 61 | |
| 58 | 62 | fireEvent.click(screen.getByRole("button")); |
| 59 | 63 | expect(localStorage.getItem("hezarfen.paletteColor")).toBeNull(); |
| 60 | - expect(document.documentElement.style.getPropertyValue("--ui-accent")).toBe(""); | |
| 64 | + expect(document.documentElement.style.getPropertyValue("--accent-light")).toBe(""); | |
| 65 | + expect(document.documentElement.style.getPropertyValue("--accent-dark")).toBe(""); | |
| 61 | 66 | }); |
src/stores/preferences-context.tsxmodified+62 -15
| @@ -93,14 +93,14 @@ function readPaletteColor(): string | null { | ||
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | -function hexToHsl(hex: string): string { | |
| 96 | +function hexToHslParts(hex: string): { h: number; s: number; l: number } { | |
| 97 | 97 | const [red, green, blue] = hex.slice(1).match(/.{2}/g)!.map((value) => parseInt(value, 16) / 255); |
| 98 | 98 | const max = Math.max(red, green, blue); |
| 99 | 99 | const min = Math.min(red, green, blue); |
| 100 | 100 | const delta = max - min; |
| 101 | 101 | const lightness = (max + min) / 2; |
| 102 | 102 | |
| 103 | - if (delta === 0) return `0 0% ${Math.round(lightness * 100)}%`; | |
| 103 | + if (delta === 0) return { h: 0, s: 0, l: lightness * 100 }; | |
| 104 | 104 | |
| 105 | 105 | const saturation = delta / (1 - Math.abs(2 * lightness - 1)); |
| 106 | 106 | const hue = |
| @@ -110,17 +110,54 @@ function hexToHsl(hex: string): string { | ||
| 110 | 110 | ? 60 * ((blue - red) / delta + 2) |
| 111 | 111 | : 60 * ((red - green) / delta + 4); |
| 112 | 112 | |
| 113 | - return `${Math.round(hue < 0 ? hue + 360 : hue)} ${Math.round(saturation * 100)}% ${Math.round(lightness * 100)}%`; | |
| 113 | + return { h: hue < 0 ? hue + 360 : hue, s: saturation * 100, l: lightness * 100 }; | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | -function paletteForeground(hex: string): string { | |
| 117 | - const [red, green, blue] = hex.slice(1).match(/.{2}/g)!.map((value) => { | |
| 118 | - const channel = parseInt(value, 16) / 255; | |
| 119 | - return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4; | |
| 120 | - }); | |
| 121 | - return red * 0.2126 + green * 0.7152 + blue * 0.0722 > 0.179 | |
| 122 | - ? "210 10.8% 14.5%" | |
| 123 | - : "210 16.7% 97.6%"; | |
| 116 | +function hslToRgb(h: number, s: number, l: number): [number, number, number] { | |
| 117 | + const sat = s / 100; | |
| 118 | + const light = l / 100; | |
| 119 | + const chroma = (1 - Math.abs(2 * light - 1)) * sat; | |
| 120 | + const x = chroma * (1 - Math.abs(((h / 60) % 2) - 1)); | |
| 121 | + const m = light - chroma / 2; | |
| 122 | + const [r1, g1, b1] = | |
| 123 | + h < 60 ? [chroma, x, 0] | |
| 124 | + : h < 120 ? [x, chroma, 0] | |
| 125 | + : h < 180 ? [0, chroma, x] | |
| 126 | + : h < 240 ? [0, x, chroma] | |
| 127 | + : h < 300 ? [x, 0, chroma] | |
| 128 | + : [chroma, 0, x]; | |
| 129 | + return [(r1 + m) * 255, (g1 + m) * 255, (b1 + m) * 255]; | |
| 130 | +} | |
| 131 | + | |
| 132 | +function relativeLuminance(r: number, g: number, b: number): number { | |
| 133 | + const channel = (value: number) => { | |
| 134 | + const c = value / 255; | |
| 135 | + return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; | |
| 136 | + }; | |
| 137 | + return channel(r) * 0.2126 + channel(g) * 0.7152 + channel(b) * 0.0722; | |
| 138 | +} | |
| 139 | + | |
| 140 | +function foregroundFor(h: number, s: number, l: number): string { | |
| 141 | + const [r, g, b] = hslToRgb(h, s, l); | |
| 142 | + return relativeLuminance(r, g, b) > 0.179 ? "210 10.8% 14.5%" : "210 16.7% 97.6%"; | |
| 143 | +} | |
| 144 | + | |
| 145 | +/** | |
| 146 | + * A theme-appropriate variant of the user's chosen accent: the hue is kept, | |
| 147 | + * saturation is clamped into a tasteful range, but lightness is pinned to a | |
| 148 | + * value known to read well against that theme's background — the swatch's own | |
| 149 | + * lightness is deliberately ignored. Without this, a pastel pick (e.g. | |
| 150 | + * `#fefae0`) would be nearly invisible as a light-mode button, and a near-black | |
| 151 | + * pick (e.g. `#023047`) would vanish against the near-black dark background. | |
| 152 | + */ | |
| 153 | +function themedAccent(hex: string, mode: ThemeMode): { hsl: string; fg: string } { | |
| 154 | + const { h, s } = hexToHslParts(hex); | |
| 155 | + const sat = Math.min(88, Math.max(30, s)); | |
| 156 | + const lightness = mode === "light" ? 40 : 68; | |
| 157 | + return { | |
| 158 | + hsl: `${Math.round(h)} ${Math.round(sat)}% ${lightness}%`, | |
| 159 | + fg: foregroundFor(h, sat, lightness), | |
| 160 | + }; | |
| 124 | 161 | } |
| 125 | 162 | |
| 126 | 163 | function readSidebarCollapsed(): boolean { |
| @@ -154,15 +191,25 @@ export function PreferencesProvider(props: ParentProps) { | ||
| 154 | 191 | |
| 155 | 192 | createEffect(() => { |
| 156 | 193 | const color = paletteColor(); |
| 194 | + const root = document.documentElement.style; | |
| 157 | 195 | if (!color) { |
| 158 | 196 | removeStorage(PALETTE_COLOR_KEY); |
| 159 | - document.documentElement.style.removeProperty("--ui-accent"); | |
| 160 | - document.documentElement.style.removeProperty("--primary-foreground"); | |
| 197 | + root.removeProperty("--accent-light"); | |
| 198 | + root.removeProperty("--accent-light-fg"); | |
| 199 | + root.removeProperty("--accent-dark"); | |
| 200 | + root.removeProperty("--accent-dark-fg"); | |
| 161 | 201 | return; |
| 162 | 202 | } |
| 163 | 203 | writeStorage(PALETTE_COLOR_KEY, color); |
| 164 | - document.documentElement.style.setProperty("--ui-accent", hexToHsl(color)); | |
| 165 | - document.documentElement.style.setProperty("--primary-foreground", paletteForeground(color)); | |
| 204 | + // Both variants are always set, regardless of the active theme, so a live | |
| 205 | + // light/dark toggle picks up the right one immediately via the CSS | |
| 206 | + // `[data-kb-theme="dark"]` cascade — no need to recompute on theme change. | |
| 207 | + const light = themedAccent(color, "light"); | |
| 208 | + const dark = themedAccent(color, "dark"); | |
| 209 | + root.setProperty("--accent-light", light.hsl); | |
| 210 | + root.setProperty("--accent-light-fg", light.fg); | |
| 211 | + root.setProperty("--accent-dark", dark.hsl); | |
| 212 | + root.setProperty("--accent-dark-fg", dark.fg); | |
| 166 | 213 | }); |
| 167 | 214 | |
| 168 | 215 | createEffect(() => { |