|
1 | 1 | import { describe, expect, it } from "vite-plus/test"; |
2 | 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"; |
4 | 9 | import { |
5 | 10 | isVsCodeThemeFile, |
6 | 11 | pairVsCodeThemes, |
@@ -30,6 +35,11 @@ function contrastRatio(first: string, second: string): number { |
30 | 35 | return (lighter + 0.05) / (darker + 0.05); |
31 | 36 | } |
32 | 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 | + |
33 | 43 | const VSCODE_DARK = { |
34 | 44 | name: "pierre-dark-soft", |
35 | 45 | type: "dark", |
@@ -293,6 +303,45 @@ describe("VS Code theme import", () => { |
293 | 303 | expect(hc.appearance).toBe("light"); |
294 | 304 | }); |
295 | 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 | + |
296 | 345 | it("stays readable when the file replaces a surface but not its foreground", () => { |
297 | 346 | const theme = parseVsCodeThemeFile({ |
298 | 347 | name: "Split", |
|
0 commit comments