41 lines
2.2 KiB
TypeScript
41 lines
2.2 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { CHARACTER_THEMES, SYNTAX_ROLES } from '../src/lib/theme/characterThemes.ts';
|
|
|
|
const rgb = (hex: string) => hex.match(/[0-9a-f]{2}/gi)!.map(channel => parseInt(channel, 16));
|
|
const luminance = (hex: string) => rgb(hex).map(channel => channel / 255)
|
|
.map(channel => channel <= .04045 ? channel / 12.92 : ((channel + .055) / 1.055) ** 2.4)
|
|
.reduce((sum, channel, index) => sum + channel * [.2126, .7152, .0722][index], 0);
|
|
const contrast = (a: string, b: string) => (Math.max(luminance(a), luminance(b)) + .05) / (Math.min(luminance(a), luminance(b)) + .05);
|
|
const mix = (a: string, b: string, ratio: number) => '#' + rgb(a)
|
|
.map((channel, index) => Math.round(channel * ratio + rgb(b)[index] * (1 - ratio)).toString(16).padStart(2, '0')).join('');
|
|
|
|
test('Silver Serpent text and syntax remain readable on opaque and image surfaces', () => {
|
|
const { surfaces, syntax, secondary } = CHARACTER_THEMES['silver-serpent'];
|
|
const [bg, elevated, active, , dim, text, strong, accent, selection] = surfaces;
|
|
const panelOnWhite = mix(elevated, '#ffffff', .82);
|
|
const editorOnWhite = mix(bg, '#ffffff', .9);
|
|
const backgrounds = {
|
|
bg, elevated, active, selection,
|
|
panelOnWhite,
|
|
editorOnWhite,
|
|
menuHoverOnWhite: mix(active, panelOnWhite, .82),
|
|
controlHoverOnWhite: mix(active, '#ffffff', .92),
|
|
codeBlockOnWhite: mix(elevated, editorOnWhite, .82),
|
|
};
|
|
const foregrounds = { dim, text, strong, accent, secondary,
|
|
...Object.fromEntries(SYNTAX_ROLES.map((role, index) => [`syntax-${role}`, syntax[index]])) };
|
|
for (const [backgroundName, background] of Object.entries(backgrounds)) {
|
|
for (const [foregroundName, foreground] of Object.entries(foregrounds)) {
|
|
assert(contrast(foreground, background) >= 4.5, `${foregroundName} on ${backgroundName}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('Silver Serpent selection has a readable perimeter and a separate fill', () => {
|
|
const [bg, elevated, , , , , , accent, selection] = CHARACTER_THEMES['silver-serpent'].surfaces;
|
|
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');
|
|
});
|