@solid-primitives/utils 7.0.0-next.1 → 7.0.0-next.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/colors/helpers.d.ts +46 -41
- package/dist/colors/helpers.js +776 -851
- package/dist/colors/index.d.ts +6 -4
- package/dist/colors/index.js +5 -4
- package/dist/colors/intl.d.ts +56 -52
- package/dist/colors/intl.js +55 -66
- package/dist/colors/manipulation.d.ts +136 -131
- package/dist/colors/manipulation.js +215 -242
- package/dist/colors/types.d.ts +73 -68
- package/dist/colors/types.js +1 -14
- package/dist/immutable/array.d.ts +113 -108
- package/dist/immutable/array.js +122 -128
- package/dist/immutable/copy.d.ts +25 -21
- package/dist/immutable/copy.js +31 -29
- package/dist/immutable/index.d.ts +8 -6
- package/dist/immutable/index.js +7 -6
- package/dist/immutable/number.d.ts +11 -7
- package/dist/immutable/number.js +21 -27
- package/dist/immutable/object.d.ts +55 -58
- package/dist/immutable/object.js +46 -48
- package/dist/immutable/types.d.ts +9 -7
- package/dist/immutable/types.js +1 -1
- package/dist/immutable/update.d.ts +33 -28
- package/dist/immutable/update.js +27 -27
- package/dist/index.d.ts +241 -223
- package/dist/index.js +302 -315
- package/dist/types.d.ts +63 -75
- package/dist/types.js +1 -1
- package/package.json +11 -7
package/dist/colors/helpers.js
CHANGED
|
@@ -1,895 +1,820 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Portions of this file are based on code from Kobalte.
|
|
3
|
-
* MIT License, Copyright Fabien Marie-Louise.
|
|
4
|
-
*
|
|
5
|
-
* Credits to the Kobalte team:
|
|
6
|
-
* https://github.com/kobaltedev/kobalte/tree/main/packages/core/src/colors
|
|
7
|
-
*
|
|
8
|
-
* Which in turn is based on code from React Spectrum.
|
|
9
|
-
* Apache License Version 2.0, Copyright 2020 Adobe.
|
|
10
|
-
*
|
|
11
|
-
* Credits to the React Spectrum team:
|
|
12
|
-
* https://github.com/adobe/react-spectrum/blob/68e305768cb829bab7b9836dded593bd731259f3/packages/%40react-stately/color/src/Color.ts
|
|
13
|
-
*/
|
|
1
|
+
/* @ts-self-types="./helpers.d.ts" */
|
|
14
2
|
import { clamp } from "../index.js";
|
|
3
|
+
//#region src/colors/helpers.ts
|
|
15
4
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
throw new Error(`Invalid color value: ${value}`);
|
|
5
|
+
* Parses a color string into a {@link Color} object.
|
|
6
|
+
*
|
|
7
|
+
* Supported formats:
|
|
8
|
+
* - **Hex**: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`
|
|
9
|
+
* - **RGB**: `rgb(r g b)`, `rgb(r, g, b)`, `rgb(r g b / a)`, `rgba(r, g, b, a)`
|
|
10
|
+
* - **HSL**: `hsl(h, s%, l%)`, `hsla(h, s%, l%, a)`
|
|
11
|
+
* - **HSB**: `hsb(h, s%, b%)`, `hsba(h, s%, b%, a)`
|
|
12
|
+
*
|
|
13
|
+
* @throws if the string does not match any supported format.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const color = parseColor("#3264C8");
|
|
18
|
+
* color.toString("hsl"); // "hsl(220 60% 49.02%)"
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function parseColor(value) {
|
|
22
|
+
const res = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
23
|
+
if (res) return res;
|
|
24
|
+
throw new Error(`Invalid color value: ${value}`);
|
|
37
25
|
}
|
|
38
26
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
* Accepts either a color string or an existing {@link Color} and always returns a `Color`.
|
|
28
|
+
* Useful for normalizing props that accept both forms.
|
|
29
|
+
*/
|
|
30
|
+
function normalizeColor(v) {
|
|
31
|
+
return typeof v === "string" ? parseColor(v) : v;
|
|
44
32
|
}
|
|
45
33
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
case "hsb":
|
|
59
|
-
return HSBColor.colorChannels;
|
|
60
|
-
}
|
|
34
|
+
* Returns the ordered channel triple for a given color space.
|
|
35
|
+
*
|
|
36
|
+
* - `"rgb"` → `["red", "green", "blue"]`
|
|
37
|
+
* - `"hsl"` → `["hue", "saturation", "lightness"]`
|
|
38
|
+
* - `"hsb"` → `["hue", "saturation", "brightness"]`
|
|
39
|
+
*/
|
|
40
|
+
function getColorChannels(colorSpace) {
|
|
41
|
+
switch (colorSpace) {
|
|
42
|
+
case "rgb": return RGBColor.colorChannels;
|
|
43
|
+
case "hsl": return HSLColor.colorChannels;
|
|
44
|
+
case "hsb": return HSBColor.colorChannels;
|
|
45
|
+
}
|
|
61
46
|
}
|
|
62
47
|
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return ((hue % 360) + 360) % 360;
|
|
48
|
+
* Normalizes a hue angle to the range `[0, 360]`.
|
|
49
|
+
* Exactly `360` is kept as-is so that a full-circle endpoint stays distinct from `0`.
|
|
50
|
+
*/
|
|
51
|
+
function normalizeHue(hue) {
|
|
52
|
+
if (hue === 360) return hue;
|
|
53
|
+
return (hue % 360 + 360) % 360;
|
|
70
54
|
}
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
// OKLCH L ≤ this value is considered "dark" for the lightness descriptor.
|
|
76
|
-
const MAX_DARK_LIGHTNESS = 0.55;
|
|
77
|
-
// OKLCH chroma below this threshold is treated as achromatic (gray).
|
|
78
|
-
const GRAY_THRESHOLD = 0.001;
|
|
55
|
+
const ORANGE_LIGHTNESS_THRESHOLD = .68;
|
|
56
|
+
const YELLOW_GREEN_LIGHTNESS_THRESHOLD = .85;
|
|
57
|
+
const MAX_DARK_LIGHTNESS = .55;
|
|
58
|
+
const GRAY_THRESHOLD = .001;
|
|
79
59
|
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
60
|
+
* Ordered hue-segment table for `getOklchHue`. Each entry is `[startDegrees, hueName]`.
|
|
61
|
+
* A hue angle H belongs to segment i when `OKLCH_HUES[i][0] <= H < OKLCH_HUES[i+1][0]`.
|
|
62
|
+
* The list wraps: angles ≥ 349° fall back to `"pink"` (the implicit 360° endpoint).
|
|
63
|
+
*/
|
|
84
64
|
const OKLCH_HUES = [
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
65
|
+
[0, "pink"],
|
|
66
|
+
[15, "red"],
|
|
67
|
+
[48, "orange"],
|
|
68
|
+
[94, "yellow"],
|
|
69
|
+
[135, "green"],
|
|
70
|
+
[175, "cyan"],
|
|
71
|
+
[264, "blue"],
|
|
72
|
+
[284, "purple"],
|
|
73
|
+
[320, "magenta"],
|
|
74
|
+
[349, "pink"]
|
|
95
75
|
];
|
|
96
76
|
/** Formats `value` with the runtime locale (`undefined` = user's system locale). */
|
|
97
77
|
function formatNumber(value, options) {
|
|
98
|
-
|
|
78
|
+
return new Intl.NumberFormat(void 0, options).format(value);
|
|
99
79
|
}
|
|
100
80
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
* - **Achromatic fallback**: Returns `"gray"` immediately when C < `GRAY_THRESHOLD`.
|
|
234
|
-
*/
|
|
235
|
-
getOklchHue(l, c, h, translations) {
|
|
236
|
-
if (c < GRAY_THRESHOLD)
|
|
237
|
-
return [translations.gray, l];
|
|
238
|
-
for (let i = 0; i < OKLCH_HUES.length; i++) {
|
|
239
|
-
const [hue] = OKLCH_HUES[i];
|
|
240
|
-
let hueName = OKLCH_HUES[i][1];
|
|
241
|
-
const [nextHue, nextHueName] = OKLCH_HUES[i + 1] || [360, "pink"];
|
|
242
|
-
if (h >= hue && h < nextHue) {
|
|
243
|
-
if (hueName === "orange") {
|
|
244
|
-
if (l < ORANGE_LIGHTNESS_THRESHOLD) {
|
|
245
|
-
hueName = "brown";
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
// Shift L so mid-lightness orange doesn't get a "light" descriptor.
|
|
249
|
-
l = l - ORANGE_LIGHTNESS_THRESHOLD + MAX_DARK_LIGHTNESS;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (h > hue + (nextHue - hue) / 2 && hueName !== nextHueName) {
|
|
253
|
-
// Past the midpoint of the segment — blend toward the next hue.
|
|
254
|
-
hueName = `${hueName} ${nextHueName}`;
|
|
255
|
-
}
|
|
256
|
-
else if (hueName === "yellow" && l < YELLOW_GREEN_LIGHTNESS_THRESHOLD) {
|
|
257
|
-
hueName = "yellow green";
|
|
258
|
-
}
|
|
259
|
-
// @ts-expect-error — hueName is a dynamic key into translations
|
|
260
|
-
return [translations[hueName], l];
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
throw new Error("Unexpected hue");
|
|
264
|
-
}
|
|
265
|
-
/** Returns only the hue family name (e.g. `"blue"`) without lightness/chroma descriptors. */
|
|
266
|
-
getHueName(translations) {
|
|
267
|
-
const [l, c, h] = colorToOKLCH(this);
|
|
268
|
-
const [name] = this.getOklchHue(l, c, h, translations);
|
|
269
|
-
return name;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
81
|
+
* Shared base providing concrete implementations of the channel-read/write, naming,
|
|
82
|
+
* and axis-resolution methods declared on {@link IColor}. Subclasses (`RGBColor`,
|
|
83
|
+
* `HSBColor`, `HSLColor`) supply format conversion and the channel-range /
|
|
84
|
+
* format-options API.
|
|
85
|
+
*
|
|
86
|
+
* Channel names are used directly as private field names on each subclass so that
|
|
87
|
+
* `getChannelValue` / `withChannelValue` can read and write them via string keying
|
|
88
|
+
* without a per-subclass switch statement.
|
|
89
|
+
*/
|
|
90
|
+
var Color = class {
|
|
91
|
+
/** Delegates to RGB since only `RGBColor` can pack channels directly into a 24-bit int. */
|
|
92
|
+
toHexInt() {
|
|
93
|
+
return this.toFormat("rgb").toHexInt();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Reads a channel value by name. Channel name strings correspond directly to
|
|
97
|
+
* the private field names on each concrete subclass (e.g. `"red"` → `this.red`).
|
|
98
|
+
* The `@ts-expect-error` suppresses the untyped index-access warning.
|
|
99
|
+
*/
|
|
100
|
+
getChannelValue(channel) {
|
|
101
|
+
if (channel in this) return this[channel];
|
|
102
|
+
throw new Error(`Unsupported color channel: ${channel}`);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns a shallow clone of this color with one channel replaced.
|
|
106
|
+
* Clones first to preserve immutability, then writes via the same string-keyed
|
|
107
|
+
* approach as `getChannelValue`.
|
|
108
|
+
*/
|
|
109
|
+
withChannelValue(channel, value) {
|
|
110
|
+
if (channel in this) {
|
|
111
|
+
const x = this.clone();
|
|
112
|
+
x[channel] = value;
|
|
113
|
+
return x;
|
|
114
|
+
}
|
|
115
|
+
throw new Error(`Unsupported color channel: ${channel}`);
|
|
116
|
+
}
|
|
117
|
+
/** Returns the translation string for `channel` (e.g. `"Hue"`, `"Saturation"`). */
|
|
118
|
+
getChannelName(channel, translations) {
|
|
119
|
+
return translations[channel];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Resolves the three channel axes used by a 2D color-area picker.
|
|
123
|
+
* `xChannel` and `yChannel` are caller-supplied (either may be omitted).
|
|
124
|
+
* `zChannel` is the remaining channel not already assigned to x or y.
|
|
125
|
+
* Omitted channels are filled in order from the color space's channel list.
|
|
126
|
+
*/
|
|
127
|
+
getColorSpaceAxes(xyChannels) {
|
|
128
|
+
const { xChannel, yChannel } = xyChannels;
|
|
129
|
+
const xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
130
|
+
const yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
131
|
+
return {
|
|
132
|
+
xChannel: xCh,
|
|
133
|
+
yChannel: yCh,
|
|
134
|
+
zChannel: this.getColorChannels().find((c) => c !== xCh && c !== yCh)
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Produces a human-readable color description (e.g. `"dark vibrant blue"`) suitable
|
|
139
|
+
* for `aria-label` attributes. Uses OKLCH — a perceptually uniform space — so that
|
|
140
|
+
* lightness and chroma descriptors correspond to what the eye perceives.
|
|
141
|
+
*
|
|
142
|
+
* Pipeline:
|
|
143
|
+
* 1. Convert to OKLCH (L = perceived lightness 0–1, C = chroma, H = hue angle).
|
|
144
|
+
* 2. Short-circuit for near-white (L > 0.999) and near-black (L < 0.001).
|
|
145
|
+
* 3. Map hue angle to a named hue family via `getOklchHue` (handles orange/brown split,
|
|
146
|
+
* hue blending at segment midpoints, etc.).
|
|
147
|
+
* 4. Choose a chroma descriptor: `"pale"` or `"grayish"` for low-C, `"vibrant"` for high-C.
|
|
148
|
+
* 5. Choose a lightness descriptor: `"very dark"` / `"dark"` / `"light"` / `"very light"`.
|
|
149
|
+
* 6. Compose via `translations.colorName`. If alpha < 1, append the transparency percentage.
|
|
150
|
+
*/
|
|
151
|
+
getColorName(translations) {
|
|
152
|
+
const [initialL, c, h] = colorToOKLCH(this);
|
|
153
|
+
let l = initialL;
|
|
154
|
+
if (l > .999) return translations.white;
|
|
155
|
+
if (l < .001) return translations.black;
|
|
156
|
+
const [hue, newL] = this.getOklchHue(l, c, h, translations);
|
|
157
|
+
l = newL;
|
|
158
|
+
let lightness = "";
|
|
159
|
+
let chroma = "";
|
|
160
|
+
if (c <= .1 && c >= GRAY_THRESHOLD) chroma = l >= .7 ? "pale" : "grayish";
|
|
161
|
+
else if (c >= .15) chroma = "vibrant";
|
|
162
|
+
if (l < .3) lightness = "very dark";
|
|
163
|
+
else if (l < MAX_DARK_LIGHTNESS) lightness = "dark";
|
|
164
|
+
else if (l < .7) {} else if (l < .85) lightness = "light";
|
|
165
|
+
else lightness = "very light";
|
|
166
|
+
if (chroma) chroma = translations[chroma];
|
|
167
|
+
if (lightness) lightness = translations[lightness];
|
|
168
|
+
const alpha = this.getChannelValue("alpha");
|
|
169
|
+
if (alpha < 1) {
|
|
170
|
+
const percentTransparent = formatNumber(1 - alpha, { style: "percent" });
|
|
171
|
+
return translations.transparentColorName(lightness, chroma, hue, percentTransparent).replace(/\s+/g, " ").trim();
|
|
172
|
+
}
|
|
173
|
+
return translations.colorName(lightness, chroma, hue).replace(/\s+/g, " ").trim();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Maps OKLCH `(L, C, H)` to a named hue family and optionally adjusts `L`.
|
|
177
|
+
*
|
|
178
|
+
* Special cases:
|
|
179
|
+
* - **Orange vs brown**: Hues in the orange segment (48–94°) are classified as
|
|
180
|
+
* `"brown"` when L < `ORANGE_LIGHTNESS_THRESHOLD`. For true orange, L is shifted
|
|
181
|
+
* down by the threshold so that the lightness descriptor still reads naturally.
|
|
182
|
+
* - **Yellow green**: Yellow (94–135°) reads as `"yellow green"` when L is below
|
|
183
|
+
* `YELLOW_GREEN_LIGHTNESS_THRESHOLD`.
|
|
184
|
+
* - **Hue blending**: When H falls in the upper half of a segment (past the midpoint
|
|
185
|
+
* toward the next hue), the name becomes a compound `"<current> <next>"` (e.g.
|
|
186
|
+
* `"blue purple"`), unless the two adjacent segment names are already the same
|
|
187
|
+
* (the pink wrap-around case).
|
|
188
|
+
* - **Achromatic fallback**: Returns `"gray"` immediately when C < `GRAY_THRESHOLD`.
|
|
189
|
+
*/
|
|
190
|
+
getOklchHue(l, c, h, translations) {
|
|
191
|
+
if (c < GRAY_THRESHOLD) return [translations.gray, l];
|
|
192
|
+
for (let i = 0; i < OKLCH_HUES.length; i++) {
|
|
193
|
+
const [hue] = OKLCH_HUES[i];
|
|
194
|
+
let hueName = OKLCH_HUES[i][1];
|
|
195
|
+
const [nextHue, nextHueName] = OKLCH_HUES[i + 1] || [360, "pink"];
|
|
196
|
+
if (h >= hue && h < nextHue) {
|
|
197
|
+
if (hueName === "orange") if (l < ORANGE_LIGHTNESS_THRESHOLD) hueName = "brown";
|
|
198
|
+
else l = l - ORANGE_LIGHTNESS_THRESHOLD + MAX_DARK_LIGHTNESS;
|
|
199
|
+
if (h > hue + (nextHue - hue) / 2 && hueName !== nextHueName) hueName = `${hueName} ${nextHueName}`;
|
|
200
|
+
else if (hueName === "yellow" && l < YELLOW_GREEN_LIGHTNESS_THRESHOLD) hueName = "yellow green";
|
|
201
|
+
return [translations[hueName], l];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
throw new Error("Unexpected hue");
|
|
205
|
+
}
|
|
206
|
+
/** Returns only the hue family name (e.g. `"blue"`) without lightness/chroma descriptors. */
|
|
207
|
+
getHueName(translations) {
|
|
208
|
+
const [l, c, h] = colorToOKLCH(this);
|
|
209
|
+
const [name] = this.getOklchHue(l, c, h, translations);
|
|
210
|
+
return name;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
272
213
|
/**
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
class RGBColor extends Color {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
getChannelFormatOptions(channel) {
|
|
476
|
-
switch (channel) {
|
|
477
|
-
case "red":
|
|
478
|
-
case "green":
|
|
479
|
-
case "blue":
|
|
480
|
-
return { style: "decimal" };
|
|
481
|
-
case "alpha":
|
|
482
|
-
return { style: "percent", maximumFractionDigits: 2 };
|
|
483
|
-
default:
|
|
484
|
-
throw new Error(`Unknown color channel: ${channel}`);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
formatChannelValue(channel) {
|
|
488
|
-
return formatNumber(this.getChannelValue(channel), this.getChannelFormatOptions(channel));
|
|
489
|
-
}
|
|
490
|
-
getColorSpace() {
|
|
491
|
-
return "rgb";
|
|
492
|
-
}
|
|
493
|
-
static colorChannels = ["red", "green", "blue"];
|
|
494
|
-
getColorChannels() {
|
|
495
|
-
return RGBColor.colorChannels;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
// Matches `hsb(h, s%, b%)` and `hsba(h, s%, b%, a)`.
|
|
499
|
-
// Components may be comma- or space-separated; values may be signed or decimal.
|
|
214
|
+
* RGB color with red/green/blue stored as integers in `[0, 255]` and alpha as a
|
|
215
|
+
* float in `[0, 1]`. This is the canonical representation — hex strings and `rgb()`
|
|
216
|
+
* strings parse directly into this class, and all other spaces delegate `toHexInt()`
|
|
217
|
+
* through an RGB conversion.
|
|
218
|
+
*/
|
|
219
|
+
var RGBColor = class RGBColor extends Color {
|
|
220
|
+
red;
|
|
221
|
+
green;
|
|
222
|
+
blue;
|
|
223
|
+
alpha;
|
|
224
|
+
constructor(red, green, blue, alpha) {
|
|
225
|
+
super();
|
|
226
|
+
this.red = red;
|
|
227
|
+
this.green = green;
|
|
228
|
+
this.blue = blue;
|
|
229
|
+
this.alpha = alpha;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Parses hex (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`) and `rgb()`/`rgba()` strings.
|
|
233
|
+
* Returns `undefined` for non-matching input.
|
|
234
|
+
*
|
|
235
|
+
* Hex shorthand is expanded by doubling each nibble (`#abc` → `#aabbcc`).
|
|
236
|
+
* The alpha byte (if present in hex) is normalized from the `[0, 255]` range to `[0, 1]`.
|
|
237
|
+
* Both space-separated (`rgb(r g b / a)`) and comma-separated (`rgb(r, g, b)`) syntaxes
|
|
238
|
+
* are accepted. Tokens with a `%` suffix are converted: RGB channels to 0–255,
|
|
239
|
+
* alpha to a 0–1 fraction. Non-numeric or non-finite tokens reject the whole match.
|
|
240
|
+
*/
|
|
241
|
+
static parse(value) {
|
|
242
|
+
let colors = [];
|
|
243
|
+
if (/^#[\da-f]+$/i.test(value) && [
|
|
244
|
+
4,
|
|
245
|
+
5,
|
|
246
|
+
7,
|
|
247
|
+
9
|
|
248
|
+
].includes(value.length)) {
|
|
249
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
250
|
+
while (values.length > 0) colors.push(Number.parseInt(values.splice(0, 2).join(""), 16));
|
|
251
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
252
|
+
}
|
|
253
|
+
const match = value.match(/^rgba?\((.*)\)$/);
|
|
254
|
+
if (match?.[1]) {
|
|
255
|
+
const tokens = match[1].replaceAll(/,|\//gu, " ").trim().split(/\s+/);
|
|
256
|
+
if (tokens.length !== 3 && tokens.length !== 4) return void 0;
|
|
257
|
+
const parsed = [];
|
|
258
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
259
|
+
const token = tokens[i];
|
|
260
|
+
let num;
|
|
261
|
+
if (token.endsWith("%")) {
|
|
262
|
+
const pct = Number(token.slice(0, -1));
|
|
263
|
+
if (!Number.isFinite(pct)) return void 0;
|
|
264
|
+
num = i < 3 ? pct / 100 * 255 : pct / 100;
|
|
265
|
+
} else {
|
|
266
|
+
num = Number(token);
|
|
267
|
+
if (!Number.isFinite(num)) return void 0;
|
|
268
|
+
}
|
|
269
|
+
parsed.push(clamp(num, 0, i < 3 ? 255 : 1));
|
|
270
|
+
}
|
|
271
|
+
colors = parsed;
|
|
272
|
+
}
|
|
273
|
+
if (colors[0] === void 0 || colors[1] === void 0 || colors[2] === void 0) return;
|
|
274
|
+
return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
275
|
+
}
|
|
276
|
+
toString(format = "css") {
|
|
277
|
+
switch (format) {
|
|
278
|
+
case "hex": return `#${(this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase()}`;
|
|
279
|
+
case "hexa": return `#${(this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0") + Math.round(this.alpha * 255).toString(16).padStart(2, "0")).toUpperCase()}`;
|
|
280
|
+
case "css":
|
|
281
|
+
case "rgb": return `rgb(${this.red}, ${this.green}, ${this.blue}${this.alpha !== 1 && this.alpha !== 100 ? ` / ${this.alpha}` : ""})`;
|
|
282
|
+
case "rgba": return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
283
|
+
default: return this.toFormat(format).toString(format);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
toFormat(format) {
|
|
287
|
+
switch (format) {
|
|
288
|
+
case "hex":
|
|
289
|
+
case "hexa":
|
|
290
|
+
case "rgb":
|
|
291
|
+
case "rgba": return this;
|
|
292
|
+
case "hsb":
|
|
293
|
+
case "hsba": return this.toHSB();
|
|
294
|
+
case "hsl":
|
|
295
|
+
case "hsla": return this.toHSL();
|
|
296
|
+
default: throw new Error(`Unsupported color conversion: rgb -> ${format}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** Packs red/green/blue into a 24-bit integer via bitwise shifts (alpha is not included). */
|
|
300
|
+
toHexInt() {
|
|
301
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Standard RGB → HSB conversion.
|
|
305
|
+
* brightness = max(r, g, b); saturation = chroma / brightness;
|
|
306
|
+
* hue is derived from which primary sits at the maximum.
|
|
307
|
+
*/
|
|
308
|
+
toHSB() {
|
|
309
|
+
const red = this.red / 255;
|
|
310
|
+
const green = this.green / 255;
|
|
311
|
+
const blue = this.blue / 255;
|
|
312
|
+
const min = Math.min(red, green, blue);
|
|
313
|
+
const brightness = Math.max(red, green, blue);
|
|
314
|
+
const chroma = brightness - min;
|
|
315
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
316
|
+
let hue = 0;
|
|
317
|
+
if (chroma !== 0) {
|
|
318
|
+
switch (brightness) {
|
|
319
|
+
case red:
|
|
320
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
321
|
+
break;
|
|
322
|
+
case green:
|
|
323
|
+
hue = (blue - red) / chroma + 2;
|
|
324
|
+
break;
|
|
325
|
+
case blue:
|
|
326
|
+
hue = (red - green) / chroma + 4;
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
hue /= 6;
|
|
330
|
+
}
|
|
331
|
+
return new HSBColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Standard RGB → HSL conversion.
|
|
335
|
+
* lightness = (max + min) / 2;
|
|
336
|
+
* saturation = chroma / (1 − |2L − 1|);
|
|
337
|
+
* hue uses the same sector formula as `toHSB`.
|
|
338
|
+
*/
|
|
339
|
+
toHSL() {
|
|
340
|
+
const red = this.red / 255;
|
|
341
|
+
const green = this.green / 255;
|
|
342
|
+
const blue = this.blue / 255;
|
|
343
|
+
const min = Math.min(red, green, blue);
|
|
344
|
+
const max = Math.max(red, green, blue);
|
|
345
|
+
const lightness = (max + min) / 2;
|
|
346
|
+
const chroma = max - min;
|
|
347
|
+
let hue;
|
|
348
|
+
let saturation;
|
|
349
|
+
if (chroma === 0) hue = saturation = 0;
|
|
350
|
+
else {
|
|
351
|
+
saturation = chroma / (lightness < .5 ? max + min : 2 - max - min);
|
|
352
|
+
switch (max) {
|
|
353
|
+
case red:
|
|
354
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
355
|
+
break;
|
|
356
|
+
case green:
|
|
357
|
+
hue = (blue - red) / chroma + 2;
|
|
358
|
+
break;
|
|
359
|
+
default:
|
|
360
|
+
hue = (red - green) / chroma + 4;
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
hue /= 6;
|
|
364
|
+
}
|
|
365
|
+
return new HSLColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
|
|
366
|
+
}
|
|
367
|
+
clone() {
|
|
368
|
+
return new RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
369
|
+
}
|
|
370
|
+
getChannelRange(channel) {
|
|
371
|
+
switch (channel) {
|
|
372
|
+
case "red":
|
|
373
|
+
case "green":
|
|
374
|
+
case "blue": return {
|
|
375
|
+
minValue: 0,
|
|
376
|
+
maxValue: 255,
|
|
377
|
+
step: 1,
|
|
378
|
+
pageSize: 17
|
|
379
|
+
};
|
|
380
|
+
case "alpha": return {
|
|
381
|
+
minValue: 0,
|
|
382
|
+
maxValue: 1,
|
|
383
|
+
step: .01,
|
|
384
|
+
pageSize: .1
|
|
385
|
+
};
|
|
386
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
getChannelFormatOptions(channel) {
|
|
390
|
+
switch (channel) {
|
|
391
|
+
case "red":
|
|
392
|
+
case "green":
|
|
393
|
+
case "blue": return { style: "decimal" };
|
|
394
|
+
case "alpha": return {
|
|
395
|
+
style: "percent",
|
|
396
|
+
maximumFractionDigits: 2
|
|
397
|
+
};
|
|
398
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
formatChannelValue(channel) {
|
|
402
|
+
return formatNumber(this.getChannelValue(channel), this.getChannelFormatOptions(channel));
|
|
403
|
+
}
|
|
404
|
+
getColorSpace() {
|
|
405
|
+
return "rgb";
|
|
406
|
+
}
|
|
407
|
+
static colorChannels = [
|
|
408
|
+
"red",
|
|
409
|
+
"green",
|
|
410
|
+
"blue"
|
|
411
|
+
];
|
|
412
|
+
getColorChannels() {
|
|
413
|
+
return RGBColor.colorChannels;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
500
416
|
const HSB_REGEX = /hsb\(([-+]?\d+(?:\.\d+)?(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%)\)|hsba\(([-+]?\d+(?:\.\d+)?(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?)\)/;
|
|
501
417
|
/**
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
class HSBColor extends Color {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
"brightness",
|
|
640
|
-
];
|
|
641
|
-
getColorChannels() {
|
|
642
|
-
return HSBColor.colorChannels;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
// Matches `hsl(h, s%, l%)` / `hsl(h s% l% / a)` and `hsla(h, s%, l%, a)` / `hsla(h s% l% / a)`.
|
|
646
|
-
// Components may be comma- or space-separated; the alpha separator also accepts `/` (CSS Color 4).
|
|
418
|
+
* HSB (Hue–Saturation–Brightness) color, also known as HSV (Hue–Saturation–Value).
|
|
419
|
+
* Hue is in degrees `[0, 360]`; saturation and brightness are percentages `[0, 100]`;
|
|
420
|
+
* alpha is a float `[0, 1]`.
|
|
421
|
+
*/
|
|
422
|
+
var HSBColor = class HSBColor extends Color {
|
|
423
|
+
hue;
|
|
424
|
+
saturation;
|
|
425
|
+
brightness;
|
|
426
|
+
alpha;
|
|
427
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
428
|
+
super();
|
|
429
|
+
this.hue = hue;
|
|
430
|
+
this.saturation = saturation;
|
|
431
|
+
this.brightness = brightness;
|
|
432
|
+
this.alpha = alpha;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Parses `hsb(h, s%, b%)` and `hsba(h, s%, b%, a)` strings via `HSB_REGEX`.
|
|
436
|
+
* Returns `undefined` for non-matching input.
|
|
437
|
+
* Hue is normalised with `normalizeHue`; saturation/brightness are clamped to 0–100;
|
|
438
|
+
* alpha defaults to 1 when absent.
|
|
439
|
+
*/
|
|
440
|
+
static parse(value) {
|
|
441
|
+
let m;
|
|
442
|
+
if (m = value.match(HSB_REGEX)) {
|
|
443
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(/[,\s]+/).filter(Boolean).map((n) => Number(n.trim().replace("%", "")));
|
|
444
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(b)) return void 0;
|
|
445
|
+
if (a !== void 0 && !Number.isFinite(a)) return void 0;
|
|
446
|
+
return new HSBColor(normalizeHue(h), clamp(s, 0, 100), clamp(b, 0, 100), clamp(a ?? 1, 0, 1));
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
toString(format = "css") {
|
|
450
|
+
switch (format) {
|
|
451
|
+
case "css": return this.toHSL().toString("css");
|
|
452
|
+
case "hex": return this.toRGB().toString("hex");
|
|
453
|
+
case "hexa": return this.toRGB().toString("hexa");
|
|
454
|
+
case "hsb": return `hsb(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.brightness, 2)}%)`;
|
|
455
|
+
case "hsba": return `hsba(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.brightness, 2)}% ${this.alpha})`;
|
|
456
|
+
default: return this.toFormat(format).toString(format);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
toFormat(format) {
|
|
460
|
+
switch (format) {
|
|
461
|
+
case "hsb":
|
|
462
|
+
case "hsba": return this;
|
|
463
|
+
case "hsl":
|
|
464
|
+
case "hsla": return this.toHSL();
|
|
465
|
+
case "rgb":
|
|
466
|
+
case "rgba": return this.toRGB();
|
|
467
|
+
default: throw new Error(`Unsupported color conversion: hsb -> ${format}`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* HSB → HSL conversion:
|
|
472
|
+
* L = B × (1 − S/2);
|
|
473
|
+
* S_hsl = (B − L) / min(L, 1 − L) (0 at achromatic extremes)
|
|
474
|
+
*/
|
|
475
|
+
toHSL() {
|
|
476
|
+
let saturation = this.saturation / 100;
|
|
477
|
+
const brightness = this.brightness / 100;
|
|
478
|
+
const lightness = brightness * (1 - saturation / 2);
|
|
479
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
480
|
+
return new HSLColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* HSB → RGB using the compact HSV formula.
|
|
484
|
+
* `fn(n)` evaluates one channel:
|
|
485
|
+
* channel = B − S·B · clamp((n + H/60) mod 6, 0, 1)
|
|
486
|
+
* where n=5 → red, n=3 → green, n=1 → blue.
|
|
487
|
+
*/
|
|
488
|
+
toRGB() {
|
|
489
|
+
const hue = this.hue;
|
|
490
|
+
const saturation = this.saturation / 100;
|
|
491
|
+
const brightness = this.brightness / 100;
|
|
492
|
+
const fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
493
|
+
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
494
|
+
}
|
|
495
|
+
clone() {
|
|
496
|
+
return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
497
|
+
}
|
|
498
|
+
getChannelRange(channel) {
|
|
499
|
+
switch (channel) {
|
|
500
|
+
case "hue": return {
|
|
501
|
+
minValue: 0,
|
|
502
|
+
maxValue: 360,
|
|
503
|
+
step: 1,
|
|
504
|
+
pageSize: 15
|
|
505
|
+
};
|
|
506
|
+
case "saturation":
|
|
507
|
+
case "brightness": return {
|
|
508
|
+
minValue: 0,
|
|
509
|
+
maxValue: 100,
|
|
510
|
+
step: 1,
|
|
511
|
+
pageSize: 10
|
|
512
|
+
};
|
|
513
|
+
case "alpha": return {
|
|
514
|
+
minValue: 0,
|
|
515
|
+
maxValue: 1,
|
|
516
|
+
step: .01,
|
|
517
|
+
pageSize: .1
|
|
518
|
+
};
|
|
519
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
getChannelFormatOptions(channel) {
|
|
523
|
+
switch (channel) {
|
|
524
|
+
case "hue": return {
|
|
525
|
+
style: "unit",
|
|
526
|
+
unit: "degree",
|
|
527
|
+
unitDisplay: "narrow"
|
|
528
|
+
};
|
|
529
|
+
case "saturation":
|
|
530
|
+
case "brightness":
|
|
531
|
+
case "alpha": return {
|
|
532
|
+
style: "percent",
|
|
533
|
+
maximumFractionDigits: 2
|
|
534
|
+
};
|
|
535
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
formatChannelValue(channel) {
|
|
539
|
+
let value = this.getChannelValue(channel);
|
|
540
|
+
if (channel === "saturation" || channel === "brightness") value /= 100;
|
|
541
|
+
return formatNumber(value, this.getChannelFormatOptions(channel));
|
|
542
|
+
}
|
|
543
|
+
getColorSpace() {
|
|
544
|
+
return "hsb";
|
|
545
|
+
}
|
|
546
|
+
static colorChannels = [
|
|
547
|
+
"hue",
|
|
548
|
+
"saturation",
|
|
549
|
+
"brightness"
|
|
550
|
+
];
|
|
551
|
+
getColorChannels() {
|
|
552
|
+
return HSBColor.colorChannels;
|
|
553
|
+
}
|
|
554
|
+
};
|
|
647
555
|
const HSL_REGEX = /hsl\(([-+]?\d+(?:\.\d+)?(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:(?:\s*[,/]\s*|\s+)[-+]?\d+(?:\.\d+)?)?)\)|hsla\(([-+]?\d+(?:\.\d+)?(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*,\s*|\s+)[-+]?\d+(?:\.\d+)?%(?:\s*[,/]\s*|\s+)[-+]?\d+(?:\.\d+)?)\)/;
|
|
648
556
|
/**
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
class HSLColor extends Color {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
return HSLColor.colorChannels;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
557
|
+
* HSL (Hue–Saturation–Lightness) color — the native CSS color notation.
|
|
558
|
+
* Hue is in degrees `[0, 360]`; saturation and lightness are percentages `[0, 100]`;
|
|
559
|
+
* alpha is a float `[0, 1]`.
|
|
560
|
+
*/
|
|
561
|
+
var HSLColor = class HSLColor extends Color {
|
|
562
|
+
hue;
|
|
563
|
+
saturation;
|
|
564
|
+
lightness;
|
|
565
|
+
alpha;
|
|
566
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
567
|
+
super();
|
|
568
|
+
this.hue = hue;
|
|
569
|
+
this.saturation = saturation;
|
|
570
|
+
this.lightness = lightness;
|
|
571
|
+
this.alpha = alpha;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Parses `hsl(h, s%, l%)` and `hsla(h, s%, l%, a)` strings via `HSL_REGEX`.
|
|
575
|
+
* Returns `undefined` for non-matching input.
|
|
576
|
+
* Hue is normalised with `normalizeHue`; saturation/lightness are clamped to 0–100;
|
|
577
|
+
* alpha defaults to 1 when absent.
|
|
578
|
+
*/
|
|
579
|
+
static parse(value) {
|
|
580
|
+
let m;
|
|
581
|
+
if (m = value.match(HSL_REGEX)) {
|
|
582
|
+
const [h, s, l, a] = (m[1] ?? m[2]).replace(/\//g, " ").split(/[,\s]+/).filter(Boolean).map((n) => Number(n.trim().replace("%", "")));
|
|
583
|
+
if (!Number.isFinite(h) || !Number.isFinite(s) || !Number.isFinite(l)) return void 0;
|
|
584
|
+
if (a !== void 0 && !Number.isFinite(a)) return void 0;
|
|
585
|
+
return new HSLColor(normalizeHue(h), clamp(s, 0, 100), clamp(l, 0, 100), clamp(a ?? 1, 0, 1));
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
toString(format = "css") {
|
|
589
|
+
switch (format) {
|
|
590
|
+
case "hex": return this.toRGB().toString("hex");
|
|
591
|
+
case "hexa": return this.toRGB().toString("hexa");
|
|
592
|
+
case "hsl": return `hsl(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.lightness, 2)}%${this.alpha !== 1 && this.alpha !== 100 ? ` / ${this.alpha}` : ""})`;
|
|
593
|
+
case "css":
|
|
594
|
+
case "hsla": return `hsla(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.lightness, 2)}% / ${this.alpha})`;
|
|
595
|
+
default: return this.toFormat(format).toString(format);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
toFormat(format) {
|
|
599
|
+
switch (format) {
|
|
600
|
+
case "hsl":
|
|
601
|
+
case "hsla": return this;
|
|
602
|
+
case "hsb":
|
|
603
|
+
case "hsba": return this.toHSB();
|
|
604
|
+
case "rgb":
|
|
605
|
+
case "rgba": return this.toRGB();
|
|
606
|
+
default: throw new Error(`Unsupported color conversion: hsl -> ${format}`);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* HSL → HSB conversion:
|
|
611
|
+
* B = L + S·min(L, 1 − L);
|
|
612
|
+
* S_hsb = 2·(1 − L/B) (0 when B = 0)
|
|
613
|
+
*/
|
|
614
|
+
toHSB() {
|
|
615
|
+
let saturation = this.saturation / 100;
|
|
616
|
+
const lightness = this.lightness / 100;
|
|
617
|
+
const brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
618
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
619
|
+
return new HSBColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* HSL → RGB using the smooth HSL formula.
|
|
623
|
+
* `a = S·min(L, 1−L)` is the chroma amplitude.
|
|
624
|
+
* `fn(n)` evaluates one channel:
|
|
625
|
+
* channel = L − a·clamp((n + H/30) mod 12 − 3, −1, 1)
|
|
626
|
+
* where n=0 → red, n=8 → green, n=4 → blue.
|
|
627
|
+
*/
|
|
628
|
+
toRGB() {
|
|
629
|
+
const hue = this.hue;
|
|
630
|
+
const saturation = this.saturation / 100;
|
|
631
|
+
const lightness = this.lightness / 100;
|
|
632
|
+
const a = saturation * Math.min(lightness, 1 - lightness);
|
|
633
|
+
const fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
634
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
635
|
+
}
|
|
636
|
+
clone() {
|
|
637
|
+
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
638
|
+
}
|
|
639
|
+
getChannelRange(channel) {
|
|
640
|
+
switch (channel) {
|
|
641
|
+
case "hue": return {
|
|
642
|
+
minValue: 0,
|
|
643
|
+
maxValue: 360,
|
|
644
|
+
step: 1,
|
|
645
|
+
pageSize: 15
|
|
646
|
+
};
|
|
647
|
+
case "saturation":
|
|
648
|
+
case "lightness": return {
|
|
649
|
+
minValue: 0,
|
|
650
|
+
maxValue: 100,
|
|
651
|
+
step: 1,
|
|
652
|
+
pageSize: 10
|
|
653
|
+
};
|
|
654
|
+
case "alpha": return {
|
|
655
|
+
minValue: 0,
|
|
656
|
+
maxValue: 1,
|
|
657
|
+
step: .01,
|
|
658
|
+
pageSize: .1
|
|
659
|
+
};
|
|
660
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
getChannelFormatOptions(channel) {
|
|
664
|
+
switch (channel) {
|
|
665
|
+
case "hue": return {
|
|
666
|
+
style: "unit",
|
|
667
|
+
unit: "degree",
|
|
668
|
+
unitDisplay: "narrow"
|
|
669
|
+
};
|
|
670
|
+
case "saturation":
|
|
671
|
+
case "lightness":
|
|
672
|
+
case "alpha": return {
|
|
673
|
+
style: "percent",
|
|
674
|
+
maximumFractionDigits: 2
|
|
675
|
+
};
|
|
676
|
+
default: throw new Error(`Unknown color channel: ${channel}`);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
formatChannelValue(channel) {
|
|
680
|
+
let value = this.getChannelValue(channel);
|
|
681
|
+
if (channel === "saturation" || channel === "lightness") value /= 100;
|
|
682
|
+
return formatNumber(value, this.getChannelFormatOptions(channel));
|
|
683
|
+
}
|
|
684
|
+
getColorSpace() {
|
|
685
|
+
return "hsl";
|
|
686
|
+
}
|
|
687
|
+
static colorChannels = [
|
|
688
|
+
"hue",
|
|
689
|
+
"saturation",
|
|
690
|
+
"lightness"
|
|
691
|
+
];
|
|
692
|
+
getColorChannels() {
|
|
693
|
+
return HSLColor.colorChannels;
|
|
694
|
+
}
|
|
695
|
+
};
|
|
791
696
|
/**
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
697
|
+
* Converts any {@link Color} to OKLCH `[L, C, H]` — a perceptually uniform space
|
|
698
|
+
* where L is perceived lightness (0–1), C is chroma (≥ 0), and H is a hue angle (0–360).
|
|
699
|
+
*
|
|
700
|
+
* Full pipeline: sRGB bytes → linear sRGB (gamma-expand) → CIE XYZ D65 → OKLab → OKLCH.
|
|
701
|
+
*
|
|
702
|
+
* @see https://www.w3.org/TR/css-color-4/#color-conversion-code
|
|
703
|
+
*/
|
|
704
|
+
function colorToOKLCH(color) {
|
|
705
|
+
const rgb = color.toFormat("rgb");
|
|
706
|
+
let red = rgb.getChannelValue("red") / 255;
|
|
707
|
+
let green = rgb.getChannelValue("green") / 255;
|
|
708
|
+
let blue = rgb.getChannelValue("blue") / 255;
|
|
709
|
+
[red, green, blue] = lin_sRGB(red, green, blue);
|
|
710
|
+
const [x, y, z] = lin_sRGB_to_XYZ(red, green, blue);
|
|
711
|
+
const [l, a, b] = XYZ_to_OKLab(x, y, z);
|
|
712
|
+
return OKLab_to_OKLCH(l, a, b);
|
|
808
713
|
}
|
|
809
714
|
/**
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
715
|
+
* Converts OKLab `(L, a, b)` to cylindrical OKLCH `(L, C, H)`.
|
|
716
|
+
* C = sqrt(a² + b²); H = atan2(b, a) in degrees, normalised to [0, 360).
|
|
717
|
+
*/
|
|
813
718
|
function OKLab_to_OKLCH(l, a, b) {
|
|
814
|
-
|
|
815
|
-
|
|
719
|
+
const hue = Math.atan2(b, a) * 180 / Math.PI;
|
|
720
|
+
return [
|
|
721
|
+
l,
|
|
722
|
+
Math.sqrt(a ** 2 + b ** 2),
|
|
723
|
+
hue >= 0 ? hue : hue + 360
|
|
724
|
+
];
|
|
816
725
|
}
|
|
817
726
|
/** Applies sRGB gamma expansion component-wise, converting display-encoded sRGB to linear light. */
|
|
818
727
|
function lin_sRGB(r, g, b) {
|
|
819
|
-
|
|
728
|
+
return [
|
|
729
|
+
lin_sRGB_component(r),
|
|
730
|
+
lin_sRGB_component(g),
|
|
731
|
+
lin_sRGB_component(b)
|
|
732
|
+
];
|
|
820
733
|
}
|
|
821
734
|
/**
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
735
|
+
* IEC 61966-2-1 sRGB gamma expansion (the inverse EOTF).
|
|
736
|
+
* Values in the linear region (|v| ≤ 0.04045) are divided by 12.92.
|
|
737
|
+
* Larger values follow the power curve: sign(v) · ((|v| + 0.055) / 1.055)^2.4.
|
|
738
|
+
*/
|
|
826
739
|
function lin_sRGB_component(val) {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
return sign * ((abs + 0.055) / 1.055) ** 2.4;
|
|
740
|
+
const sign = val < 0 ? -1 : 1;
|
|
741
|
+
const abs = Math.abs(val);
|
|
742
|
+
if (abs <= .04045) return val / 12.92;
|
|
743
|
+
return sign * ((abs + .055) / 1.055) ** 2.4;
|
|
832
744
|
}
|
|
833
745
|
/**
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
746
|
+
* Transforms linear sRGB to CIE XYZ adapted to the D65 white point.
|
|
747
|
+
* The matrix coefficients are expressed as exact rational fractions (as in the CSS Color 4
|
|
748
|
+
* spec) to avoid floating-point rounding accumulation that can occur with decimal literals.
|
|
749
|
+
*/
|
|
838
750
|
function lin_sRGB_to_XYZ(r, g, b) {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
return multiplyMatrix(M, r, g, b);
|
|
751
|
+
return multiplyMatrix([
|
|
752
|
+
506752 / 1228815,
|
|
753
|
+
87881 / 245763,
|
|
754
|
+
12673 / 70218,
|
|
755
|
+
87098 / 409605,
|
|
756
|
+
175762 / 245763,
|
|
757
|
+
12673 / 175545,
|
|
758
|
+
7918 / 409605,
|
|
759
|
+
87881 / 737289,
|
|
760
|
+
1001167 / 1053270
|
|
761
|
+
], r, g, b);
|
|
851
762
|
}
|
|
852
763
|
/**
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
764
|
+
* Converts CIE XYZ to OKLab via Björn Ottosson's two-step LMS approach:
|
|
765
|
+
* 1. XYZ → LMS using a linear matrix (XYZtoLMS).
|
|
766
|
+
* 2. Cube-root each LMS component (perceptual non-linearity).
|
|
767
|
+
* 3. LMS^(1/3) → OKLab using a second linear matrix (LMStoOKLab).
|
|
768
|
+
*
|
|
769
|
+
* The result is a perceptually uniform Lab space where equal Euclidean distances
|
|
770
|
+
* correspond to equal perceived color differences.
|
|
771
|
+
*
|
|
772
|
+
* @see https://bottosson.github.io/posts/oklab/
|
|
773
|
+
*/
|
|
863
774
|
function XYZ_to_OKLab(x, y, z) {
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
775
|
+
const XYZtoLMS = [
|
|
776
|
+
.819022437996703,
|
|
777
|
+
.3619062600528904,
|
|
778
|
+
-.1288737815209879,
|
|
779
|
+
.0329836539323885,
|
|
780
|
+
.9292868615863434,
|
|
781
|
+
.0361446663506424,
|
|
782
|
+
.0481771893596242,
|
|
783
|
+
.2642395317527308,
|
|
784
|
+
.6335478284694309
|
|
785
|
+
];
|
|
786
|
+
const LMStoOKLab = [
|
|
787
|
+
.210454268309314,
|
|
788
|
+
.7936177747023054,
|
|
789
|
+
-.0040720430116193,
|
|
790
|
+
1.9779985324311684,
|
|
791
|
+
-2.42859224204858,
|
|
792
|
+
.450593709617411,
|
|
793
|
+
.0259040424655478,
|
|
794
|
+
.7827717124575296,
|
|
795
|
+
-.8086757549230774
|
|
796
|
+
];
|
|
797
|
+
const [a, b, c] = multiplyMatrix(XYZtoLMS, x, y, z);
|
|
798
|
+
return multiplyMatrix(LMStoOKLab, Math.cbrt(a), Math.cbrt(b), Math.cbrt(c));
|
|
876
799
|
}
|
|
877
800
|
/**
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
801
|
+
* Multiplies a flat row-major 3×3 matrix `m` by the column vector `[x, y, z]`.
|
|
802
|
+
* Layout: `m = [m00, m01, m02, m10, m11, m12, m20, m21, m22]`.
|
|
803
|
+
*/
|
|
881
804
|
function multiplyMatrix(m, x, y, z) {
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
805
|
+
return [
|
|
806
|
+
m[0] * x + m[1] * y + m[2] * z,
|
|
807
|
+
m[3] * x + m[4] * y + m[5] * z,
|
|
808
|
+
m[6] * x + m[7] * y + m[8] * z
|
|
809
|
+
];
|
|
887
810
|
}
|
|
888
811
|
/**
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
812
|
+
* Rounds `value` to `digits` decimal places in `base`.
|
|
813
|
+
* Uses integer scaling instead of `toFixed` to avoid floating-point string round-trips.
|
|
814
|
+
*/
|
|
892
815
|
function toFixedNumber(value, digits, base = 10) {
|
|
893
|
-
|
|
894
|
-
|
|
816
|
+
const pow = base ** digits;
|
|
817
|
+
return Math.round(value * pow) / pow;
|
|
895
818
|
}
|
|
819
|
+
//#endregion
|
|
820
|
+
export { colorToOKLCH, getColorChannels, normalizeColor, normalizeHue, parseColor };
|