TypeScript
refactor(web): remove obsolete HSL theme generator (#10061)
Julius Marminge80f775a42c44
diff --git a/apps/web/src/themePalette.test.ts b/apps/web/src/themePalette.test.tsindex a76885a862e..e1d9bfb74ca 100644--- a/apps/web/src/themePalette.test.ts+++ b/apps/web/src/themePalette.test.ts@@ -34,7 +34,6 @@ import {OCEAN_THEME,updateCustomTheme,CUSTOM_THEMES_STORAGE_KEY,- createManagedThemeColors,createVividThemeColors,getDefaultThemeColors,themeColorToHex,@@ -91,50 +90,6 @@ describe("theme files", () => {}});- it("derives a readable palette from extreme simple-editor colors", () => {- const light = createManagedThemeColors("light", "#111827", "#ffff00");- const dark = createManagedThemeColors("dark", "#ffffff", "#ffff00");- const darkDefaults = getDefaultThemeColors("dark");-- expect(asHex(light.canvas)).not.toBe("#111827");- expect(asHex(dark.canvas)).not.toBe("#ffffff");- expect(contrastRatio(light.accent, light.canvas)).toBeGreaterThanOrEqual(4.5);- expect(contrastRatio(dark.accent, dark.canvas)).toBeGreaterThanOrEqual(4.5);- expect(contrastRatio(light.textMuted, light.canvas)).toBeGreaterThanOrEqual(4.5);- expect(contrastRatio(dark.textMuted, dark.canvas)).toBeGreaterThanOrEqual(4.5);- expect(contrastRatio(light.textMuted, light.canvas)).toBeLessThan(5.5);- expect(contrastRatio(dark.textMuted, dark.canvas)).toBeLessThan(5.5);- expect(contrastRatio(light.textMuted, light.canvas)).toBeCloseTo(4.705, 1);- expect(contrastRatio(dark.textMuted, dark.canvas)).toBeCloseTo(5.082, 1);- expect(light.secondaryLabel).toBe(light.textMuted);- expect(dark.secondaryLabel).toBe(dark.textMuted);- expect(contrastRatio(light.accentForeground, light.accent)).toBeGreaterThanOrEqual(4.5);- expect(contrastRatio(dark.accentForeground, dark.accent)).toBeGreaterThanOrEqual(4.5);- // Status colors fall back to T3 Code's standard red and amber rather than- // the flagship palette's, so no generated theme inherits a brand tint.- const channels = (value: string) =>- [1, 3, 5].map((index) => Number.parseInt(asHex(value).slice(index, index + 2), 16)) as [- number,- number,- number,- ];- for (const colors of [light, dark]) {- const [errorRed, errorGreen, errorBlue] = channels(colors.error);- // Red leads by a wide margin; the old default was a pink whose blue sat- // close behind its red.- expect(errorRed).toBeGreaterThan(errorGreen * 2);- expect(errorRed).toBeGreaterThan(errorBlue * 2);- expect(contrastRatio(colors.error, "#ffffff")).toBeGreaterThanOrEqual(2.5);- expect(contrastRatio(colors.errorForeground, colors.errorSurface)).toBeGreaterThanOrEqual(- 4.5,- );- const [warnRed, warnGreen, warnBlue] = channels(colors.warning);- expect(warnRed).toBeGreaterThan(warnBlue);- expect(warnGreen).toBeGreaterThan(warnBlue);- }- expect(asHex(dark.error)).not.toBe(asHex(darkDefaults.error));- });-it("keeps stock dark controls in the neutral-black surface hierarchy", () => {expectThemeColors(getStandardThemeColors("dark"), {canvas: "#0a0a0a",@@ -158,6 +113,12 @@ describe("theme files", () => {["light", "#111827", "#8ab4f8"],["dark", "#f5ecf5", "#a84370"],];+ const channels = (value: string) =>+ [1, 3, 5].map((index) => Number.parseInt(asHex(value).slice(index, index + 2), 16)) as [+ number,+ number,+ number,+ ];for (const [appearance, canvas, accent] of seeds) {const colors = createVividThemeColors(appearance, canvas, accent);// Exact seeds are honored.@@ -193,6 +154,17 @@ describe("theme files", () => {expect(colors.messageAction).not.toBe(colors.accent);// Update family follows the theme, not the default palette.expect(asHex(colors.update)).toBe(accent);+ // Semantic statuses stay red and amber instead of inheriting a brand tint.+ const [errorRed, errorGreen, errorBlue] = channels(colors.error);+ expect(errorRed).toBeGreaterThan(errorGreen * 2);+ expect(errorRed).toBeGreaterThan(errorBlue * 2);+ expect(contrastRatio(colors.error, "#ffffff")).toBeGreaterThanOrEqual(2.5);+ expect(contrastRatio(colors.errorForeground, colors.errorSurface)).toBeGreaterThanOrEqual(+ 4.5,+ );+ const [warnRed, warnGreen, warnBlue] = channels(colors.warning);+ expect(warnRed).toBeGreaterThan(warnBlue);+ expect(warnGreen).toBeGreaterThan(warnBlue);}});@@ -202,8 +174,8 @@ describe("theme files", () => {const inverted = [createVividThemeColors("light", "#111827", "#8ab4f8"),createVividThemeColors("dark", "#f5ecf5", "#a84370"),- createManagedThemeColors("light", "#0d1117", "#69b1ff", { exactSeeds: true }),- createManagedThemeColors("dark", "#fdfdfd", "#c2571b", { exactSeeds: true }),+ createVividThemeColors("light", "#0d1117", "#69b1ff"),+ createVividThemeColors("dark", "#fdfdfd", "#c2571b"),];for (const colors of inverted) {expect(contrastRatio(colors.errorForeground, colors.errorSurface)).toBeGreaterThanOrEqual(diff --git a/apps/web/src/themePalette.ts b/apps/web/src/themePalette.tsindex f67eb943e3f..f4b5c83df39 100644--- a/apps/web/src/themePalette.ts+++ b/apps/web/src/themePalette.ts@@ -475,12 +475,6 @@ type ThemeRgbColor = {b: number;};-type ThemeHslColor = {- h: number;- s: number;- l: number;-};-type ThemeOklch = { L: number; C: number; h: number };type ParsedThemeColor = { color: ThemeOklch; alpha: number };@@ -601,48 +595,6 @@ function canonicalizeThemeDefinition(theme: ThemeDefinition): ThemeDefinition {};}-function themeRgbToHsl(color: ThemeRgbColor): ThemeHslColor {- const red = color.r / 255;- const green = color.g / 255;- const blue = color.b / 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 { h: 0, s: 0, l: lightness };-- const saturation = delta / (1 - Math.abs(2 * lightness - 1));- let hue = 0;- if (max === red) hue = ((green - blue) / delta) % 6;- else if (max === green) hue = (blue - red) / delta + 2;- else hue = (red - green) / delta + 4;-- return { h: (hue * 60 + 360) % 360, s: saturation, l: lightness };-}--function themeHslToRgb(color: ThemeHslColor): ThemeRgbColor {- const hue = ((color.h % 360) + 360) % 360;- const chroma = (1 - Math.abs(2 * color.l - 1)) * color.s;- const hueSector = hue / 60;- const secondary = chroma * (1 - Math.abs((hueSector % 2) - 1));- const match = color.l - chroma / 2;- const [red, green, blue] =- hueSector < 1- ? [chroma, secondary, 0]- : hueSector < 2- ? [secondary, chroma, 0]- : hueSector < 3- ? [0, chroma, secondary]- : hueSector < 4- ? [0, secondary, chroma]- : hueSector < 5- ? [secondary, 0, chroma]- : [chroma, 0, secondary];-- return { r: (red + match) * 255, g: (green + match) * 255, b: (blue + match) * 255 };-}-function mixThemeRgbColors(base: ThemeRgbColor,overlay: ThemeRgbColor,@@ -1046,205 +998,6 @@ function standardMutedThemeText(return readableThemeText(background, foreground, 1, target);}-function managedThemeBackground(value: string, appearance: ThemeAppearance): ThemeRgbColor {- const selected = parseThemeRgbColor(- value,- appearance === "dark" ? { r: 24, g: 15, b: 27 } : { r: 250, g: 245, b: 250 },- );- const hsl = themeRgbToHsl(selected);- return themeHslToRgb({- h: hsl.h,- // A background tint should support the selected mode, not turn the whole- // app into a high-saturation surface.- s: Math.min(hsl.s, appearance === "dark" ? 0.3 : 0.2),- l:- appearance === "dark"- ? Math.min(0.13, Math.max(0.07, hsl.l))- : Math.min(0.985, Math.max(0.94, hsl.l)),- });-}--function managedThemeAccent(- value: string,- appearance: ThemeAppearance,- background: ThemeRgbColor,-): ThemeRgbColor {- const selected = parseThemeRgbColor(value, { r: 168, g: 67, b: 112 });- const hsl = themeRgbToHsl(selected);- const preferredLightness =- appearance === "dark"- ? Math.min(0.72, Math.max(0.42, hsl.l))- : Math.min(0.58, Math.max(0.35, hsl.l));- const lightnessRange: readonly [number, number] =- appearance === "dark" ? [0.42, 0.82] : [0.22, 0.58];- const saturation = Math.min(hsl.s, 0.82);- const candidates = Array.from({ length: 61 }, (_, index) => {- const lightness =- lightnessRange[0] + ((lightnessRange[1] - lightnessRange[0]) * index) / (61 - 1);- const color = themeHslToRgb({ h: hsl.h, s: saturation, l: lightness });- return { color, lightness, contrast: themeContrastRatio(color, background) };- });- // Leave a little room for browser color conversion at render time.- const readableCandidates = candidates.filter((candidate) => candidate.contrast >= 4.7);- const pool = readableCandidates.length > 0 ? readableCandidates : candidates;-- return pool.reduce((best, candidate) => {- const distance = Math.abs(candidate.lightness - preferredLightness);- const bestDistance = Math.abs(best.lightness - preferredLightness);- return distance < bestDistance ||- (distance === bestDistance && candidate.contrast > best.contrast)- ? candidate- : best;- }).color;-}--/**- * Creates the guided palette used by the basic theme editor. The two user- * colors control the mood, while dependent roles are generated together so- * text, surfaces, message actions, code, and terminal UI stay coherent.- */-export function createManagedThemeColors(- appearance: ThemeAppearance,- backgroundValue: string,- accentValue: string,- options?: {- /** Use the seeds exactly as given instead of nudging them into the- * readability envelope. Derived foregrounds still adapt for contrast. */- exactSeeds?: boolean;- },-): ThemeColors {- const defaults = getDefaultThemeColors(appearance);- const canvas = options?.exactSeeds- ? parseThemeRgbColor(- backgroundValue,- appearance === "dark" ? { r: 24, g: 15, b: 27 } : { r: 250, g: 245, b: 250 },- )- : managedThemeBackground(backgroundValue, appearance);- const accent = options?.exactSeeds- ? parseThemeRgbColor(accentValue, { r: 168, g: 67, b: 112 })- : managedThemeAccent(accentValue, appearance, canvas);- const text = readableThemeForeground(canvas);- const textMuted = standardMutedThemeText(canvas, text);- // The top bar is part of the main panel, not a separate chrome layer: it- // shares the canvas, and its controls sit on the panel's own surfaces.- const chrome = canvas;- const sidebar = mixThemeRgbColors(canvas, accent, 0.08);- const surfaceRaised = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.12 : 0.035);- const surfaceOverlay = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.18 : 0.06);- const secondary = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.2 : 0.08);- const muted = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.13 : 0.06);- const mutedForeground = readableThemeText(muted, text, 1, 4.6);- const placeholder = readableThemeText(surfaceRaised, text, 1, 4.6);- const accentSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.3 : 0.14);- const messageSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.36 : 0.18);- const toolbarControl = mixThemeRgbColors(chrome, accent, appearance === "dark" ? 0.2 : 0.08);- const toolbarBorder = mixThemeRgbColors(chrome, accent, appearance === "dark" ? 0.35 : 0.14);- const accentForeground = readableThemeForeground(accent);- // Code and terminal are large surfaces: they keep the canvas hue instead of- // drifting toward the foreground grey. Code sits just above the canvas —- // a whisper of the text tint — and the terminal sits on the canvas itself.- const codeBackground = mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.06 : 0.025);- const terminalBackground = canvas;- const messageActionHover = mixThemeRgbColors(- accent,- accentForeground === THEME_LIGHT_FOREGROUND || accentForeground === THEME_WHITE_FOREGROUND- ? THEME_BLACK_FOREGROUND- : THEME_WHITE_FOREGROUND,- 0.12,- );-- // The update family follows the accent instead of inheriting the default- // palette's brand color, so generated themes carry their own identity in- // update pills and banners. Error and warning stay semantic defaults.- const updateSurface = mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.32 : 0.16);- const updateForeground = mixThemeRgbColors(- accent,- appearance === "dark" ? THEME_WHITE_FOREGROUND : THEME_BLACK_FOREGROUND,- 0.35,- );-- return {- ...defaults,- ...standardStatusColors(canvas),- update: themeRgbToThemeColor(accent),- updateForeground: themeRgbToThemeColor(updateForeground),- updateSurface: themeRgbToThemeColor(updateSurface),- canvas: themeRgbToThemeColor(canvas),- chrome: themeRgbToThemeColor(chrome),- toolbar: themeRgbToThemeColor(chrome),- toolbarForeground: themeRgbToThemeColor(text),- toolbarBorder: themeRgbToThemeColor(toolbarBorder),- toolbarControl: themeRgbToThemeColor(toolbarControl),- toolbarControlForeground: themeRgbToThemeColor(text),- toolbarControlHover: themeRgbToThemeColor(accentSurface),- surface: themeRgbToThemeColor(canvas),- surfaceRaised: themeRgbToThemeColor(surfaceRaised),- surfaceOverlay: themeRgbToThemeColor(surfaceOverlay),- text: themeRgbToThemeColor(text),- textMuted: themeRgbToThemeColor(textMuted),- // Borders blend through the accent before lightening so control chrome- // carries the theme hue like the hand-tuned palettes (#5c345b, #e0d3e1)- // instead of flattening to grey.- border: themeRgbToThemeColor(- mixThemeRgbColors(- mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.22 : 0.1),- text,- 0.1,- ),- ),- input: themeRgbToThemeColor(- mixThemeRgbColors(- mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.3 : 0.14),- text,- appearance === "dark" ? 0.14 : 0.13,- ),- ),- focus: themeRgbToThemeColor(accent),- accent: themeRgbToThemeColor(accent),- accentForeground: themeRgbToThemeColor(accentForeground),- secondary: themeRgbToThemeColor(secondary),- secondaryForeground: themeRgbToThemeColor(readableThemeForeground(secondary)),- muted: themeRgbToThemeColor(muted),- mutedForeground: themeRgbToThemeColor(mutedForeground),- placeholder: themeRgbToThemeColor(placeholder),- secondaryLabel: themeRgbToThemeColor(textMuted),- iconMuted: themeRgbToThemeColor(textMuted),- accentSurface: themeRgbToThemeColor(accentSurface),- accentSurfaceForeground: themeRgbToThemeColor(readableThemeForeground(accentSurface)),- messageSurface: themeRgbToThemeColor(messageSurface),- messageForeground: themeRgbToThemeColor(readableThemeForeground(messageSurface)),- messageAction: themeRgbToThemeColor(accent),- messageActionForeground: themeRgbToThemeColor(accentForeground),- messageActionHover: themeRgbToThemeColor(messageActionHover),- codeBackground: themeRgbToThemeColor(codeBackground),- codeForeground: themeRgbToThemeColor(readableThemeForeground(codeBackground)),- sidebar: themeRgbToThemeColor(sidebar),- sidebarForeground: themeRgbToThemeColor(readableThemeForeground(sidebar)),- sidebarMutedForeground: themeRgbToThemeColor(standardMutedThemeText(sidebar, text)),- sidebarControlSurface: themeRgbToThemeColor(- mixThemeRgbColors(sidebar, text, appearance === "dark" ? 0.16 : 0.08),- ),- sidebarRowHover: themeRgbToThemeColor(mixThemeRgbColors(sidebar, accent, 0.12)),- sidebarRowActive: themeRgbToThemeColor(mixThemeRgbColors(sidebar, accent, 0.2)),- sidebarRowSelected: themeRgbToThemeColor(mixThemeRgbColors(sidebar, accent, 0.24)),- sidebarBorder: themeRgbToThemeColor(- mixThemeRgbColors(sidebar, text, appearance === "dark" ? 0.35 : 0.12),- ),- terminalBackground: themeRgbToThemeColor(terminalBackground),- terminalForeground: themeRgbToThemeColor(readableThemeForeground(terminalBackground)),- terminalCursor: themeRgbToThemeColor(accent),- terminalSelection: themeRgbToThemeColor(- mixThemeRgbColors(canvas, accent, appearance === "dark" ? 0.35 : 0.18),- ),- terminalScrollbar: themeRgbToThemeColor(- mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.42 : 0.22),- ),- terminalScrollbarHover: themeRgbToThemeColor(- mixThemeRgbColors(canvas, text, appearance === "dark" ? 0.55 : 0.32),- ),- };-}-/** Theme-file defaults follow the flagship palette for the requested mode. */export function getDefaultThemeColors(appearance: ThemeAppearance): ThemeColors {return appearance === "dark" ? T3_CHAT_THEME.variants!.dark! : T3_CHAT_THEME.colors;