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.
This commit is contained in:
Knotty Wyvern
2026-10-28 15:50:45 -07:00
parent af68d92969
commit 5bd616d7fd
55 changed files with 928 additions and 355 deletions
+88
View File
@@ -0,0 +1,88 @@
// Readability rules for theme roles. The unit tests and the theme editor use the same rules.
// docs/ui-color.md lists the thresholds.
import { composite, contrast, distance, formatHex, luminance, mix, parseHex } from './color.ts';
import { ANSI_COLORS, SYNTAX_ROLES, type Mode, type ResolvedColors, type Role } from './roles.ts';
/** Text selection keeps at most this alpha, so the background shows through (UI-029). */
export const SELECTION_ALPHA = 166 / 255;
/** The colors that the app applies. Text selection is at most 65% opaque. */
export function appliedColors(colors: ResolvedColors): ResolvedColors {
const [red, green, blue, alpha] = parseHex(colors.selection);
return { ...colors, selection: formatHex([red, green, blue, Math.min(SELECTION_ALPHA, alpha)]) };
}
/** Opaque surfaces as the app draws them. Each takes the applied colors of a theme. */
export const SURFACES = {
bg: colors => opaque(colors.bg),
'bg-elevated': colors => composite(colors['bg-elevated'], opaque(colors.bg)),
'bg-active': colors => composite(colors['bg-active'], opaque(colors.bg)),
selection: colors => composite(colors.selection, opaque(colors.bg)),
'line-highlight': colors => composite(colors['line-highlight'], opaque(colors.bg)),
highlight: colors => composite(mix(colors.highlight, '#0000', 0.3), opaque(colors.bg)),
'item-selection': colors => composite(colors['item-selection-bg'], composite(colors['bg-elevated'], opaque(colors.bg))),
accent: colors => composite(colors.accent, opaque(colors.bg)),
} satisfies Record<string, (colors: ResolvedColors) => string>;
export type Surface = keyof typeof SURFACES;
function opaque(color: string) { return composite(color, '#000000'); }
const SYNTAX = SYNTAX_ROLES.map(role => `syntax-${role}` as const);
// Black and white are exempt: one of them always matches the terminal background.
const ANSI = ANSI_COLORS.filter(color => color !== 'black' && color !== 'white').flatMap(color => [`ansi-${color}`, `ansi-bright-${color}`] as const);
export type ContrastRule = { id: string; kind: 'contrast'; foregrounds: readonly Role[]; surfaces: readonly Surface[]; min: number };
export type DistinctRule = { id: string; kind: 'distinct'; pairs: readonly (readonly [Role, Role])[]; min: number };
export type VarietyRule = { id: string; kind: 'variety'; roles: readonly Role[]; min: number };
export type LightestRule = { id: string; kind: 'lightest'; mode: Mode; role: Role; others: readonly Role[] };
export type Rule = ContrastRule | DistinctRule | VarietyRule | LightestRule;
/** OKLab distance that keeps two status or identity colors apart. */
export const DISTINCT_DISTANCE = 0.1;
export const RULES: readonly Rule[] = [
{ id: 'text', kind: 'contrast', foregrounds: ['text', 'text-dim', 'text-strong'], surfaces: ['bg', 'bg-elevated', 'bg-active', 'selection'], min: 4.5 },
{ id: 'accent-text', kind: 'contrast', foregrounds: ['accent', 'link'], surfaces: ['bg', 'bg-elevated', 'bg-active'], min: 4.5 },
{ id: 'status-text', kind: 'contrast', foregrounds: ['error', 'warn', 'ok'], surfaces: ['bg', 'bg-elevated'], min: 4.5 },
{ id: 'syntax', kind: 'contrast', foregrounds: SYNTAX, surfaces: ['bg', 'bg-active', 'line-highlight'], min: 4.5 },
{ id: 'ansi', kind: 'contrast', foregrounds: ANSI, surfaces: ['bg'], min: 4.5 },
{ id: 'focus', kind: 'contrast', foregrounds: ['focus'], surfaces: ['bg', 'bg-elevated', 'bg-active'], min: 3 },
{ id: 'on-accent', kind: 'contrast', foregrounds: ['on-accent'], surfaces: ['accent'], min: 4.5 },
{ id: 'highlight', kind: 'contrast', foregrounds: ['text'], surfaces: ['highlight'], min: 4.5 },
{ id: 'selection-outline', kind: 'contrast', foregrounds: ['accent'], surfaces: ['item-selection'], min: 3 },
{ id: 'status-distinct', kind: 'distinct', pairs: [['accent', 'error'], ['accent', 'warn'], ['accent', 'ok'], ['error', 'warn'], ['warn', 'ok'], ['error', 'ok']], min: DISTINCT_DISTANCE },
{ id: 'syntax-variety', kind: 'variety', roles: SYNTAX, min: 7 },
{ id: 'light-page', kind: 'lightest', mode: 'light', role: 'bg', others: ['bg-elevated', 'bg-active'] },
];
export type Finding = { rule: string; subject: string; value: number; min: number; pass: boolean };
/** Every check of every rule for one theme. `colors` are the resolved colors, before the selection cap. */
export function checkTheme(resolved: ResolvedColors, mode: Mode, rules: readonly Rule[] = RULES): Finding[] {
const colors = appliedColors(resolved);
const findings: Finding[] = [];
const surfaceColors = Object.fromEntries(Object.entries(SURFACES).map(([name, color]) => [name, color(colors)])) as Record<Surface, string>;
for (const rule of rules) {
if (rule.kind === 'contrast') {
for (const foreground of rule.foregrounds) for (const surface of rule.surfaces) {
const value = contrast(composite(colors[foreground], surfaceColors[surface]), surfaceColors[surface]);
findings.push({ rule: rule.id, subject: `${foreground} on ${surface}`, value, min: rule.min, pass: value >= rule.min });
}
} else if (rule.kind === 'distinct') {
for (const [first, second] of rule.pairs) {
const value = distance(composite(colors[first], surfaceColors.bg), composite(colors[second], surfaceColors.bg));
findings.push({ rule: rule.id, subject: `${first} and ${second}`, value, min: rule.min, pass: value >= rule.min });
}
} else if (rule.kind === 'variety') {
const value = new Set(rule.roles.map(role => colors[role].toLowerCase())).size;
findings.push({ rule: rule.id, subject: 'distinct colors', value, min: rule.min, pass: value >= rule.min });
} else if (rule.mode === mode) {
const page = luminance(surfaceColors.bg);
for (const other of rule.others) {
const value = page - luminance(composite(colors[other], surfaceColors.bg));
findings.push({ rule: rule.id, subject: `${rule.role} lighter than ${other}`, value, min: 0, pass: value > 0 });
}
}
}
return findings;
}