// contrast.js from the chapter, checking every text colour of the profile page's stylesheet (Chapter 7).
function luminance(hex) {
const channels = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
const linear = channels.map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2];
}
function contrast(foreground, background) {
const [a, b] = [luminance(foreground), luminance(background)].sort((x, y) => y - x);
return (a + 0.05) / (b + 0.05);
}
// [text, background, where]
const pairs = [
["#1e1b2e", "#f5f5fb", "body text"],
["#1e1b2e", "#ffffff", "text in the cards"],
["#ffffff", "#4338ca", "header text"],
["#4338ca", "#ffffff", "menu links, section titles"],
["#ffffff", "#db2777", "skill tags"],
["#db2777", "#ffffff", "links in the cards"],
["#5b5873", "#f5f5fb", "footer"],
];
let failures = 0;
for (const [fg, bg, where] of pairs) {
const ratio = contrast(fg, bg);
const verdict = ratio >= 4.5 ? "pass" : "fail";
if (ratio < 4.5) failures++;
console.log(`${fg} / ${bg}: ${ratio.toFixed(2)} → ${verdict} (${where})`);
}
console.log(failures === 0 ? "Every pair passes 4.5:1. Nothing to change." : `Colours to change: ${failures}`);
// A colour that would fail, and a darker one that passes:
console.log(`#9ca3af / #ffffff: ${contrast("#9ca3af", "#ffffff").toFixed(2)} → #6b7280 / #ffffff: ${contrast("#6b7280", "#ffffff").toFixed(2)}`);
Recorded output$ node contrast-profile.js
#1e1b2e / #f5f5fb: 15.45 → pass (body text)
#1e1b2e / #ffffff: 16.78 → pass (text in the cards)
#ffffff / #4338ca: 7.90 → pass (header text)
#4338ca / #ffffff: 7.90 → pass (menu links, section titles)
#ffffff / #db2777: 4.60 → pass (skill tags)
#db2777 / #ffffff: 4.60 → pass (links in the cards)
#5b5873 / #f5f5fb: 6.26 → pass (footer)
Every pair passes 4.5:1. Nothing to change.
#9ca3af / #ffffff: 2.54 → #6b7280 / #ffffff: 4.83