colorjs.io
Version:
Let’s get serious about color
26 lines (20 loc) • 708 B
JavaScript
// WCAG 2.0 contrast https://www.w3.org/TR/WCAG20-TECHS/G18.html
// Simple contrast, with fixed 5% viewing flare contribution
// Symmetric, does not matter which is foreground and which is background
import getColor from "../getColor.js";
import { getLuminance } from "../luminance.js";
/**
* @param {import("../types.js").ColorTypes} color1
* @param {import("../types.js").ColorTypes} color2
* @returns {number}
*/
export default function contrastWCAG21 (color1, color2) {
color1 = getColor(color1);
color2 = getColor(color2);
let Y1 = Math.max(getLuminance(color1), 0);
let Y2 = Math.max(getLuminance(color2), 0);
if (Y2 > Y1) {
[Y1, Y2] = [Y2, Y1];
}
return (Y1 + 0.05) / (Y2 + 0.05);
}