Files
RuneStone/tests/unit/theme/characterPalettes.spec.ts
T
Knotty Wyvern 5bd616d7fd Give themes named color roles, theme data files, and file format 3
Phase 5a-1 and 5a-2.

- roles.ts names 51 color roles. One derivation rule fills the roles
  that a theme does not give, for all themes. tokens.css keeps only the
  root defaults, without theme blocks.
- Each built-in theme is one data file in src/lib/theme/themes/. No code
  outside the theme data names a theme ID. characterThemes.ts is gone.
- New roles: link, focus, on-accent, highlight, scrim, the syntax roles,
  and 16 terminal (ANSI) roles. Stray colors in components use the roles.
- contrast.ts checks each theme against the readability rules of
  docs/ui-color.md. The tests mark the known palette failures as todo,
  and a second test fails when a known failure starts to pass.
- Theme files use schema 3 with a mode and an optional built-in base.
  A short file gets the missing colors and fonts from the base. Schema 1
  and 2 files still import. Old syntax names convert on import.
- Docs: ui-color.md, plugin-development.md (theme tokens), ui-design.md,
  guides/themes.md, and UI-041 and UI-042 in design-decisions.md.

The applied colors stay the same for every built-in theme, except three
small changes: the conflict line uses the warn color, the Write
highlight uses the highlight role, and two dialog backdrops use the
scrim role.

pnpm check passes. pnpm test: 447 pass, 31 todo, 0 fail. Full smoke
suite: 93 passed, 2 known. appearance/palettes read the old syntax
names and passed 3 of 3 times after the fix. workspace/manageTemplates
failed one time and passed on retry.
2026-10-28 15:50:45 -07:00

95 lines
4.9 KiB
TypeScript

// Character palettes: WCAG contrast of text and syntax colors on opaque and image-tinted surfaces, plus per-theme palette rules.
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { BUILTIN_THEMES } from '../../../src/lib/theme/themes/index.ts';
import { SYNTAX_ROLES, resolveColors } from '../../../src/lib/theme/roles.ts';
/** Every role of a dark character palette, with the derived roles. */
function characterTheme(theme: string) {
const palette = BUILTIN_THEMES.find(item => item.id === theme);
return palette && 'dark' in palette.variants ? resolveColors(palette.variants.dark, 'dark') : undefined;
}
const rgb = (color: string) => color.slice(1).match(/../g)!.map(value => parseInt(value, 16));
const mix = (foreground: string, background: string, opacity: number) => '#' + rgb(foreground)
.map((value, index) => Math.round(value * opacity + rgb(background)[index] * (1 - opacity)).toString(16).padStart(2, '0')).join('');
function luminance(color: string) {
return rgb(color).map(value => value / 255)
.map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4)
.reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index], 0);
}
function contrast(foreground: string, background: string) {
const [high, low] = [luminance(foreground), luminance(background)].sort((a, b) => b - a);
return (high + 0.05) / (low + 0.05);
}
/** Named foreground groups and surfaces of a palette. Image surfaces are tinted over a white image. */
function roles(theme: string) {
const palette = characterTheme(theme)!;
const { bg, 'bg-elevated': elevated, 'bg-active': active, 'text-dim': dim, text, 'text-strong': strong, accent, selection } = palette;
const editorOnWhite = mix(bg, '#ffffff', 0.9);
const panelOnWhite = mix(elevated, '#ffffff', 0.82);
const foregrounds = {
ui: { dim, text, strong },
accents: { accent, secondary: palette['accent-2'] },
syntax: Object.fromEntries(SYNTAX_ROLES.map(role => [`syntax-${role}`, palette[`syntax-${role}`]])),
};
const surfaces = {
bg, elevated, active, selection, editorOnWhite, panelOnWhite,
codeBlockOnWhite: mix(elevated, editorOnWhite, 0.82),
menuHoverOnWhite: mix(active, panelOnWhite, 0.82),
controlHoverOnWhite: mix(active, '#ffffff', 0.92),
};
return { foregrounds, surfaces };
}
type Roles = ReturnType<typeof roles>;
type Check = [foregrounds: (keyof Roles['foregrounds'])[], surfaces: (keyof Roles['surfaces'])[]];
// Every listed foreground needs 4.5:1 contrast on every listed surface.
const readable: { theme: string; name: string; checks: Check[] }[] = [
{ theme: 'harvest-moon', name: 'Harvest Moon', checks: [
[['ui', 'accents'], ['bg', 'elevated', 'active', 'selection']],
[['syntax'], ['bg', 'elevated', 'selection']],
] },
{ theme: 'crimson-pilot', name: 'Crimson Pilot', checks: [
[['ui', 'accents', 'syntax'], ['bg', 'elevated', 'active', 'selection']],
[['syntax'], ['editorOnWhite', 'codeBlockOnWhite']],
[['ui'], ['panelOnWhite']],
] },
{ theme: 'silver-serpent', name: 'Silver Serpent', checks: [
[['ui', 'accents', 'syntax'], ['bg', 'elevated', 'active', 'selection', 'panelOnWhite', 'editorOnWhite', 'menuHoverOnWhite', 'controlHoverOnWhite', 'codeBlockOnWhite']],
] },
];
for (const { theme, name, checks } of readable) {
test(`${name} text and syntax remain readable on opaque and image surfaces`, () => {
const { foregrounds, surfaces } = roles(theme);
for (const [groups, surfaceNames] of checks) {
for (const [role, foreground] of groups.flatMap(group => Object.entries(foregrounds[group]))) {
for (const surface of surfaceNames) {
assert(contrast(foreground, surfaces[surface]) >= 4.5, `${role} ${foreground} on ${surface} ${surfaces[surface]} needs 4.5:1 contrast`);
}
}
}
});
}
test('Harvest Moon uses the warm character colors for all palette roles', () => {
const palette = characterTheme('harvest-moon')!;
const identity = ['bg', 'bg-elevated', 'bg-active', 'border', 'text', 'text-dim', 'text-strong', 'accent', 'accent-2', 'selection', ...SYNTAX_ROLES.map(role => `syntax-${role}` as const)] as const;
for (const role of identity) {
const color = palette[role];
assert.match(color, /^#[0-9a-f]{6}$/i);
const [red, green, blue] = rgb(color);
assert(red >= green && green >= blue, `${role} ${color} must follow the copper, amber, red, or cream palette`);
}
assert.deepEqual(Object.keys(BUILTIN_THEMES.find(item => item.id === 'harvest-moon')!.variants), ['dark']);
});
test('Silver Serpent selection has a readable perimeter and a separate fill', () => {
const { surfaces: { bg, elevated, selection }, foregrounds: { accents: { accent } } } = roles('silver-serpent');
assert(contrast(accent, selection) >= 3, 'selection perimeter');
assert(contrast(selection, bg) >= 1.5, 'selection against the editor');
assert(contrast(selection, elevated) >= 1.5, 'selection against a panel');
});