Debian Codegraph @

node/colorjs.io/src/GamutRelativeColorSpace.js

raw
1 import ColorSpace from "./ColorSpace.js";
2 import toGamut from "./toGamut.js";
3 import { isNone } from "./util.js";
4
5 /** @import { SpaceOptions } from "./ColorSpace.js" */
6
7 /**
8 * A color space whose chroma-like coordinate is expressed relative to an RGB gamut: chroma = 1 is
9 * the most colorful in-gamut color for the rest of the coordinates, so any chroma in [0, 1] stays
10 * in gamut. The reduced coordinate (`chroma`, default `"c"`) is rescaled against `gamutSpace`; every
11 * other coordinate is inherited unchanged from `base`. Works for any base whose chroma coordinate
12 * brings the color into gamut as it is reduced (e.g. OKLCh, LCH).
13 */
14 export default class GamutRelativeColorSpace extends ColorSpace {
15 /**
16 * @param {SpaceOptions & { gamutSpace: string | ColorSpace, chroma?: string }} options
17 * Requires `base` (the source space) and `gamutSpace` (the RGB gamut). `chroma` names the
18 * coordinate to rescale (default `"c"`).
19 */
20 constructor (options) {
21 let base = ColorSpace.get(options.base);
22 let chroma = options.chroma ?? "c";
23 let ids = Object.keys(base.coords);
24 let chromaIndex = ids.indexOf(chroma);
25
26 if (chromaIndex === -1) {
27 throw new TypeError(
28 `GamutRelativeColorSpace needs a "${chroma}" coordinate, got ${base.id}`,
29 );
30 }
31
32 // Inherit the base's coords; the reduced coordinate becomes a 0–1 fraction of its gamut maximum
33 let coords = Object.fromEntries(
34 Object.entries(base.coords).map(([id, meta]) => [id, { ...meta }]),
35 );
36 coords[chroma].range = [0, 1];
37 delete coords[chroma].refRange;
38 options.coords ??= coords;
39
40 // Serialize the reduced coordinate and any other 0–1 coordinate as percentages; angles as angles
41 options.formats ??= {
42 color: {
43 coords: ids.map(id => {
44 if (base.coords[id].type === "angle") {
45 return "<number> | <angle>";
46 }
47 let [min, max] = base.coords[id].range ?? base.coords[id].refRange ?? [];
48 let pctFirst = id === chroma || (min === 0 && max === 1);
49 return pctFirst ? "<percentage> | <number>" : "<number> | <percentage>";
50 }),
51 },
52 };
53
54 super(options);
55
56 this.chromaIndex = chromaIndex;
57 this.method = `${base.id}.${chroma}`;
58
59 // A chroma that is out of gamut at every lightness and hue: the gamut's most saturated corner
60 // (its primaries/secondaries are its chroma maxima) plus headroom. Seeding the gamut search
61 // from here lands on the first in-gamut crossing — the highest contiguous in-gamut chroma —
62 // and avoids clamping wide gamuts (e.g. Rec.2020 exceeds OKLCh's nominal 0.4 chroma).
63 // prettier-ignore
64 let corners = /** @type {[number, number, number][]} */ (
65 [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]
66 );
67 this.oogChroma =
68 1.01 *
69 Math.max(...corners.map(rgb => base.from(this.gamutSpace, rgb)[this.chromaIndex] || 0));
70 }
71
72 /**
73 * The highest in-gamut value of the reduced coordinate for the given coordinates. The reduced
74 * coordinate's own value is ignored; the others determine the result.
75 * @param {number[]} coords
76 * @returns {number}
77 */
78 maxChroma (coords) {
79 let seed = /** @type {[number, number, number]} */ ([...coords]);
80 seed[this.chromaIndex] = this.oogChroma;
81
82 return toGamut(
83 { space: this.base, coords: seed },
84 // deltaE OK stays sensitive near black, where deltaE 2000 is too forgiving of excess chroma
85 { space: this.gamutSpace, method: this.method, deltaEMethod: "OK", jnd: 0 },
86 ).coords[this.chromaIndex];
87 }
88
89 toBase (coords) {
90 coords = [...coords];
91
92 if (coords.some((c, i) => i !== this.chromaIndex && isNone(c))) {
93 // chroma is a fraction of maxChroma(the other coords); without them all it is undefined
94 if (coords[this.chromaIndex] !== 0) {
95 coords[this.chromaIndex] = null;
96 }
97 }
98 else if (!isNone(coords[this.chromaIndex])) {
99 coords[this.chromaIndex] *= this.maxChroma(coords);
100 }
101
102 return coords;
103 }
104
105 fromBase (coords) {
106 coords = [...coords];
107
108 if (coords.some((c, i) => i !== this.chromaIndex && isNone(c))) {
109 if (coords[this.chromaIndex] !== 0) {
110 coords[this.chromaIndex] = null;
111 }
112 }
113 else if (!isNone(coords[this.chromaIndex])) {
114 let cMax = this.maxChroma(coords);
115 coords[this.chromaIndex] = cMax ? coords[this.chromaIndex] / cMax : 0;
116 }
117
118 return coords;
119 }
120 }