Debian Codegraph @

node/colorjs.io/src/chromaticity.js

raw
1 import xyz_d65 from "./spaces/xyz-d65.js";
2 import getAll from "./getAll.js";
3
4 /** @import Color, { ColorTypes } from "./color.js" */
5
6 // Chromaticity coordinates
7 /**
8 * @param {ColorTypes} color
9 * @returns {[number, number]}
10 */
11 export function uv (color) {
12 // Assumes getAll() calls getColor() on color
13 let [X, Y, Z] = getAll(color, xyz_d65);
14 let denom = X + 15 * Y + 3 * Z;
15 return [(4 * X) / denom, (9 * Y) / denom];
16 }
17
18 /**
19 * @param {ColorTypes} color
20 * @returns {[number, number]}
21 */
22 export function xy (color) {
23 // Assumes getAll() calls getColor() on color
24 let [X, Y, Z] = getAll(color, xyz_d65);
25 let sum = X + Y + Z;
26 return [X / sum, Y / sum];
27 }
28
29 /**
30 * @param {typeof Color} Color
31 */
32 export function register (Color) {
33 // no setters, as lightness information is lost
34 // when converting color to chromaticity
35 Object.defineProperty(Color.prototype, "uv", {
36 get () {
37 return uv(this);
38 },
39 });
40
41 Object.defineProperty(Color.prototype, "xy", {
42 get () {
43 return xy(this);
44 },
45 });
46 }