Skip to content

Instantly share code, notes, and snippets.

@marionebl
Created October 23, 2019 10:25
Show Gist options
  • Save marionebl/fb767a5b1f1df0b4bbe7dd36bc406999 to your computer and use it in GitHub Desktop.
Save marionebl/fb767a5b1f1df0b4bbe7dd36bc406999 to your computer and use it in GitHub Desktop.
var chromatism = require("chromatism")
const blue = chromatism.convert("#0000ff");
const white = chromatism.convert("#ffffff");
function channelLuminance(channel) {
const c = channel / 255;
if (c <= 0.03928) {
return c / 12.92;
}
return Math.pow((c + 0.055) / 1.055, 2.4);
}
// http://www.w3.org/WAI/GL/wiki/Relative_luminance
function relativeLuminance({ r, g, b }) {
return 0.2126 * channelLuminance(r) + 0.7152 * channelLuminance(g) + 0.0722 * channelLuminance(b);
}
// http://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html#key-terms
function contrast(color1, color2) {
const L1 = relativeLuminance(color1);
const L2 = relativeLuminance(color2);
if (L1 < L2) {
return (L2 + 0.05) / (L1 + 0.05);
}
return (L1 + 0.05) / (L2 + 0.05);
}
contrast(white.rgb, blue.rgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment