From 67af0d436cb5abf3168ef30d9e31aa6bdaf48174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sun, 2 Aug 2026 19:14:43 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=A8=20theme-aware=20accent=20c?= =?UTF-8?q?olor=20with=20pinned=20lightness=20variants=20-=20picked=20hue?= =?UTF-8?q?=20is=20kept,=20saturation=20clamped,=20but=20lightness=20pinne?= =?UTF-8?q?d=20per=20theme=20=20=20(light=2040,=20dark=2068)=20so=20pastel?= =?UTF-8?q?=20and=20near-black=20picks=20stay=20legible=20in=20=20=20both?= =?UTF-8?q?=20modes;=20foreground=20chosen=20by=20relative=20luminance=20-?= =?UTF-8?q?=20--accent-light/--accent-dark=20CSS=20vars=20(with=20fg=20cou?= =?UTF-8?q?nterparts)=20feed=20=20=20--ui-accent/--primary-foreground/--ri?= =?UTF-8?q?ng,=20both=20set=20up=20front=20so=20a=20live=20=20=20theme=20t?= =?UTF-8?q?oggle=20applies=20instantly=20via=20the=20dark=20cascade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.css | 18 ++++-- src/stores/preferences-context.test.tsx | 13 +++-- src/stores/preferences-context.tsx | 77 ++++++++++++++++++++----- 3 files changed, 83 insertions(+), 25 deletions(-) diff --git a/src/index.css b/src/index.css index ff3095c..11ce4d2 100644 --- a/src/index.css +++ b/src/index.css @@ -328,7 +328,10 @@ @layer base { /* Hue-neutral gray scale + Carbon-style semantic colors. */ :root { - --ui-accent: 0 0% 13%; + /* --accent-light / --accent-light-fg are set inline by preferences-context + when the user picks an accent color; the fallback here is the neutral + default for everyone else. */ + --ui-accent: var(--accent-light, 0 0% 13%); --background: 0 0% 98%; --foreground: 0 0% 8%; --card: 0 0% 100%; @@ -336,7 +339,7 @@ --popover: 0 0% 100%; --popover-foreground: 0 0% 8%; --primary: var(--ui-accent); - --primary-foreground: 0 0% 98%; + --primary-foreground: var(--accent-light-fg, 0 0% 98%); --secondary: 0 0% 94%; --secondary-foreground: 0 0% 16%; --muted: 0 0% 96%; @@ -347,7 +350,7 @@ --destructive-foreground: 0 0% 100%; --border: 0 0% 89%; --input: 0 0% 82%; - --ring: 0 0% 45%; + --ring: var(--accent-light, 0 0% 45%); --radius: 0.5rem; --success: 137 63% 39%; --warning: 38 92% 50%; @@ -358,7 +361,10 @@ } [data-kb-theme="dark"] { - --ui-accent: 0 0% 92%; + /* --accent-dark / --accent-dark-fg are set inline by preferences-context — + a separate, lighter-lightness variant of the same picked hue so it stays + legible against this theme's near-black background. */ + --ui-accent: var(--accent-dark, 0 0% 92%); --background: 0 0% 4%; --foreground: 0 0% 98%; --card: 0 0% 8%; @@ -366,7 +372,7 @@ --popover: 0 0% 8%; --popover-foreground: 0 0% 98%; --primary: var(--ui-accent); - --primary-foreground: 0 0% 6%; + --primary-foreground: var(--accent-dark-fg, 0 0% 6%); --secondary: 0 0% 16%; --secondary-foreground: 0 0% 98%; --muted: 0 0% 13%; @@ -377,7 +383,7 @@ --destructive-foreground: 0 0% 100%; --border: 0 0% 17%; --input: 0 0% 24%; - --ring: 0 0% 45%; + --ring: var(--accent-dark, 0 0% 45%); --success: 137 58% 58%; --warning: 38 92% 54%; --info: 219 98% 60%; diff --git a/src/stores/preferences-context.test.tsx b/src/stores/preferences-context.test.tsx index 66176d0..2d24b93 100644 --- a/src/stores/preferences-context.test.tsx +++ b/src/stores/preferences-context.test.tsx @@ -46,16 +46,21 @@ test("preferences stay usable when browser storage writes fail", () => { setItem.mockRestore(); }); -test("palette color applies, persists, and resets", () => { +test("palette color applies theme-aware light/dark variants, persists, and resets", () => { localStorage.removeItem("hezarfen.paletteColor"); render(() => ); fireEvent.click(screen.getByRole("button")); expect(localStorage.getItem("hezarfen.paletteColor")).toBe("#fefae0"); - expect(document.documentElement.style.getPropertyValue("--ui-accent")).toBe("52 94% 94%"); - expect(document.documentElement.style.getPropertyValue("--primary-foreground")).toBe("210 10.8% 14.5%"); + // Same hue for both themes, but lightness is pinned per theme (not the + // swatch's own near-white lightness) so the accent stays legible either way. + expect(document.documentElement.style.getPropertyValue("--accent-light")).toBe("52 88% 40%"); + expect(document.documentElement.style.getPropertyValue("--accent-light-fg")).toBe("210 10.8% 14.5%"); + expect(document.documentElement.style.getPropertyValue("--accent-dark")).toBe("52 88% 68%"); + expect(document.documentElement.style.getPropertyValue("--accent-dark-fg")).toBe("210 10.8% 14.5%"); fireEvent.click(screen.getByRole("button")); expect(localStorage.getItem("hezarfen.paletteColor")).toBeNull(); - expect(document.documentElement.style.getPropertyValue("--ui-accent")).toBe(""); + expect(document.documentElement.style.getPropertyValue("--accent-light")).toBe(""); + expect(document.documentElement.style.getPropertyValue("--accent-dark")).toBe(""); }); diff --git a/src/stores/preferences-context.tsx b/src/stores/preferences-context.tsx index 493aca7..1572f33 100644 --- a/src/stores/preferences-context.tsx +++ b/src/stores/preferences-context.tsx @@ -93,14 +93,14 @@ function readPaletteColor(): string | null { } } -function hexToHsl(hex: string): string { +function hexToHslParts(hex: string): { h: number; s: number; l: number } { const [red, green, blue] = hex.slice(1).match(/.{2}/g)!.map((value) => parseInt(value, 16) / 255); const max = Math.max(red, green, blue); const min = Math.min(red, green, blue); const delta = max - min; const lightness = (max + min) / 2; - if (delta === 0) return `0 0% ${Math.round(lightness * 100)}%`; + if (delta === 0) return { h: 0, s: 0, l: lightness * 100 }; const saturation = delta / (1 - Math.abs(2 * lightness - 1)); const hue = @@ -110,17 +110,54 @@ function hexToHsl(hex: string): string { ? 60 * ((blue - red) / delta + 2) : 60 * ((red - green) / delta + 4); - return `${Math.round(hue < 0 ? hue + 360 : hue)} ${Math.round(saturation * 100)}% ${Math.round(lightness * 100)}%`; + return { h: hue < 0 ? hue + 360 : hue, s: saturation * 100, l: lightness * 100 }; } -function paletteForeground(hex: string): string { - const [red, green, blue] = hex.slice(1).match(/.{2}/g)!.map((value) => { - const channel = parseInt(value, 16) / 255; - return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4; - }); - return red * 0.2126 + green * 0.7152 + blue * 0.0722 > 0.179 - ? "210 10.8% 14.5%" - : "210 16.7% 97.6%"; +function hslToRgb(h: number, s: number, l: number): [number, number, number] { + const sat = s / 100; + const light = l / 100; + const chroma = (1 - Math.abs(2 * light - 1)) * sat; + const x = chroma * (1 - Math.abs(((h / 60) % 2) - 1)); + const m = light - chroma / 2; + const [r1, g1, b1] = + h < 60 ? [chroma, x, 0] + : h < 120 ? [x, chroma, 0] + : h < 180 ? [0, chroma, x] + : h < 240 ? [0, x, chroma] + : h < 300 ? [x, 0, chroma] + : [chroma, 0, x]; + return [(r1 + m) * 255, (g1 + m) * 255, (b1 + m) * 255]; +} + +function relativeLuminance(r: number, g: number, b: number): number { + const channel = (value: number) => { + const c = value / 255; + return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; + }; + return channel(r) * 0.2126 + channel(g) * 0.7152 + channel(b) * 0.0722; +} + +function foregroundFor(h: number, s: number, l: number): string { + const [r, g, b] = hslToRgb(h, s, l); + return relativeLuminance(r, g, b) > 0.179 ? "210 10.8% 14.5%" : "210 16.7% 97.6%"; +} + +/** + * A theme-appropriate variant of the user's chosen accent: the hue is kept, + * saturation is clamped into a tasteful range, but lightness is pinned to a + * value known to read well against that theme's background — the swatch's own + * lightness is deliberately ignored. Without this, a pastel pick (e.g. + * `#fefae0`) would be nearly invisible as a light-mode button, and a near-black + * pick (e.g. `#023047`) would vanish against the near-black dark background. + */ +function themedAccent(hex: string, mode: ThemeMode): { hsl: string; fg: string } { + const { h, s } = hexToHslParts(hex); + const sat = Math.min(88, Math.max(30, s)); + const lightness = mode === "light" ? 40 : 68; + return { + hsl: `${Math.round(h)} ${Math.round(sat)}% ${lightness}%`, + fg: foregroundFor(h, sat, lightness), + }; } function readSidebarCollapsed(): boolean { @@ -154,15 +191,25 @@ export function PreferencesProvider(props: ParentProps) { createEffect(() => { const color = paletteColor(); + const root = document.documentElement.style; if (!color) { removeStorage(PALETTE_COLOR_KEY); - document.documentElement.style.removeProperty("--ui-accent"); - document.documentElement.style.removeProperty("--primary-foreground"); + root.removeProperty("--accent-light"); + root.removeProperty("--accent-light-fg"); + root.removeProperty("--accent-dark"); + root.removeProperty("--accent-dark-fg"); return; } writeStorage(PALETTE_COLOR_KEY, color); - document.documentElement.style.setProperty("--ui-accent", hexToHsl(color)); - document.documentElement.style.setProperty("--primary-foreground", paletteForeground(color)); + // Both variants are always set, regardless of the active theme, so a live + // light/dark toggle picks up the right one immediately via the CSS + // `[data-kb-theme="dark"]` cascade — no need to recompute on theme change. + const light = themedAccent(color, "light"); + const dark = themedAccent(color, "dark"); + root.setProperty("--accent-light", light.hsl); + root.setProperty("--accent-light-fg", light.fg); + root.setProperty("--accent-dark", dark.hsl); + root.setProperty("--accent-dark-fg", dark.fg); }); createEffect(() => {