import fs from 'node:fs'; import postcss from 'postcss'; const file = 'src/client/styles.css'; const root = postcss.parse(fs.readFileSync(file, 'utf8')); const rgb = hex => hex.match(/../g).map(value => parseInt(value, 16)); const luminance = color => color.map(value => value / 255).map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4) .reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index], 0); const ink = rgb('1c2e32'); const background = luminance(rgb('edf3f1')); root.walkDecls(declaration => { if (declaration.prop === 'font-size' && /^\d+px$/.test(declaration.value) && parseInt(declaration.value) < 11) declaration.value = '11px'; if (declaration.prop !== 'color' || !/^#[0-9a-f]{6}$/i.test(declaration.value)) return; if (/\.toast|\.brand-mark|\.send-button/.test(declaration.parent.selector ?? '')) return; const original = rgb(declaration.value.slice(1)); let color = original; for (let mix = 0; (background + 0.05) / (luminance(color) + 0.05) < 4.7 && mix <= 1; mix += 0.03) { color = original.map((value, index) => Math.round(value * (1 - mix) + ink[index] * mix)); } declaration.value = '#' + color.map(value => value.toString(16).padStart(2, '0')).join(''); }); fs.writeFileSync(file, root.toString(), 'utf8'); console.log('Text contrast and minimum caption sizes calibrated.');