Plum Night, River, Paper and Reed (designed by Amy, approved by Daniel) join the brand pair. theme.list/current/select/import, a picker with live preview (view.selectTheme) and "Import Theme…" (view.importTheme); imported themes are validated and stored in <data_dir>/themes. Editor font family and line height settings apply live. Docs: docs/themes.md. Gate: fmt, clippy and the rest pass up to cargo test, where only the unrelated lutra-terminal PTY test a_full_buffer_stops_the_shell timed out under load; to be re-run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
8.4 KiB
TypeScript
233 lines
8.4 KiB
TypeScript
/**
|
|
* WCAG 2.1 contrast check of the built-in themes
|
|
* (https://www.w3.org/TR/WCAG21/#contrast-minimum). Every token pair the
|
|
* UI draws text with reaches AA: 4.5:1 for body text, 3:1 for non-text
|
|
* marks (cursor, modified dot, settings switch knob; WCAG 1.4.11). Every
|
|
* syntax colour (`--lt-syntax-*`) reaches 4.5:1 on the editor background
|
|
* and on the active line. The terminal's text, cursor and every ANSI colour
|
|
* that shows as text reach AA on the terminal background (M3.6); the colour
|
|
* meant to vanish into it is exempt per theme (`TERMINAL_EXEMPT`). Every
|
|
* diagnostic severity (`--lt-diag-*`) and version-control status
|
|
* (`--lt-vcs-*`) colour reaches 4.5:1 on the editor background, the active
|
|
* line and the window background (and on the active tab, M4.4), and every completion kind icon colour
|
|
* reaches 3:1 on the popup and its selected row (H5). The
|
|
* themes come from
|
|
* `themes.json`, a snapshot that the Rust test `ui_snapshot_is_current`
|
|
* keeps equal to `lutra_engine::theme`.
|
|
*/
|
|
import { describe, expect, it } from "vitest";
|
|
import { KIND_COLOURS } from "../../src/components/editor/assist/icons";
|
|
import themes from "./themes.json";
|
|
|
|
/** Minimum ratio for normal-size text (AA). */
|
|
const TEXT = 4.5;
|
|
/** Minimum ratio for large text and non-text marks (AA). */
|
|
const LARGE = 3;
|
|
|
|
/**
|
|
* Foreground token, background token and minimum ratio, for every
|
|
* combination the components use (see `ui/src/**` `color`/`background`).
|
|
*/
|
|
const PAIRS: [string, string, number][] = [
|
|
["fg", "bg", TEXT],
|
|
["fg", "editor-bg", TEXT],
|
|
["fg", "tab-active-bg", TEXT],
|
|
["fg", "notice-bg", TEXT],
|
|
["fg", "editor-active-line-bg", TEXT],
|
|
["fg", "editor-selection-bg", TEXT],
|
|
["fg", "editor-match-bg", TEXT],
|
|
["fg-muted", "bg", TEXT],
|
|
["fg-muted", "editor-bg", TEXT],
|
|
["accent", "bg", TEXT],
|
|
["error", "bg", TEXT],
|
|
["error", "statusbar-bg", TEXT],
|
|
["statusbar-fg", "statusbar-bg", TEXT],
|
|
["editor-gutter-fg", "editor-gutter-bg", TEXT],
|
|
["editor-gutter-fg", "editor-active-line-bg", TEXT],
|
|
["editor-cursor", "editor-bg", LARGE],
|
|
["bg", "accent", LARGE],
|
|
["fg", "editor-bg", LARGE],
|
|
["editor-cursor", "editor-active-line-bg", LARGE],
|
|
["tab-modified", "bg", LARGE],
|
|
["tab-modified", "tab-active-bg", LARGE],
|
|
];
|
|
|
|
/** Syntax token names (`--lt-syntax-<name>`), as `SYNTAX_TOKENS` in `theme.rs`. */
|
|
export const SYNTAX = [
|
|
"keyword",
|
|
"string",
|
|
"number",
|
|
"comment",
|
|
"variable",
|
|
"function",
|
|
"type",
|
|
"constant",
|
|
"tag",
|
|
"attribute",
|
|
"operator",
|
|
] as const;
|
|
|
|
/** Every syntax token as text on the editor background and the active line. */
|
|
const SYNTAX_PAIRS: [string, string, number][] = SYNTAX.flatMap((name): [string, string, number][] => [
|
|
[`syntax-${name}`, "editor-bg", TEXT],
|
|
[`syntax-${name}`, "editor-active-line-bg", TEXT],
|
|
]);
|
|
|
|
/** Diagnostic severity names (`--lt-diag-<name>`), as `DIAGNOSTIC_TOKENS` in `theme.rs`. */
|
|
export const DIAGNOSTIC = ["error", "warning", "info", "hint"] as const;
|
|
|
|
/** Version-control status names (`--lt-vcs-<name>`), as `VCS_TOKENS` in `theme.rs`. */
|
|
export const VCS = ["added", "modified", "deleted", "untracked", "ignored", "conflicted"] as const;
|
|
|
|
/** Backgrounds the diagnostic and status colours are drawn on as text or marks. */
|
|
const STATUS_BACKGROUNDS = ["editor-bg", "editor-active-line-bg", "bg"] as const;
|
|
|
|
/** Every diagnostic and status colour on each of its backgrounds. */
|
|
const STATUS_PAIRS: [string, string, number][] = [
|
|
...DIAGNOSTIC.map((name) => `diag-${name}`),
|
|
...VCS.map((name) => `vcs-${name}`),
|
|
].flatMap((fg) => STATUS_BACKGROUNDS.map((bg): [string, string, number] => [fg, bg, TEXT]));
|
|
|
|
/** Status colours as tab names on the active tab (M4.4). */
|
|
const TAB_STATUS_PAIRS: [string, string, number][] = VCS.map((name): [string, string, number] => [
|
|
`vcs-${name}`,
|
|
"tab-active-bg",
|
|
TEXT,
|
|
]);
|
|
|
|
/** Every completion kind icon colour on the popup and its selected row. */
|
|
const KIND_PAIRS: [string, string, number][] = [...new Set(Object.values(KIND_COLOURS))].flatMap(
|
|
(fg): [string, string, number][] => [
|
|
[fg, "bg", LARGE],
|
|
[fg, "editor-selection-bg", LARGE],
|
|
],
|
|
);
|
|
|
|
/** ANSI colour names, as in `TERMINAL_TOKENS` in `theme.rs`. */
|
|
const ANSI = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] as const;
|
|
|
|
/**
|
|
* ANSI tokens that may blend into the background: black in dark themes,
|
|
* white and bright white in light ones.
|
|
*/
|
|
const TERMINAL_EXEMPT = {
|
|
dark: ["terminal-ansi-black"],
|
|
light: ["terminal-ansi-white", "terminal-ansi-bright-white"],
|
|
} as const;
|
|
|
|
/**
|
|
* The terminal's pairs for one theme: text and selection, the cursor, and
|
|
* every ANSI colour (normal and bright) that is not exempt.
|
|
*
|
|
* @param dark - whether the theme is dark.
|
|
* @returns foreground token, background token and minimum ratio.
|
|
*/
|
|
function terminalPairs(dark: boolean): [string, string, number][] {
|
|
const exempt: readonly string[] = TERMINAL_EXEMPT[dark ? "dark" : "light"];
|
|
const ansi = ANSI.flatMap((name) => [`terminal-ansi-${name}`, `terminal-ansi-bright-${name}`]).filter(
|
|
(name) => !exempt.includes(name),
|
|
);
|
|
return [
|
|
["terminal-fg", "terminal-bg", TEXT],
|
|
["terminal-fg", "terminal-selection-bg", TEXT],
|
|
["terminal-cursor", "terminal-bg", LARGE],
|
|
...ansi.map((name): [string, string, number] => [name, "terminal-bg", TEXT]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Relative luminance of a `#rrggbb` colour (WCAG 2.1 definition).
|
|
*
|
|
* @param hex - the colour.
|
|
* @returns the luminance, 0 (black) to 1 (white).
|
|
*/
|
|
export function luminance(hex: string): number {
|
|
const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex);
|
|
if (!match) {
|
|
throw new Error(`not a #rrggbb colour: ${hex}`);
|
|
}
|
|
const [r, g, b] = match.slice(1).map((part) => {
|
|
const c = parseInt(part, 16) / 255;
|
|
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
}) as [number, number, number];
|
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
}
|
|
|
|
/**
|
|
* Contrast ratio of two colours.
|
|
*
|
|
* @param a - one `#rrggbb` colour.
|
|
* @param b - the other.
|
|
* @returns the ratio, 1 to 21.
|
|
*/
|
|
export function contrast(a: string, b: string): number {
|
|
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number];
|
|
return (hi + 0.05) / (lo + 0.05);
|
|
}
|
|
|
|
describe("built-in themes", () => {
|
|
it("are the brand pair and the four C9 themes, two of each kind added", () => {
|
|
expect(themes.map((theme) => theme.id)).toEqual([
|
|
"lutra-dark",
|
|
"lutra-light",
|
|
"lutra-plum-night",
|
|
"lutra-river",
|
|
"lutra-paper",
|
|
"lutra-reed",
|
|
]);
|
|
expect(themes.filter((theme) => theme.dark)).toHaveLength(3);
|
|
});
|
|
|
|
it("all define the same tokens", () => {
|
|
const names = (tokens: Record<string, string>): string[] => Object.keys(tokens).sort();
|
|
for (const theme of themes) {
|
|
expect(names(theme.tokens)).toEqual(names(themes[0]?.tokens ?? {}));
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("contrast helper", () => {
|
|
it("matches the WCAG extremes", () => {
|
|
expect(contrast("#000000", "#ffffff")).toBeCloseTo(21, 5);
|
|
expect(contrast("#777777", "#777777")).toBe(1);
|
|
expect(contrast("#767676", "#ffffff")).toBeGreaterThanOrEqual(4.5);
|
|
});
|
|
});
|
|
|
|
describe.each(themes)("theme $id", (theme) => {
|
|
const tokens: Record<string, string> = theme.tokens;
|
|
const token = (name: string): string => {
|
|
const value = tokens[`--lt-${name}`];
|
|
if (value === undefined) {
|
|
throw new Error(`${theme.id} has no --lt-${name}`);
|
|
}
|
|
return value;
|
|
};
|
|
|
|
it("checks every syntax token it defines", () => {
|
|
const defined = Object.keys(tokens)
|
|
.filter((name) => name.startsWith("--lt-syntax-"))
|
|
.map((name) => name.slice("--lt-syntax-".length))
|
|
.sort();
|
|
expect(defined).toEqual([...SYNTAX].sort());
|
|
});
|
|
|
|
it("checks every diagnostic and status token it defines", () => {
|
|
const defined = (prefix: string): string[] =>
|
|
Object.keys(tokens)
|
|
.filter((name) => name.startsWith(prefix))
|
|
.map((name) => name.slice(prefix.length))
|
|
.sort();
|
|
expect(defined("--lt-diag-")).toEqual([...DIAGNOSTIC].sort());
|
|
expect(defined("--lt-vcs-")).toEqual([...VCS].sort());
|
|
});
|
|
|
|
it("checks every terminal ANSI token it defines", () => {
|
|
const defined = Object.keys(tokens).filter((name) => name.startsWith("--lt-terminal-ansi-"));
|
|
expect(defined).toHaveLength(ANSI.length * 2);
|
|
});
|
|
|
|
it.each([...PAIRS, ...SYNTAX_PAIRS, ...STATUS_PAIRS, ...TAB_STATUS_PAIRS, ...KIND_PAIRS, ...terminalPairs(theme.dark)])("%s on %s reaches %d:1", (fg, bg, min) => {
|
|
expect(contrast(token(fg), token(bg))).toBeGreaterThanOrEqual(min);
|
|
});
|
|
});
|