Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// 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';
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');const seasons = [...prefsSource.matchAll(/light: \{ accent: '(#\w+)', background: '(#\w+)', foreground: '(#\w+)' \}, dark: \{ accent: '(#\w+)', background: '(#\w+)', foreground: '(#\w+)' \}/g)];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.equal(seasons.length, 4, 'every season must be read from prefs'); return seasons.flatMap(([, la, lb, lf, da, db, df]) => [['light', la, lb, lf], ['dark', da, db, df]].flatMap(([mode, accent, background, foreground]) => [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}`); } }});
test('faint stays fainter than text and bright stays apart from normal', () => { for (const ground of grounds()) { const colors = theme.terminalColors(ground); const against = (name) => theme.contrast(colors[name], colors.background); assert.ok(against('brightBlack') < against('foreground') - 1, `${ground.mode} ${ground.background} bright black must read fainter than text`); for (const hue of HUES) assert.notEqual(colors[hue], colors[bright(hue)], `${ground.mode} ${ground.background} ${hue} and its bright twin must differ`); }});
test('readable keeps a colour that already reads and darkens one that does not by day', () => { assert.equal(theme.readable('#584a5c', '#f6eff8', theme.READABLE), '#584a5c'); const fixed = theme.readable('#e6eaff', '#f6eff8', theme.READABLE); assert.ok(theme.contrast(fixed, '#f6eff8') >= theme.READABLE); assert.ok(theme.luminance(fixed) < theme.luminance('#e6eaff'));});