Source draws search matches and selection matches with the item selection fill, behind code. No rule checked that surface. In four core palettes, code colors were 3.8:1 to 4.3:1 on it, and dim text in a selected row was 4.0:1 in two light palettes. - The text rule also checks the item selection fill over bg-elevated. The syntax rule also checks the fill over bg (UI-046). - The derived fill is the accent at 12%, not 20%. RuneStone, Claude, and custom themes without their own fill use it. Sakura and the character themes set their own fill and pass. - Matches keep their underline, and selected rows keep their outline. pnpm check passes. pnpm test: 446 of 446 pass. Full smoke suite: 92 passed, 2 known. Three tests failed one time while Vite compiled the changed modules, and passed on retry and 3 of 3 times alone.
88 lines
4.8 KiB
TypeScript
88 lines
4.8 KiB
TypeScript
// 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.12),
|
|
'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;
|
|
}
|