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
+87
View File
@@ -0,0 +1,87 @@
// Theme roles: the named colors of a theme. Each role is the CSS token `--rs-<role>`.
// A theme gives the base roles. The other roles come from the base roles through one rule for all themes.
// docs/ui-color.md describes each role and its contrast requirement.
import { mix, readableOn } from './color.ts';
export type Mode = 'light' | 'dark';
/** Code colors in Source. src/lib/editor/sourceView.ts maps the syntax tags to these roles. */
export const SYNTAX_ROLES = ['keyword', 'string', 'number', 'comment', 'type', 'function', 'property', 'variable', 'constant', 'punctuation', 'heading'] as const;
/** The eight terminal colors. Each has a bright variant, for example `ansi-bright-red`. */
export const ANSI_COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] as const;
/** Every role, in the order of theme files and of resolution. A derived role comes after the roles that it uses. */
export const ROLES = [
'bg', 'bg-elevated', 'bg-active', 'border', 'text', 'text-dim', 'text-strong',
'accent', 'accent-2', 'accent-dim', 'accent-deep', 'link', 'focus', 'on-accent',
'selection', 'item-selection-bg', 'line-highlight', 'highlight', 'scrim',
'error', 'warn', 'warn-hot', 'ok', 'special',
'syntax-keyword', 'syntax-string', 'syntax-number', 'syntax-comment', 'syntax-type', 'syntax-function',
'syntax-property', 'syntax-variable', 'syntax-constant', 'syntax-punctuation', 'syntax-heading',
'ansi-black', 'ansi-red', 'ansi-green', 'ansi-yellow', 'ansi-blue', 'ansi-magenta', 'ansi-cyan', 'ansi-white',
'ansi-bright-black', 'ansi-bright-red', 'ansi-bright-green', 'ansi-bright-yellow',
'ansi-bright-blue', 'ansi-bright-magenta', 'ansi-bright-cyan', 'ansi-bright-white',
] as const;
export type Role = typeof ROLES[number];
export type ThemeColors = Partial<Record<Role, string>>;
export type ResolvedColors = Record<Role, string>;
/** The roles that every built-in theme gives. Other themes get the missing ones from their base theme. */
export const BASE_ROLES = ['bg', 'bg-elevated', 'bg-active', 'border', 'text', 'text-dim', 'text-strong', 'accent', 'selection', 'error', 'warn', 'warn-hot', 'ok', 'special'] as const satisfies readonly Role[];
type Derive = (colors: ResolvedColors, mode: Mode) => string;
const bright = (role: Role): Derive => colors => colors[role];
// Terminal red, green, and yellow follow the status colors. Blue, magenta, and cyan have their own defaults,
// because the accent and special colors of a theme can have any hue.
const DERIVED: Record<Exclude<Role, typeof BASE_ROLES[number]>, Derive> = {
'accent-2': colors => colors.accent,
'accent-dim': colors => mix(colors.accent, colors.bg, 0.8),
'accent-deep': colors => mix(colors.accent, colors.bg, 0.6),
link: colors => colors.accent,
focus: colors => colors.accent,
'on-accent': colors => readableOn(colors.accent),
'item-selection-bg': colors => mix(colors.accent, '#0000', 0.2),
'line-highlight': (_, mode) => mode === 'light' ? '#0000000d' : '#ffffff0d',
highlight: () => '#edc65b',
scrim: () => '#00000088',
'syntax-keyword': colors => colors.accent,
'syntax-string': colors => colors.ok,
'syntax-number': colors => colors.special,
'syntax-comment': colors => colors['text-dim'],
'syntax-type': colors => colors['accent-2'],
'syntax-function': colors => colors['accent-2'],
'syntax-property': colors => colors['accent-2'],
'syntax-variable': colors => colors.text,
'syntax-constant': colors => colors['text-dim'],
'syntax-punctuation': colors => colors['text-dim'],
'syntax-heading': colors => colors['syntax-keyword'],
'ansi-black': colors => colors['text-dim'],
'ansi-red': colors => colors.error,
'ansi-green': colors => colors.ok,
'ansi-yellow': colors => colors.warn,
'ansi-blue': (_, mode) => mode === 'light' ? '#0451a5' : '#3b8eea',
'ansi-magenta': (_, mode) => mode === 'light' ? '#a626a4' : '#d670d6',
'ansi-cyan': (_, mode) => mode === 'light' ? '#05748a' : '#29b8db',
'ansi-white': colors => colors.text,
'ansi-bright-black': colors => colors['text-dim'],
'ansi-bright-red': bright('ansi-red'),
'ansi-bright-green': bright('ansi-green'),
'ansi-bright-yellow': bright('ansi-yellow'),
'ansi-bright-blue': bright('ansi-blue'),
'ansi-bright-magenta': bright('ansi-magenta'),
'ansi-bright-cyan': bright('ansi-cyan'),
'ansi-bright-white': colors => colors['text-strong'],
};
/** Every role of a theme. `colors` wins over `base`, and the derivation rules fill the rest. */
export function resolveColors(colors: ThemeColors, mode: Mode, base: ThemeColors = {}): ResolvedColors {
const result = { ...base, ...colors } as ResolvedColors;
for (const role of ROLES) {
if (result[role]) continue;
const derive = DERIVED[role as keyof typeof DERIVED];
if (!derive) throw new Error(`The theme has no ${role} color.`);
result[role] = derive(result, mode);
}
return result;
}