repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/contrast.test.js
Raw'use strict';
const assert = require('assert');
const fs = require('fs');
// Both modes, as nugu generates them for Theme.qml.
const palettes = JSON.parse(fs.readFileSync('nugu.json', 'utf8'));
assert.deepStrictEqual(Object.keys(palettes), ['dark', 'light']);
function rgb(hex) {
return [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16) / 255);
}
function composite(fg, bg, alpha) {
const a = rgb(fg);
const b = rgb(bg);
return a.map((v, i) => v * alpha + b[i] * (1 - alpha));
}
function luminance(color) {
const values = typeof color === 'string' ? rgb(color) : color;
const linear = values.map(v => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
return linear[0] * 0.2126 + linear[1] * 0.7152 + linear[2] * 0.0722;
}
function contrast(a, b) {
const x = luminance(a);
const y = luminance(b);
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
}
for (const [mode, palette] of Object.entries(palettes)) {
const worstInteractiveFill = composite(palette.ui.accent, palette.ui.backdrop, 0.38);
assert(contrast(palette.content.normal, worstInteractiveFill) >= 4.5, `${mode}: normal text meets WCAG AA on strongest interactive fill`);
// A selected Bar chip inverts: backdrop-coloured text on the focus fill (Bar.qml).
assert(contrast(palette.content.backdrop, palette.ui.focus) >= 4.5, `${mode}: selected chip text meets WCAG AA`);
}
const button = fs.readFileSync('PopupButton.qml', 'utf8');
const badge = fs.readFileSync('PopupBadge.qml', 'utf8');
const text = fs.readFileSync('NuguText.qml', 'utf8');
assert(!/selected:\s*root\.checked \|\| root\.primary/.test(button), 'buttons never select dark text on dark translucent fills');
assert(/color:\s*root\.theme\.content\.normal/.test(badge), 'badge text uses readable content color');
assert(!/selected \? theme\.content\.backdrop/.test(text), 'selected text stays readable on translucent dark controls');
console.log('contrast.test.js: ALL PASS');