// Theme engine v3, step 4: the contrast solver (themeInk.ts). Property test over random palettes. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { contrastRatio } from '../src/themeApi.ts'; import { composite, hexToOklch, inkOn, inkOnEvery, oklchToHex, TARGET } from '../src/themeInk.ts'; import { reach } from '../src/themeSpec.ts'; /** Deterministic PRNG (mulberry32): the property test is reproducible. */ function rng(seed: number) { return () => { seed |= 0; seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } const randomHex = (r: () => number) => `#${Array.from({ length: 3 }, () => Math.floor(r() * 256).toString(16).padStart(2, '0')).join('')}`; test('inkOn keeps an ink that already reads, unchanged', () => { const result = inkOn('#ffffff', '#000000', TARGET.text); assert.deepEqual([result.ink, result.adjusted], ['#000000', false]); }); test('5,000 random fill and ink pairs reach 4.5:1 after inkOn, 3:1 for non-text', () => { const r = rng(20261002); for (let i = 0; i < 5000; i++) { const fill = randomHex(r), preferred = randomHex(r); for (const target of [TARGET.text, TARGET.nonText]) { const { ink, ratio } = inkOn(fill, preferred, target); assert.ok(contrastRatio(ink, fill) >= target - 1e-9, `${preferred} on ${fill}: ${ratio.toFixed(2)} < ${target} (${ink})`); } } }); test('an adjusted ink keeps the hue of the preferred colour (meaning colours stay themselves)', () => { // The live 1.0.2 failure: navy accent text on Creator Studio's indigo pill, 1.19:1. const { ink, adjusted, ratio } = inkOn('#1e1b4b', '#0000a8', TARGET.text); assert.ok(adjusted && ratio >= 4.5, `${ink} ${ratio}`); const hue = (hex: string) => hexToOklch(hex).h; assert.ok(Math.abs(hue(ink) - hue('#0000a8')) < 0.15, `hue drifted: ${ink}`); // X's like pink on white was 3.84:1 as a count. const pink = inkOn('#ffffff', '#f91880', TARGET.text, { keepHue: true }); assert.ok(pink.ratio >= 4.5 && Math.abs(hue(pink.ink) - hue('#f91880')) < 0.15, pink.ink); }); test('the "More" HoverLabel: black on X grey 82 gets a readable ink', () => { const { ratio } = inkOn('#525252', '#000000', TARGET.text); assert.ok(ratio >= 4.5); }); test('every fill of a gradient or glass is honoured', () => { // The 98 title bar is bold chrome type: large-text target on both ends of its gradient. const { ink } = inkOnEvery(['#000080', '#1084d0'], '#c0c0c0', TARGET.large); for (const fill of ['#000080', '#1084d0']) assert.ok(contrastRatio(ink, fill) >= 3, `${ink} on ${fill}`); // No ink reaches 4.5:1 on both: the solver says so instead of pretending (the deriver then darkens the end). const none = inkOnEvery(['#000080', '#1084d0'], '#ffffff', TARGET.text); assert.ok(none.adjusted && none.ratio < 4.5); assert.equal(composite('#00000080', '#ffffff'), '#7f7f7f'); const glass = inkOn('#ffffff80', '#808080', TARGET.text, { over: '#000000' }); assert.ok(contrastRatio(glass.ink, '#808080') >= 4.5 - 0.05); }); test('OKLCH round trip is exact for sRGB colours', () => { const r = rng(7); for (let i = 0; i < 500; i++) { const hex = randomHex(r); assert.equal(oklchToHex(hexToOklch(hex)), hex); } }); test('reach runs on the solver and still guarantees every surface', () => { const out = reach('#888888', ['#ffffff', '#f4f4f4', '#dfdfdf'], 4.5); for (const s of ['#ffffff', '#f4f4f4', '#dfdfdf']) assert.ok(contrastRatio(out, s) >= 4.5); assert.equal(reach('#000000', ['#ffffff'], 7), '#000000'); });