Debian Codegraph @

node/colorjs.io/src/spaces/okhsv.js

raw
1 // Okhsv class.
2 //
3 // ---- License ----
4 //
5 // Copyright (c) 2021 Björn Ottosson
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of
8 // this software and associated documentation files (the "Software"), to deal in
9 // the Software without restriction, including without limitation the rights to
10 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 // of the Software, and to permit persons to whom the Software is furnished to do
12 // so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 import ColorSpace from "../ColorSpace.js";
25 import RGBColorSpace from "../RGBColorSpace.js";
26 import HSL from "./hsl.js";
27 import Oklab from "./oklab.js";
28 import { spow, multiply_v3_m3x3 } from "../util.js";
29 import { constrain } from "../angles.js";
30 import Okhsl, { tau, toe, toeInv, findCusp, toSt, oklabToLinearRGB, RGBCoeff } from "./okhsl.js";
31
32 /**
33 * Matrices used by this color space (reused from Okhsl), also available as `Okhsv.M`
34 * @type {Record<string, Matrix3x3>}
35 */
36 export const M = { toSRGBLinear: Okhsl.M.toSRGBLinear };
37
38 /** @import { Coords, Matrix3x3, OKCoeff, Vector3 } from "../types.js" */
39
40 /**
41 *
42 * @param {Vector3} hsv
43 * @param {Matrix3x3} lmsToRgb
44 * @param {OKCoeff} okCoeff
45 * @returns {Coords}
46 */
47 function okhsvToOklab (hsv, lmsToRgb, okCoeff) {
48 // Convert from Okhsv to Oklab."""
49
50 let [h, s, v] = hsv;
51 h = constrain(h) / 360.0;
52
53 let l = toeInv(v);
54 let a = null;
55 let b = null;
56
57 // Avoid processing gray or colors with undefined hues
58 if (l !== 0.0 && s !== 0.0) {
59 let a_ = Math.cos(tau * h);
60 let b_ = Math.sin(tau * h);
61
62 let cusp = findCusp(a_, b_, lmsToRgb, okCoeff);
63 let [sMax, tMax] = toSt(cusp);
64 let s0 = 0.5;
65 let k = 1 - s0 / sMax;
66
67 // first we compute L and V as if the gamut is a perfect triangle:
68
69 // L, C when v==1:
70 let lv = 1 - (s * s0) / (s0 + tMax - tMax * k * s);
71 let cv = (s * tMax * s0) / (s0 + tMax - tMax * k * s);
72
73 l = v * lv;
74 let c = v * cv;
75
76 // then we compensate for both toe and the curved top part of the triangle:
77 let lvt = toeInv(lv);
78 let cvt = (cv * lvt) / lv;
79
80 let lNew = toeInv(l);
81 c = (c * lNew) / l;
82 l = lNew;
83
84 // RGB scale
85 let [rs, gs, bs] = oklabToLinearRGB([lvt, a_ * cvt, b_ * cvt], lmsToRgb);
86 let scaleL = spow(1.0 / Math.max(Math.max(rs, gs), Math.max(bs, 0.0)), 1 / 3);
87
88 l = l * scaleL;
89 c = c * scaleL;
90
91 a = c * a_;
92 b = c * b_;
93 }
94
95 return [l, a, b];
96 }
97
98 /**
99 *
100 * @param {Vector3} lab
101 * @param {Matrix3x3} lmsToRgb
102 * @param {OKCoeff} okCoeff
103 * @returns {Coords}
104 */
105 function oklabToOkhsv (lab, lmsToRgb, okCoeff) {
106 // Oklab to Okhsv.
107
108 // Epsilon for saturation just needs to be sufficiently close when denoting achromatic
109 let ε = 1e-4;
110 let l = lab[0];
111 let s = 0.0;
112 let v = toe(l);
113 let c = Math.sqrt(lab[1] ** 2 + lab[2] ** 2);
114 let h = 0.5 + Math.atan2(-lab[2], -lab[1]) / tau;
115
116 if (l !== 0.0 && l !== 1 && c !== 0.0) {
117 let a_ = lab[1] / c;
118 let b_ = lab[2] / c;
119
120 let cusp = findCusp(a_, b_, lmsToRgb, okCoeff);
121 let [sMax, tMax] = toSt(cusp);
122 let s0 = 0.5;
123 let k = 1 - s0 / sMax;
124
125 // first we find `L_v`, `C_v`, `L_vt` and `C_vt`
126 let t = tMax / (c + l * tMax);
127 let lv = t * l;
128 let cv = t * c;
129
130 let lvt = toeInv(lv);
131 let cvt = (cv * lvt) / lv;
132
133 // we can then use these to invert the step that compensates
134 // for the toe and the curved top part of the triangle:
135 let [rs, gs, bs] = oklabToLinearRGB([lvt, a_ * cvt, b_ * cvt], lmsToRgb);
136 let scaleL = spow(1.0 / Math.max(Math.max(rs, gs), Math.max(bs, 0.0)), 1 / 3);
137
138 l = l / scaleL;
139 c = c / scaleL;
140
141 c = (c * toe(l)) / l;
142 l = toe(l);
143
144 // we can now compute v and s:
145 v = l / lv;
146 s = ((s0 + tMax) * cv) / (tMax * s0 + tMax * k * cv);
147 }
148
149 if (Math.abs(s) < ε || v === 0.0) {
150 h = null;
151 }
152 else {
153 h = constrain(h * 360);
154 }
155
156 return [h, s, v];
157 }
158
159 const Okhsv = new ColorSpace({
160 id: "okhsv",
161 name: "Okhsv",
162 coords: {
163 h: {
164 refRange: [0, 360],
165 type: "angle",
166 name: "Hue",
167 },
168 s: {
169 range: [0, 1],
170 name: "Saturation",
171 },
172 v: {
173 range: [0, 1],
174 name: "Value",
175 },
176 },
177
178 base: Oklab,
179 gamutSpace: "self",
180
181 M,
182
183 // Convert Oklab to Okhsl
184 fromBase (lab) {
185 return oklabToOkhsv(lab, M.toSRGBLinear, RGBCoeff);
186 },
187
188 // Convert Okhsl to Oklab
189 toBase (hsl) {
190 return okhsvToOklab(hsl, M.toSRGBLinear, RGBCoeff);
191 },
192
193 formats: {
194 color: {
195 id: "--okhsv",
196 coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"],
197 },
198 },
199 });
200
201 Okhsv.rgbGamut = new RGBColorSpace({
202 id: "okhsv-prism",
203 cssId: "--okhsv-prism",
204 name: "Okhsv Prism",
205 base: Okhsv,
206 fromBase (hsl) {
207 return HSL.toBase([hsl[0], hsl[1] * 100, hsl[2] * 100]);
208 },
209 toBase (rgb) {
210 const hsl = HSL.fromBase(rgb);
211 hsl[1] /= 100;
212 hsl[2] /= 100;
213 return hsl;
214 },
215 });
216
217 export default Okhsv;