// 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))), // Source draws search matches and selection matches with the item selection fill, behind code. match: colors => composite(colors['item-selection-bg'], opaque(colors.bg)), accent: colors => composite(colors.accent, opaque(colors.bg)), } satisfies Record 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', 'item-selection'], min: 4.5 }, { id: 'accent-text', kind: 'contrast', foregrounds: ['accent', 'accent-2', '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', 'match'], 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; 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; }