| 1 |
// WCAG 2.0 contrast https://www.w3.org/TR/WCAG20-TECHS/G18.html |
| 2 |
// Simple contrast, with fixed 5% viewing flare contribution |
| 3 |
// Symmetric, does not matter which is foreground and which is background |
| 4 |
|
| 5 |
import getColor from "../getColor.js"; |
| 6 |
import { getLuminance } from "../luminance.js"; |
| 7 |
|
| 8 |
/** |
| 9 |
* @param {import("../types.js").ColorTypes} color1 |
| 10 |
* @param {import("../types.js").ColorTypes} color2 |
| 11 |
* @returns {number} |
| 12 |
*/ |
| 13 |
export default function contrastWCAG21 (color1, color2) { |
| 14 |
color1 = getColor(color1); |
| 15 |
color2 = getColor(color2); |
| 16 |
|
| 17 |
let Y1 = Math.max(getLuminance(color1), 0); |
| 18 |
let Y2 = Math.max(getLuminance(color2), 0); |
| 19 |
|
| 20 |
if (Y2 > Y1) { |
| 21 |
[Y1, Y2] = [Y2, Y1]; |
| 22 |
} |
| 23 |
|
| 24 |
return (Y1 + 0.05) / (Y2 + 0.05); |
| 25 |
} |