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:
@@ -1,7 +1,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { parseCustomTheme } from '../../../src/lib/theme/customThemes.ts';
|
||||
import { CHARACTER_THEMES, SYNTAX_ROLES } from '../../../src/lib/theme/characterThemes.ts';
|
||||
import { SYNTAX_ROLES } from '../../../src/lib/theme/roles.ts';
|
||||
import { parseAppearance, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, THEMES, themeModes } from '../../../src/lib/theme/appearance.ts';
|
||||
|
||||
test('Removed palettes preserve the saved mode with the default palette', () => {
|
||||
@@ -33,37 +33,17 @@ test('Removed character themes retain the saved background', () => {
|
||||
}
|
||||
});
|
||||
|
||||
function luminance(color: string) {
|
||||
const rgb = color.slice(1).match(/../g)!.map(value => parseInt(value, 16) / 255)
|
||||
.map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4);
|
||||
return rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722;
|
||||
}
|
||||
function contrast(foreground: string, background: string) {
|
||||
const values = [luminance(foreground), luminance(background)].sort((a, b) => b - a);
|
||||
return (values[0] + 0.05) / (values[1] + 0.05);
|
||||
}
|
||||
test('Character palettes restore one appearance and export readable surfaces and syntax', () => {
|
||||
for (const theme of Object.keys(CHARACTER_THEMES) as (keyof typeof CHARACTER_THEMES)[]) {
|
||||
const exported = exportAppearanceTheme(theme);
|
||||
// tests/unit/theme/contrast.spec.ts checks the readability of every palette.
|
||||
test('Single-mode palettes restore their one appearance and export a valid theme', () => {
|
||||
for (const { id } of BUILTIN_THEMES.filter(item => themeModes(item.id).length === 1)) {
|
||||
const [only] = themeModes(id);
|
||||
const exported = exportAppearanceTheme(id);
|
||||
for (const mode of ['light', 'dark'] as const) {
|
||||
assert.deepEqual(parseAppearance(JSON.stringify({ theme, mode })), { theme, mode: CHARACTER_THEMES[theme].mode });
|
||||
const colors = exported.colors;
|
||||
for (const foreground of ['text', 'text-dim', 'text-strong', 'accent'] as const) {
|
||||
for (const background of ['bg', 'bg-elevated', 'bg-active', 'selection'] as const) {
|
||||
if (foreground === 'accent' && background === 'selection') continue;
|
||||
assert(contrast(colors[foreground]!, colors[background]!) >= 4.5, `${theme} ${mode}: ${foreground} on ${background}`);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(parseAppearance(JSON.stringify({ theme: id, mode })), { theme: id, mode: only });
|
||||
}
|
||||
assert.deepEqual(themeModes(theme), [CHARACTER_THEMES[theme].mode]);
|
||||
assert.equal(exported.schema, 2);
|
||||
assert.equal(exported.schema, 3);
|
||||
assert.deepEqual(parseCustomTheme(exported), exported);
|
||||
for (const role of SYNTAX_ROLES) {
|
||||
for (const background of ['bg', 'bg-active'] as const) {
|
||||
assert(contrast(exported.colors[`syntax-${role}`]!, exported.colors[background]!) >= 4.5, `${theme}: ${role} on ${background}`);
|
||||
}
|
||||
}
|
||||
assert(new Set(SYNTAX_ROLES.map(role => exported.colors[`syntax-${role}`])).size >= 7);
|
||||
for (const role of SYNTAX_ROLES) assert.match(exported.colors[`syntax-${role}`] ?? '', /^#[0-9a-f]{6}$/);
|
||||
assert.notDeepEqual(exported.colors, exportAppearanceTheme('default').colors);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
// 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 { characterTheme, characterSyntax, SYNTAX_ROLES } from '../../../src/lib/theme/characterThemes.ts';
|
||||
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)
|
||||
@@ -19,13 +26,13 @@ function contrast(foreground: string, background: string) {
|
||||
/** 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, elevated, active, , dim, text, strong, accent, selection] = palette.surfaces;
|
||||
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.secondary },
|
||||
syntax: Object.fromEntries(SYNTAX_ROLES.map((role, index) => [`syntax-${role}`, palette.syntax[index]])),
|
||||
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,
|
||||
@@ -69,14 +76,14 @@ for (const { theme, name, checks } of readable) {
|
||||
|
||||
test('Harvest Moon uses the warm character colors for all palette roles', () => {
|
||||
const palette = characterTheme('harvest-moon')!;
|
||||
for (const color of [...palette.surfaces, palette.secondary, ...palette.syntax]) {
|
||||
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, `${color} must follow the copper, amber, red, or cream palette`);
|
||||
assert(red >= green && green >= blue, `${role} ${color} must follow the copper, amber, red, or cream palette`);
|
||||
}
|
||||
assert.equal(palette.mode, 'dark');
|
||||
assert.equal(Object.keys(characterSyntax('harvest-moon')).length, SYNTAX_ROLES.length);
|
||||
assert.deepEqual(characterSyntax('unknown'), {});
|
||||
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', () => {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
// Readability rules for every built-in theme (src/lib/theme/contrast.ts), and the rule that only theme data names a theme.
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import { join, relative } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { BUILTIN_THEMES } from '../../../src/lib/theme/themes/index.ts';
|
||||
import { BASE_ROLES, resolveColors, type Mode } from '../../../src/lib/theme/roles.ts';
|
||||
import { RULES, checkTheme } from '../../../src/lib/theme/contrast.ts';
|
||||
import { composite, contrast, distance, formatHex, mix, parseHex } from '../../../src/lib/theme/color.ts';
|
||||
|
||||
// Rules that a palette fails today. Stages 5a-3 and 5a-4 redesign these palettes and remove each entry.
|
||||
const KNOWN_FAILURES: Record<string, readonly string[]> = {
|
||||
'default/dark': ['text', 'syntax-variety'],
|
||||
'default/light': ['text', 'syntax', 'syntax-variety', 'light-page'],
|
||||
'claude/dark': ['accent-text', 'status-text', 'syntax', 'status-distinct', 'syntax-variety'],
|
||||
'claude/light': ['accent-text', 'status-text', 'syntax', 'ansi', 'focus', 'selection-outline', 'syntax-variety', 'light-page'],
|
||||
'sakura/light': ['text', 'accent-text', 'status-text', 'syntax', 'ansi', 'syntax-variety', 'light-page'],
|
||||
'harvest-moon/dark': ['status-text', 'status-distinct'],
|
||||
'crimson-pilot/dark': ['status-distinct'],
|
||||
'silver-serpent/dark': ['status-text', 'status-distinct'],
|
||||
};
|
||||
|
||||
const variants = BUILTIN_THEMES.flatMap(theme => Object.entries(theme.variants).map(([mode, colors]) => ({ key: `${theme.id}/${mode}`, mode: mode as Mode, colors: colors! })));
|
||||
|
||||
for (const variant of variants) {
|
||||
const findings = checkTheme(resolveColors(variant.colors, variant.mode), variant.mode);
|
||||
for (const rule of RULES) {
|
||||
const results = findings.filter(finding => finding.rule === rule.id);
|
||||
if (!results.length) continue;
|
||||
const known = KNOWN_FAILURES[variant.key]?.includes(rule.id);
|
||||
const failures = results.filter(finding => !finding.pass).map(finding => `${finding.subject}: ${finding.value.toFixed(2)} (minimum ${finding.min})`);
|
||||
test(`${variant.key} passes the ${rule.id} rule`, known ? { todo: 'Known palette failure. Stages 5a-3 and 5a-4 redesign this palette.' } : {}, () => {
|
||||
assert.deepEqual(failures, []);
|
||||
});
|
||||
if (known) test(`${variant.key} still fails the known ${rule.id} rule`, () => {
|
||||
assert(failures.length > 0, `Remove ${rule.id} from KNOWN_FAILURES['${variant.key}'].`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test('Each known failure names a built-in variant and a rule', () => {
|
||||
for (const [key, rules] of Object.entries(KNOWN_FAILURES)) {
|
||||
assert(variants.some(variant => variant.key === key), key);
|
||||
for (const rule of rules) assert(RULES.some(item => item.id === rule), `${key}: ${rule}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('Every built-in variant gives all base roles as hex colors', () => {
|
||||
for (const variant of variants) {
|
||||
for (const role of BASE_ROLES) assert.match(variant.colors[role] ?? '', /^#[0-9a-f]{6}$/, `${variant.key}: ${role}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('Color arithmetic matches CSS and WCAG', () => {
|
||||
assert.deepEqual(parseHex('#0f08'), [0, 255, 0, 136 / 255]);
|
||||
assert.equal(formatHex([255, 0, 0, 1]), '#ff0000');
|
||||
assert.equal(mix('#82aaff', '#141414', 0.8), '#6c8cd0');
|
||||
assert.equal(mix('#82aaff', '#0000', 0.2), '#82aaff33');
|
||||
assert.equal(composite('#ffffff80', '#000000'), '#808080');
|
||||
assert.equal(contrast('#000000', '#ffffff').toFixed(1), '21.0');
|
||||
assert(distance('#ff0000', '#ff0000') === 0 && distance('#000000', '#ffffff') > 0.99);
|
||||
});
|
||||
|
||||
test('Only theme data names a theme ID', () => {
|
||||
const root = fileURLToPath(new URL('../../../src', import.meta.url));
|
||||
const ids = BUILTIN_THEMES.flatMap(theme => [theme.id, ...('legacyIds' in theme ? theme.legacyIds : [])]).filter(id => id !== 'default');
|
||||
const pattern = new RegExp(`(['"\`])(?:${ids.join('|')})\\1|data-palette=`);
|
||||
const files = (function walk(folder: string): string[] {
|
||||
return readdirSync(folder).flatMap(name => {
|
||||
const path = join(folder, name);
|
||||
return statSync(path).isDirectory() ? walk(path) : /\.(ts|svelte|css)$/.test(name) ? [path] : [];
|
||||
});
|
||||
})(root);
|
||||
const found = files.filter(file => !relative(root, file).startsWith(join('lib', 'theme', 'themes')) && !file.endsWith('messages.ts'))
|
||||
.filter(file => pattern.test(readFileSync(file, 'utf8')))
|
||||
.map(file => relative(root, file));
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
@@ -1,11 +1,44 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { COLOR_TOKENS, parseCustomTheme } from '../../../src/lib/theme/customThemes.ts';
|
||||
import { applyTransparency, normalizeAppearance, exportAppearanceTheme, BUILTIN_THEMES, themeModes } from '../../../src/lib/theme/appearance.ts';
|
||||
import { parseCustomTheme } from '../../../src/lib/theme/customThemes.ts';
|
||||
import { ROLES } from '../../../src/lib/theme/roles.ts';
|
||||
import { BUILTIN_THEMES as THEME_DATA } from '../../../src/lib/theme/themes/index.ts';
|
||||
import { applyTransparency, normalizeAppearance, exportAppearanceTheme, resolveTheme, BUILTIN_THEMES, themeModes } from '../../../src/lib/theme/appearance.ts';
|
||||
const palette = { bg: '#123', text: '#abcdef', accent: '#AABBCC' };
|
||||
const theme = { schema: 2, id: 'my-theme', name: 'My theme', colors: palette };
|
||||
test('Fixed theme palettes retain all supplied colors', () => {
|
||||
assert.deepEqual(parseCustomTheme(theme), theme);
|
||||
test('Schema 2 palettes convert to schema 3 and retain all supplied colors', () => {
|
||||
assert.deepEqual(parseCustomTheme(theme), { ...theme, schema: 3, mode: 'dark' });
|
||||
assert.equal(parseCustomTheme({ ...theme, colors: { ...palette, bg: '#fafafa' } }).mode, 'light');
|
||||
});
|
||||
test('Old syntax names convert to the schema 3 roles', () => {
|
||||
const colors = { ...palette, 'syntax-local': '#111111', 'syntax-instance': '#222222', 'syntax-global': '#333333', 'syntax-parameter': '#444444', 'syntax-static': '#555555', 'syntax-keyword': '#666666' };
|
||||
assert.deepEqual(parseCustomTheme({ ...theme, colors }).colors, {
|
||||
...palette, 'syntax-variable': '#111111', 'syntax-property': '#222222', 'syntax-constant': '#333333', 'syntax-punctuation': '#333333', 'syntax-keyword': '#666666',
|
||||
});
|
||||
assert.throws(() => parseCustomTheme({ ...theme, colors: { ...palette, 'syntax-variable': '#111111' } }), /Unknown theme color/);
|
||||
});
|
||||
test('Schema 3 files name their mode and can take missing colors from a built-in base', () => {
|
||||
const file = { schema: 3, id: 'amber', name: 'Amber', mode: 'dark', base: 'holo', colors: { accent: '#00ff00', 'ansi-blue': '#0000ff' } };
|
||||
assert.deepEqual(parseCustomTheme(file), { ...file, base: 'harvest-moon' });
|
||||
assert.deepEqual(parseCustomTheme({ ...file, base: undefined, colors: {} }), { schema: 3, id: 'amber', name: 'Amber', mode: 'dark', colors: {} });
|
||||
for (const invalid of [{ mode: 'dim' }, { mode: undefined }, { base: 'sakura' }, { base: 'unknown' }, { base: 4 }, { colors: [] }, { colors: { 'accent-x': '#000' } }, { colors: { accent: 'red' } }]) {
|
||||
assert.throws(() => parseCustomTheme({ ...file, ...invalid }), JSON.stringify(invalid));
|
||||
}
|
||||
const previous = globalThis.localStorage;
|
||||
try {
|
||||
const stored = new Map([['runestone.custom-themes', JSON.stringify([file])]]);
|
||||
globalThis.localStorage = { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => stored.set(key, value) } as unknown as Storage;
|
||||
const resolved = resolveTheme('custom:amber', 'light');
|
||||
const harvest = THEME_DATA.find(item => item.id === 'harvest-moon')!;
|
||||
assert.equal(resolved.mode, 'dark');
|
||||
assert.equal(resolved.colors.bg, harvest.variants.dark.bg);
|
||||
assert.equal(resolved.colors.accent, '#00ff00');
|
||||
assert.equal(resolved.colors['syntax-keyword'], harvest.variants.dark['syntax-keyword']);
|
||||
assert.equal(resolved.colors.link, '#00ff00', 'A derived role follows the colors of the file');
|
||||
assert.equal(resolved.colors['ansi-blue'], '#0000ff');
|
||||
assert.match(resolved.fonts.ui, /Candara/);
|
||||
assert.deepEqual(themeModes('custom:amber'), ['dark']);
|
||||
} finally { globalThis.localStorage = previous; }
|
||||
});
|
||||
test('Legacy import selects the current appearance and validates both palettes', () => {
|
||||
const light = { ...palette, bg: '#fff' };
|
||||
@@ -20,10 +53,11 @@ test('Theme files reject CSS injection, unknown tokens, and incomplete palettes'
|
||||
}
|
||||
assert.throws(() => parseCustomTheme({ ...theme, id: '../unsafe' }));
|
||||
});
|
||||
test('Every built-in template contains every current color token and one palette', () => {
|
||||
test('Every built-in template contains every role and one palette', () => {
|
||||
for (const item of BUILTIN_THEMES) for (const mode of themeModes(item.id)) {
|
||||
const exported = exportAppearanceTheme(item.id, mode);
|
||||
assert.deepEqual(Object.keys(exported.colors).sort(), [...COLOR_TOKENS].sort());
|
||||
assert.deepEqual(Object.keys(exported.colors), [...ROLES]);
|
||||
assert.equal(exported.mode, mode);
|
||||
assert.deepEqual(parseCustomTheme(exported), exported);
|
||||
assert.equal('light' in exported, false);
|
||||
assert.equal('dark' in exported, false);
|
||||
@@ -86,7 +120,7 @@ test('Saved legacy light themes migrate once and preserve both original palettes
|
||||
const legacy = { schema: 1, id: 'saved-light', name: 'Saved light', light: { ...palette, bg: '#fff' }, dark: { ...palette, bg: '#111' } };
|
||||
const old = new Map([[CUSTOM_THEMES_KEY, JSON.stringify([legacy])], ['runestone.appearance', JSON.stringify({ theme: 'custom:saved-light', mode: 'light' })]]);
|
||||
const sections = migrateLegacyPreferences(key => old.get(key) ?? null);
|
||||
assert.equal((sections.customThemes as { schema: number }[])[0].schema, 2);
|
||||
assert.equal((sections.customThemes as { schema: number }[])[0].schema, 3);
|
||||
assert.deepEqual(sections.customThemesLegacy, [legacy]);
|
||||
assert.deepEqual(sections.appearance, { theme: 'custom:saved-light', mode: 'light' });
|
||||
// The browser preview store reads the sections from their old keys.
|
||||
|
||||
Reference in New Issue
Block a user