Debian Codegraph @

node/colorjs.io/src/contrast/Weber.js

raw
1 // Weber luminance contrast
2 // The difference between the two luminances divided by the lower luminance
3 // Symmetric, does not matter which is foreground and which is background
4 // No black level compensation for flare.
5
6 import getColor from "../getColor.js";
7 import { getLuminance } from "../luminance.js";
8
9 // the darkest sRGB color above black is #000001 and this produces
10 // a plain Weber contrast of ~45647.
11 // So, setting the divide-by-zero result at 50000 is a reasonable
12 // max clamp for the plain Weber
13 const max = 50000;
14
15 /**
16 * @param {import("../types.js").ColorTypes} color1
17 * @param {import("../types.js").ColorTypes} color2
18 * @returns {number}
19 */
20 export default function contrastWeber (color1, color2) {
21 color1 = getColor(color1);
22 color2 = getColor(color2);
23
24 let Y1 = Math.max(getLuminance(color1), 0);
25 let Y2 = Math.max(getLuminance(color2), 0);
26
27 if (Y2 > Y1) {
28 [Y1, Y2] = [Y2, Y1];
29 }
30
31 return Y2 === 0 ? max : (Y1 - Y2) / Y2;
32 }