Debian Codegraph @

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

raw
1 // Michelson luminance contrast
2 // the relation between the spread and the sum of the two luminances
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 /**
10 * @param {import("../types.js").ColorTypes} color1
11 * @param {import("../types.js").ColorTypes} color2
12 * @returns {number}
13 */
14 export default function contrastMichelson (color1, color2) {
15 color1 = getColor(color1);
16 color2 = getColor(color2);
17
18 let Y1 = Math.max(getLuminance(color1), 0);
19 let Y2 = Math.max(getLuminance(color2), 0);
20
21 if (Y2 > Y1) {
22 [Y1, Y2] = [Y2, Y1];
23 }
24
25 let denom = Y1 + Y2;
26 return denom === 0 ? 0 : (Y1 - Y2) / denom;
27 }