Files
RuneStone/tests/unit/theme/characterPalettes.spec.ts
2026-11-16 04:21:21 -07:00

102 lines
5.4 KiB
TypeScript

// 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 { BUILTIN_THEMES } from '../../../src/lib/theme/themes/index.ts';
import { SYNTAX_ROLES, resolveColors } from '../../../src/lib/theme/roles.ts';
import { SURFACES, appliedColors } from '../../../src/lib/theme/contrast.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)
.map((value, index) => Math.round(value * opacity + rgb(background)[index] * (1 - opacity)).toString(16).padStart(2, '0')).join('');
function luminance(color: string) {
return rgb(color).map(value => value / 255)
.map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4)
.reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index], 0);
}
function contrast(foreground: string, background: string) {
const [high, low] = [luminance(foreground), luminance(background)].sort((a, b) => b - a);
return (high + 0.05) / (low + 0.05);
}
/** 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, 'bg-elevated': elevated, 'bg-active': active, 'text-dim': dim, text, 'text-strong': strong, accent } = palette;
// The app draws text selection at most 65% opaque over the page (UI-029).
const selection = SURFACES.selection(appliedColors(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['accent-2'] },
// A menu or pane row shows interface text and the main accent. Syntax and the second accent show only in documents.
rows: { dim, text, strong, accent },
syntax: Object.fromEntries(SYNTAX_ROLES.map(role => [`syntax-${role}`, palette[`syntax-${role}`]])),
};
const surfaces = {
bg, elevated, active, selection, editorOnWhite, panelOnWhite,
codeBlockOnWhite: mix(elevated, editorOnWhite, 0.82),
// Over an image, a hovered menu or pane row is a 6% veil of text-strong on the tinted panel (UI-061).
hoverOnWhite: mix(strong, panelOnWhite, 0.06),
controlHoverOnWhite: mix(active, '#ffffff', 0.92),
};
return { foregrounds, surfaces };
}
type Roles = ReturnType<typeof roles>;
type Check = [foregrounds: (keyof Roles['foregrounds'])[], surfaces: (keyof Roles['surfaces'])[]];
// Every listed foreground needs 4.5:1 contrast on every listed surface.
const readable: { theme: string; name: string; checks: Check[] }[] = [
{ theme: 'harvest-moon', name: 'Harvest Moon', checks: [
[['ui', 'accents'], ['bg', 'elevated', 'active', 'selection']],
[['syntax'], ['bg', 'elevated', 'selection']],
] },
{ theme: 'crimson-pilot', name: 'Crimson Pilot', checks: [
[['ui', 'accents', 'syntax'], ['bg', 'elevated', 'active', 'selection']],
[['syntax'], ['editorOnWhite', 'codeBlockOnWhite']],
[['ui'], ['panelOnWhite']],
] },
{ theme: 'silver-serpent', name: 'Silver Serpent', checks: [
[['ui', 'accents', 'syntax'], ['bg', 'elevated', 'active', 'selection', 'panelOnWhite', 'editorOnWhite', 'controlHoverOnWhite', 'codeBlockOnWhite']],
[['rows'], ['hoverOnWhite']],
] },
];
for (const { theme, name, checks } of readable) {
test(`${name} text and syntax remain readable on opaque and image surfaces`, () => {
const { foregrounds, surfaces } = roles(theme);
for (const [groups, surfaceNames] of checks) {
for (const [role, foreground] of groups.flatMap(group => Object.entries(foregrounds[group]))) {
for (const surface of surfaceNames) {
assert(contrast(foreground, surfaces[surface]) >= 4.5, `${role} ${foreground} on ${surface} ${surfaces[surface]} needs 4.5:1 contrast`);
}
}
}
});
}
test('Harvest Moon uses the warm character colors for all palette roles', () => {
const palette = characterTheme('harvest-moon')!;
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, `${role} ${color} must follow the copper, amber, red, or cream palette`);
}
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', () => {
const { surfaces: { bg, elevated, selection }, foregrounds: { accents: { accent } } } = roles('silver-serpent');
assert(contrast(accent, selection) >= 3, 'selection perimeter');
assert(contrast(selection, bg) >= 1.5, 'selection against the editor');
assert(contrast(selection, elevated) >= 1.5, 'selection against a panel');
});