forge
Explore
Log in

Don't you dare go Hollow

🎨 theme-aware accent color with pinned lightness variants

Browse files at this commit

67af0d436cb5abf3168ef30d9e31aa6bdaf48174Burak BoduroğluParent: 5be4692
  • feat
    • 🎨 theme-aware accent color with pinned lightness variants
Show full message
feat: 🎨 theme-aware accent color with pinned lightness variants
- picked hue is kept, saturation clamped, but lightness pinned per theme
  (light 40, dark 68) so pastel and near-black picks stay legible in
  both modes; foreground chosen by relative luminance
- --accent-light/--accent-dark CSS vars (with fg counterparts) feed
  --ui-accent/--primary-foreground/--ring, both set up front so a live
  theme toggle applies instantly via the dark cascade
src/index.cssmodified+12 -6
@@ -328,7 +328,10 @@
328328 @layer base {
329329 /* Hue-neutral gray scale + Carbon-style semantic colors. */
330330 :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%);
332335 --background: 0 0% 98%;
333336 --foreground: 0 0% 8%;
334337 --card: 0 0% 100%;
@@ -336,7 +339,7 @@
336339 --popover: 0 0% 100%;
337340 --popover-foreground: 0 0% 8%;
338341 --primary: var(--ui-accent);
339- --primary-foreground: 0 0% 98%;
342+ --primary-foreground: var(--accent-light-fg, 0 0% 98%);
340343 --secondary: 0 0% 94%;
341344 --secondary-foreground: 0 0% 16%;
342345 --muted: 0 0% 96%;
@@ -347,7 +350,7 @@
347350 --destructive-foreground: 0 0% 100%;
348351 --border: 0 0% 89%;
349352 --input: 0 0% 82%;
350- --ring: 0 0% 45%;
353+ --ring: var(--accent-light, 0 0% 45%);
351354 --radius: 0.5rem;
352355 --success: 137 63% 39%;
353356 --warning: 38 92% 50%;
@@ -358,7 +361,10 @@
358361 }
359362
360363 [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%);
362368 --background: 0 0% 4%;
363369 --foreground: 0 0% 98%;
364370 --card: 0 0% 8%;
@@ -366,7 +372,7 @@
366372 --popover: 0 0% 8%;
367373 --popover-foreground: 0 0% 98%;
368374 --primary: var(--ui-accent);
369- --primary-foreground: 0 0% 6%;
375+ --primary-foreground: var(--accent-dark-fg, 0 0% 6%);
370376 --secondary: 0 0% 16%;
371377 --secondary-foreground: 0 0% 98%;
372378 --muted: 0 0% 13%;
@@ -377,7 +383,7 @@
377383 --destructive-foreground: 0 0% 100%;
378384 --border: 0 0% 17%;
379385 --input: 0 0% 24%;
380- --ring: 0 0% 45%;
386+ --ring: var(--accent-dark, 0 0% 45%);
381387 --success: 137 58% 58%;
382388 --warning: 38 92% 54%;
383389 --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", () => {
4646 setItem.mockRestore();
4747 });
4848
49-test("palette color applies, persists, and resets", () => {
49+test("palette color applies theme-aware light/dark variants, persists, and resets", () => {
5050 localStorage.removeItem("hezarfen.paletteColor");
5151 render(() => <PreferencesProvider><PalettePreferenceProbe /></PreferencesProvider>);
5252
5353 fireEvent.click(screen.getByRole("button"));
5454 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%");
5761
5862 fireEvent.click(screen.getByRole("button"));
5963 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("");
6166 });
src/stores/preferences-context.tsxmodified+62 -15
@@ -93,14 +93,14 @@ function readPaletteColor(): string | null {
9393 }
9494 }
9595
96-function hexToHsl(hex: string): string {
96+function hexToHslParts(hex: string): { h: number; s: number; l: number } {
9797 const [red, green, blue] = hex.slice(1).match(/.{2}/g)!.map((value) => parseInt(value, 16) / 255);
9898 const max = Math.max(red, green, blue);
9999 const min = Math.min(red, green, blue);
100100 const delta = max - min;
101101 const lightness = (max + min) / 2;
102102
103- if (delta === 0) return `0 0% ${Math.round(lightness * 100)}%`;
103+ if (delta === 0) return { h: 0, s: 0, l: lightness * 100 };
104104
105105 const saturation = delta / (1 - Math.abs(2 * lightness - 1));
106106 const hue =
@@ -110,17 +110,54 @@ function hexToHsl(hex: string): string {
110110 ? 60 * ((blue - red) / delta + 2)
111111 : 60 * ((red - green) / delta + 4);
112112
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 };
114114 }
115115
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+ };
124161 }
125162
126163 function readSidebarCollapsed(): boolean {
@@ -154,15 +191,25 @@ export function PreferencesProvider(props: ParentProps) {
154191
155192 createEffect(() => {
156193 const color = paletteColor();
194+ const root = document.documentElement.style;
157195 if (!color) {
158196 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");
161201 return;
162202 }
163203 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);
166213 });
167214
168215 createEffect(() => {