@devries-cameron20 / t3code-1 / commits / 7845898c87
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): imported VS Code themes keep destructive buttons red (#21924)

Louis Gollut committed Oct 22, 2026 at 00:13 UTC 7845898c871bb0776eb8beda08a7b6907beca090
3 files changed +81 -3
apps/web/src/themePalette.ts
+19
@@ -558,6 +558,25 @@ function solveOklchLightness(
558 return { ...base, L: direction === "lighter" ? high : low };
559 }
560
561 +/**
562 + * Moves only the lightness, so a color that is too dim on `surface` keeps its
563 + * hue instead of collapsing to black or white.
564 + */
565 +export function readableThemeColorOn(color: string, surface: string, minContrast: number): string {
566 + const parsedColor = parseThemeColor(color);
567 + const parsedSurface = parseThemeColor(surface);
568 + if (!parsedColor || !parsedSurface) return color;
569 + const surfaceRgb = themeOklchToRgb(parsedSurface.color);
570 + if (themeContrastRatio(themeOklchToRgb(parsedColor.color), surfaceRgb) >= minContrast) {
571 + return color;
572 + }
573 + const direction = themeRelativeLuminance(surfaceRgb) < 0.179 ? "lighter" : "darker";
574 + const solved = solveOklchLightness(parsedColor.color, surfaceRgb, minContrast, direction);
575 + // Return the 8-bit color the solver measured: the unrounded OKLCH can round
576 + // to a neighbor just under the target once it is stored and read back.
577 + return themeRgbToHexColor(themeOklchToRgb(solved));
578 +}
579 +
580 const STANDARD_STATUS_COLORS = {
581 light: {
582 error: "#fb2c36",
apps/web/src/vscodeThemeImport.test.ts
+50 -1
@@ -1,6 +1,11 @@
1 import { describe, expect, it } from "vite-plus/test";
2
3 -import { getThemeColorsForMode, themeColorToHex, THEME_FILE_VERSION } from "./themePalette";
3 +import {
4 + getThemeColorsForMode,
5 + themeColorToHex,
6 + THEME_FILE_VERSION,
7 + toCanonicalThemeColor,
8 +} from "./themePalette";
9 import {
10 isVsCodeThemeFile,
11 pairVsCodeThemes,
@@ -30,6 +35,11 @@ function contrastRatio(first: string, second: string): number {
35 return (lighter + 0.05) / (darker + 0.05);
36 }
37
38 +function oklchParts(value: string): { chroma: number; hue: number } {
39 + const [, chroma, hue] = /^oklch\(\S+ (\S+) (\S+)\)$/.exec(value)!;
40 + return { chroma: Number(chroma), hue: Number(hue) };
41 +}
42 +
43 const VSCODE_DARK = {
44 name: "pierre-dark-soft",
45 type: "dark",
@@ -293,6 +303,45 @@ describe("VS Code theme import", () => {
303 expect(hc.appearance).toBe("light");
304 });
305
306 + it.each([
307 + ["dark", "#282828"],
308 + ["light", "#ffffff"],
309 + ] as const)(
310 + "keeps a dim status color's hue on a %s canvas instead of replacing it",
311 + (type, canvas) => {
312 + const theme = parseVsCodeThemeFile({
313 + name: "Gruvbox",
314 + type,
315 + colors: {
316 + "editor.background": canvas,
317 + "editorError.foreground": "#fb4934",
318 + "editorWarning.foreground": "#b57614",
319 + },
320 + });
321 + for (const [role, source] of [
322 + ["error", "#fb4934"],
323 + ["warning", "#b57614"],
324 + ] as const) {
325 + const imported = oklchParts(theme.colors[role]);
326 + expect(contrastRatio(theme.colors[role], theme.colors.canvas)).toBeGreaterThanOrEqual(4.5);
327 + expect(imported.chroma).toBeGreaterThan(0.1);
328 + // 8-bit sRGB can only approximate the source hue at the new lightness.
329 + expect(
330 + Math.abs(imported.hue - oklchParts(toCanonicalThemeColor(source)!).hue),
331 + ).toBeLessThan(2);
332 + }
333 + },
334 + );
335 +
336 + it("keeps the derived status color when the file hides its own", () => {
337 + const theme = parseVsCodeThemeFile({
338 + name: "Hidden squiggles",
339 + type: "dark",
340 + colors: { "editor.background": "#101010", "editorError.foreground": "#ff000000" },
341 + });
342 + expect(oklchParts(theme.colors.error).chroma).toBeGreaterThan(0.1);
343 + });
344 +
345 it("stays readable when the file replaces a surface but not its foreground", () => {
346 const theme = parseVsCodeThemeFile({
347 name: "Split",
apps/web/src/vscodeThemeImport.ts
+12 -2
@@ -4,6 +4,7 @@ import {
4 getThemeModes,
5 isReservedThemeId,
6 parseThemeFile,
7 + readableThemeColorOn,
8 themeColorToHex,
9 themeIdFromName,
10 THEME_FILE_VERSION,
@@ -251,6 +252,15 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
252 if (isReadable(fallback)) return fallback;
253 return relativeLuminance(surfaceRgb) < 0.179 ? "#ffffff" : "#000000";
254 };
255 + // Status colors also fill controls such as destructive buttons, which carry
256 + // white text, so a dim one is lightened along its hue rather than replaced
257 + // by readableOn's black or white. One hidden in the canvas has no hue to
258 + // keep, so the derived color stands in.
259 + const readableStatusColor = (fallback: string, ...keys: ReadonlyArray<string>): string => {
260 + const specified = solidOver(canvas, ...keys);
261 + const base = specified && standsApart(specified, canvasHex) ? specified : fallback;
262 + return readableThemeColorOn(base, canvasHex, 4.5);
263 + };
264
265 const surfaceRaisedHex = raisedSurfaceCandidateHex ?? derived.surfaceRaised;
266 // The checked switch track maps to messageAction, so resolve it before
@@ -302,8 +312,8 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
312 solidOver(canvas, "panel.border", "editorGroup.border", "contrastBorder") ?? derived.border,
313 input: inputHex,
314 placeholder: readableOn(surfaceRaisedHex, derived.placeholder, "input.placeholderForeground"),
305 - error: readableOn(canvasHex, derived.error, "editorError.foreground", "errorForeground"),
306 - warning: readableOn(canvasHex, derived.warning, "editorWarning.foreground"),
315 + error: readableStatusColor(derived.error, "editorError.foreground", "errorForeground"),
316 + warning: readableStatusColor(derived.warning, "editorWarning.foreground"),
317 accentSurface:
318 solidOver(canvas, "list.activeSelectionBackground", "list.hoverBackground") ??
319 derived.accentSurface,