// 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'; 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, elevated, active, , dim, text, strong, accent, selection] = palette.surfaces; 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]])), }; const surfaces = { bg, elevated, active, selection, editorOnWhite, panelOnWhite, codeBlockOnWhite: mix(elevated, editorOnWhite, 0.82), menuHoverOnWhite: mix(active, panelOnWhite, 0.82), controlHoverOnWhite: mix(active, '#ffffff', 0.92), }; return { foregrounds, surfaces }; } type Roles = ReturnType; 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', 'menuHoverOnWhite', 'controlHoverOnWhite', 'codeBlockOnWhite']], ] }, ]; 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')!; for (const color of [...palette.surfaces, palette.secondary, ...palette.syntax]) { 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.equal(palette.mode, 'dark'); assert.equal(Object.keys(characterSyntax('harvest-moon')).length, SYNTAX_ROLES.length); assert.deepEqual(characterSyntax('unknown'), {}); }); 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'); });