Skip to content

Commit 7845898

Browse files
authored
fix(web): imported VS Code themes keep destructive buttons red (#21924)
1 parent 1a44c40 commit 7845898

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎apps/web/src/themePalette.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,25 @@ function solveOklchLightness(
558558
return { ...base, L: direction === "lighter" ? high : low };
559559
}
560560

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+
561580
const STANDARD_STATUS_COLORS = {
562581
light: {
563582
error: "#fb2c36",

‎apps/web/src/vscodeThemeImport.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { getThemeColorsForMode, themeColorToHex, THEME_FILE_VERSION } from "./themePalette";
3+
import {
4+
getThemeColorsForMode,
5+
themeColorToHex,
6+
THEME_FILE_VERSION,
7+
toCanonicalThemeColor,
8+
} from "./themePalette";
49
import {
510
isVsCodeThemeFile,
611
pairVsCodeThemes,
@@ -30,6 +35,11 @@ function contrastRatio(first: string, second: string): number {
3035
return (lighter + 0.05) / (darker + 0.05);
3136
}
3237

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+
3343
const VSCODE_DARK = {
3444
name: "pierre-dark-soft",
3545
type: "dark",
@@ -293,6 +303,45 @@ describe("VS Code theme import", () => {
293303
expect(hc.appearance).toBe("light");
294304
});
295305

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+
296345
it("stays readable when the file replaces a surface but not its foreground", () => {
297346
const theme = parseVsCodeThemeFile({
298347
name: "Split",

‎apps/web/src/vscodeThemeImport.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getThemeModes,
55
isReservedThemeId,
66
parseThemeFile,
7+
readableThemeColorOn,
78
themeColorToHex,
89
themeIdFromName,
910
THEME_FILE_VERSION,
@@ -251,6 +252,15 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
251252
if (isReadable(fallback)) return fallback;
252253
return relativeLuminance(surfaceRgb) < 0.179 ? "#ffffff" : "#000000";
253254
};
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+
};
254264

255265
const surfaceRaisedHex = raisedSurfaceCandidateHex ?? derived.surfaceRaised;
256266
// The checked switch track maps to messageAction, so resolve it before
@@ -302,8 +312,8 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
302312
solidOver(canvas, "panel.border", "editorGroup.border", "contrastBorder") ?? derived.border,
303313
input: inputHex,
304314
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"),
307317
accentSurface:
308318
solidOver(canvas, "list.activeSelectionBackground", "list.hoverBackground") ??
309319
derived.accentSurface,

0 commit comments

Comments
 (0)