// No daemon run can see what colour xterm paints, and a palette tuned on one // ground goes unreadable on another: brightWhite on a light ground was. Check // every season by day and night against the contrast each colour promises. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; import { parse } from 'svelte/compiler'; const load = async (path) => import( `data:text/javascript;base64,${Buffer.from(stripTypeScriptTypes(await readFile(new URL(path, import.meta.url), 'utf8'))).toString('base64')}` ); const theme = await load('../src/lib/terminal-theme.ts'); const prefsSource = await readFile( new URL('../src/lib/prefs.svelte.ts', import.meta.url), 'utf8', ); // Read the exported palette data without running browser preference effects. const script = ``; const seasonDeclaration = parse(script) .instance.content.body.find( (node) => node.type === 'ExportNamedDeclaration' && node.declaration?.declarations?.some( (item) => item.id.name === 'SEASONS', ), ) ?.declaration.declarations.find((item) => item.id.name === 'SEASONS'); assert.ok(seasonDeclaration?.init, 'preferences must export seasonal palettes'); const { SEASONS: seasons } = await import( `data:text/javascript;base64,${Buffer.from( `export const SEASONS = ${script.slice(seasonDeclaration.init.start, seasonDeclaration.init.end)};`, ).toString('base64')}` ); const semantic = { light: { added: '#5e8933', danger: '#ce2700' }, dark: { added: '#c1dd4a', danger: '#ff8b79' }, }; // The terminal sits in a well a step from the season's ground; walk a few steps either way. const shades = { light: ['#ffffff', '#e9e4ea'], dark: ['#000000', '#3a3340'] }; const HUES = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan']; const bright = (name) => `bright${name[0].toUpperCase()}${name.slice(1)}`; function grounds() { assert.ok(seasons.length > 0, 'preferences must provide seasonal palettes'); return seasons.flatMap((season) => ['light', 'dark'].flatMap((mode) => { const { accent, background, foreground } = season[mode]; return [ background, ...shades[mode].map((shade) => mix(background, shade)), ].map((ground) => ({ mode, background: ground, foreground, accent, ...semantic[mode], })); }), ); } function mix(a, b) { const [x, y] = [theme.parseHex(a), theme.parseHex(b)]; return theme.toHex(x.map((c, i) => c + (y[i] - c) * 0.3)); } test('every terminal colour reads on its ground', () => { for (const ground of grounds()) { const colors = theme.terminalColors(ground); const faint = ['brightBlack', ...(ground.mode === 'dark' ? ['black'] : [])]; for (const name of [ ...HUES, ...HUES.map(bright), 'black', 'white', 'brightBlack', 'brightWhite', 'foreground', ]) { const ratio = theme.contrast(colors[name], colors.background); const floor = faint.includes(name) ? theme.FAINT : theme.READABLE; assert.ok( ratio >= floor - 0.01, `${ground.mode} ${ground.background} ${name} ${colors[name]} is ${ratio.toFixed(2)}:1, under ${floor}`, ); } } });