@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
|
@@ -1,278 +1,251 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Portions of this file incorporate color science from the following sources:
|
|
3
|
-
*
|
|
4
|
-
* OKLab/OKLCH color space by Björn Ottosson.
|
|
5
|
-
* MIT License, Copyright Björn Ottosson.
|
|
6
|
-
* https://bottosson.github.io/posts/oklab/
|
|
7
|
-
*
|
|
8
|
-
* WCAG 2.1 relative luminance and contrast ratio formulas.
|
|
9
|
-
* W3C Web Content Accessibility Guidelines 2.1, Copyright W3C.
|
|
10
|
-
* https://www.w3.org/TR/WCAG21/#contrast-minimum
|
|
11
|
-
*/
|
|
1
|
+
/* @ts-self-types="./manipulation.d.ts" */
|
|
12
2
|
import { clamp } from "../index.js";
|
|
13
3
|
import { colorToOKLCH, normalizeHue, parseColor } from "./helpers.js";
|
|
14
|
-
|
|
4
|
+
//#region src/colors/manipulation.ts
|
|
15
5
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
6
|
+
* Like {@link parseColor} but returns `undefined` instead of throwing for invalid input.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* tryParseColor("#3264C8") // Color
|
|
11
|
+
* tryParseColor("nope") // undefined
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function tryParseColor(value) {
|
|
15
|
+
try {
|
|
16
|
+
return parseColor(value);
|
|
17
|
+
} catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
31
20
|
}
|
|
32
21
|
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
* Returns `true` if `value` is a parseable color string.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* isValidColor("#3264C8") // true
|
|
27
|
+
* isValidColor("nope") // false
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
function isValidColor(value) {
|
|
31
|
+
return tryParseColor(value) !== void 0;
|
|
43
32
|
}
|
|
44
33
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return /^rgba\(/i.test(s) ? "rgba" : "rgb";
|
|
70
|
-
if (/^hsla?\(/i.test(s))
|
|
71
|
-
return /^hsla\(/i.test(s) ? "hsla" : "hsl";
|
|
72
|
-
if (/^hsba?\(/i.test(s))
|
|
73
|
-
return /^hsba\(/i.test(s) ? "hsba" : "hsb";
|
|
74
|
-
return undefined;
|
|
34
|
+
* Detects the format of a color string without parsing it.
|
|
35
|
+
* Returns `undefined` for strings that do not match any known format.
|
|
36
|
+
*
|
|
37
|
+
* The returned format reflects the **syntax** of the string (e.g. `"rgb"` for
|
|
38
|
+
* `rgb(...)` even if an alpha channel is present via slash notation).
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* detectColorFormat("#3264C8") // "hex"
|
|
43
|
+
* detectColorFormat("#3264C8FF") // "hexa"
|
|
44
|
+
* detectColorFormat("rgb(50, 100, 200)") // "rgb"
|
|
45
|
+
* detectColorFormat("hsla(220, 60%, 49%, 0.5)") // "hsla"
|
|
46
|
+
* detectColorFormat("nope") // undefined
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
function detectColorFormat(value) {
|
|
50
|
+
const s = value.trim();
|
|
51
|
+
if (/^#[\da-f]+$/i.test(s)) {
|
|
52
|
+
if (s.length === 4 || s.length === 7) return "hex";
|
|
53
|
+
if (s.length === 5 || s.length === 9) return "hexa";
|
|
54
|
+
}
|
|
55
|
+
if (/^rgba?\(/i.test(s)) return /^rgba\(/i.test(s) ? "rgba" : "rgb";
|
|
56
|
+
if (/^hsla?\(/i.test(s)) return /^hsla\(/i.test(s) ? "hsla" : "hsl";
|
|
57
|
+
if (/^hsba?\(/i.test(s)) return /^hsba\(/i.test(s) ? "hsba" : "hsb";
|
|
75
58
|
}
|
|
76
|
-
// ─── Manipulation ─────────────────────────────────────────────────────────────
|
|
77
59
|
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
60
|
+
* Returns a new color with lightness increased by `amount` (0–1 ratio = percentage points).
|
|
61
|
+
* Operates in HSL space; the returned color is in HSL.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* lighten(parseColor("hsl(220, 60%, 50%)"), 0.1).getChannelValue("lightness") // 60
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
function lighten(color, amount) {
|
|
69
|
+
const hsl = color.toFormat("hsl");
|
|
70
|
+
return hsl.withChannelValue("lightness", clamp(hsl.getChannelValue("lightness") + amount * 100, 0, 100));
|
|
89
71
|
}
|
|
90
72
|
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
73
|
+
* Returns a new color with lightness decreased by `amount` (0–1 ratio = percentage points).
|
|
74
|
+
* Operates in HSL space; the returned color is in HSL.
|
|
75
|
+
*/
|
|
76
|
+
function darken(color, amount) {
|
|
77
|
+
return lighten(color, -amount);
|
|
96
78
|
}
|
|
97
79
|
/**
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
80
|
+
* Returns a new color with saturation increased by `amount` (0–1 ratio = percentage points).
|
|
81
|
+
* Operates in HSL space; the returned color is in HSL.
|
|
82
|
+
*/
|
|
83
|
+
function saturate(color, amount) {
|
|
84
|
+
const hsl = color.toFormat("hsl");
|
|
85
|
+
return hsl.withChannelValue("saturation", clamp(hsl.getChannelValue("saturation") + amount * 100, 0, 100));
|
|
104
86
|
}
|
|
105
87
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
88
|
+
* Returns a new color with saturation decreased by `amount` (0–1 ratio = percentage points).
|
|
89
|
+
* Operates in HSL space; the returned color is in HSL.
|
|
90
|
+
*/
|
|
91
|
+
function desaturate(color, amount) {
|
|
92
|
+
return saturate(color, -amount);
|
|
111
93
|
}
|
|
112
94
|
/**
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
95
|
+
* Returns the complementary color (hue rotated 180°), preserving saturation, lightness, and alpha.
|
|
96
|
+
* The returned color is in HSL.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* complement(parseColor("hsl(60, 50%, 50%)")).getChannelValue("hue") // 240
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
function complement(color) {
|
|
104
|
+
const hsl = color.toFormat("hsl");
|
|
105
|
+
return hsl.withChannelValue("hue", normalizeHue(hsl.getChannelValue("hue") + 180));
|
|
124
106
|
}
|
|
125
107
|
/**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
.withChannelValue("red", Math.round(lerp(ra.getChannelValue("red"), rb.getChannelValue("red"))))
|
|
142
|
-
.withChannelValue("green", Math.round(lerp(ra.getChannelValue("green"), rb.getChannelValue("green"))))
|
|
143
|
-
.withChannelValue("blue", Math.round(lerp(ra.getChannelValue("blue"), rb.getChannelValue("blue"))))
|
|
144
|
-
.withChannelValue("alpha", lerp(ra.getChannelValue("alpha"), rb.getChannelValue("alpha")));
|
|
108
|
+
* Mixes two colors in RGB space at the given `ratio` (0 = fully `a`, 1 = fully `b`).
|
|
109
|
+
* Alpha is also interpolated. The returned color is in RGB.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* mix(parseColor("#000000"), parseColor("#ffffff")).toString("hex") // "#808080"
|
|
114
|
+
* mix(parseColor("#000000"), parseColor("#ffffff"), 0.25).toString("hex") // "#404040"
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function mix(a, b, ratio = .5) {
|
|
118
|
+
const r = Number.isFinite(ratio) ? clamp(ratio, 0, 1) : .5;
|
|
119
|
+
const ra = a.toFormat("rgb");
|
|
120
|
+
const rb = b.toFormat("rgb");
|
|
121
|
+
const lerp = (x, y) => x + (y - x) * r;
|
|
122
|
+
return ra.withChannelValue("red", Math.round(lerp(ra.getChannelValue("red"), rb.getChannelValue("red")))).withChannelValue("green", Math.round(lerp(ra.getChannelValue("green"), rb.getChannelValue("green")))).withChannelValue("blue", Math.round(lerp(ra.getChannelValue("blue"), rb.getChannelValue("blue")))).withChannelValue("alpha", lerp(ra.getChannelValue("alpha"), rb.getChannelValue("alpha")));
|
|
145
123
|
}
|
|
146
|
-
// ─── Accessibility ────────────────────────────────────────────────────────────
|
|
147
124
|
/** WCAG 2.1 relative luminance. Alpha is ignored (assumes opaque rendering). */
|
|
148
125
|
function relativeLuminance(color) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
0.7152 * linearize(rgb.getChannelValue("green")) +
|
|
156
|
-
0.0722 * linearize(rgb.getChannelValue("blue")));
|
|
126
|
+
const rgb = color.toFormat("rgb");
|
|
127
|
+
const linearize = (v) => {
|
|
128
|
+
const c = v / 255;
|
|
129
|
+
return c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
|
|
130
|
+
};
|
|
131
|
+
return .2126 * linearize(rgb.getChannelValue("red")) + .7152 * linearize(rgb.getChannelValue("green")) + .0722 * linearize(rgb.getChannelValue("blue"));
|
|
157
132
|
}
|
|
158
133
|
/**
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
134
|
+
* Returns the WCAG 2.1 contrast ratio between two colors (1–21).
|
|
135
|
+
* The order of arguments does not matter.
|
|
136
|
+
*
|
|
137
|
+
* - Black on white: `21`
|
|
138
|
+
* - Same color: `1`
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* contrastRatio(parseColor("#000"), parseColor("#fff")) // 21
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
function contrastRatio(a, b) {
|
|
146
|
+
const l1 = relativeLuminance(a);
|
|
147
|
+
const l2 = relativeLuminance(b);
|
|
148
|
+
return (Math.max(l1, l2) + .05) / (Math.min(l1, l2) + .05);
|
|
174
149
|
}
|
|
175
150
|
/**
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return ratio >= (size === "large" ? 3 : 4.5);
|
|
151
|
+
* Returns whether the contrast between `foreground` and `background` meets the
|
|
152
|
+
* specified WCAG 2.1 conformance level and text size.
|
|
153
|
+
*
|
|
154
|
+
* | Level | Normal text | Large text (≥18pt / ≥14pt bold) |
|
|
155
|
+
* |-------|-------------|----------------------------------|
|
|
156
|
+
* | AA | 4.5 : 1 | 3 : 1 |
|
|
157
|
+
* | AAA | 7 : 1 | 4.5 : 1 |
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* isReadable(parseColor("#fff"), parseColor("#000")) // true (AA, normal)
|
|
162
|
+
* isReadable(parseColor("#fff"), parseColor("#000"), "AAA") // true
|
|
163
|
+
* isReadable(parseColor("#777"), parseColor("#fff"), "AAA") // false
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
function isReadable(foreground, background, level = "AA", size = "normal") {
|
|
167
|
+
const ratio = contrastRatio(foreground, background);
|
|
168
|
+
if (level === "AAA") return ratio >= (size === "large" ? 4.5 : 7);
|
|
169
|
+
return ratio >= (size === "large" ? 3 : 4.5);
|
|
196
170
|
}
|
|
197
|
-
// ─── Color scales ─────────────────────────────────────────────────────────────
|
|
198
171
|
/**
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
return Array.from({ length: steps }, (_, i) => mix(from, to, i / (steps - 1)));
|
|
172
|
+
* Returns an array of `steps` colors linearly interpolated in RGB space between
|
|
173
|
+
* `from` and `to`. The first element is `from`, the last is `to`.
|
|
174
|
+
*
|
|
175
|
+
* @throws if `steps` is not a finite integer >= 2.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* colorScale(parseColor("#000"), parseColor("#fff"), 3)
|
|
180
|
+
* // [#000000, #808080, #FFFFFF]
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
function colorScale(from, to, steps) {
|
|
184
|
+
if (!Number.isFinite(steps) || !Number.isInteger(steps) || steps < 2) throw new Error("colorScale: steps must be a finite integer >= 2");
|
|
185
|
+
return Array.from({ length: steps }, (_, i) => mix(from, to, i / (steps - 1)));
|
|
214
186
|
}
|
|
215
187
|
/** Interpolate between two OKLCH triplets, taking the shortest hue path. */
|
|
216
188
|
function lerpOKLCH([l1, c1, h1], [l2, c2, h2], t) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
189
|
+
let dh = h2 - h1;
|
|
190
|
+
if (dh > 180) dh -= 360;
|
|
191
|
+
if (dh < -180) dh += 360;
|
|
192
|
+
return [
|
|
193
|
+
l1 + (l2 - l1) * t,
|
|
194
|
+
c1 + (c2 - c1) * t,
|
|
195
|
+
normalizeHue(h1 + dh * t)
|
|
196
|
+
];
|
|
223
197
|
}
|
|
224
198
|
/**
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
199
|
+
* Convert OKLCH back to an RGB `Color` via the OKLab → LMS → linear sRGB → sRGB pipeline.
|
|
200
|
+
* Out-of-gamut values are clamped to [0, 1] before gamma encoding.
|
|
201
|
+
* Coefficients from https://bottosson.github.io/posts/oklab/
|
|
202
|
+
*/
|
|
229
203
|
function oklchToColor(l, c, h, alpha) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
};
|
|
248
|
-
return parseColor(`rgba(${toSRGB(r_lin)}, ${toSRGB(g_lin)}, ${toSRGB(b_lin)}, ${alpha})`);
|
|
204
|
+
const hRad = h * Math.PI / 180;
|
|
205
|
+
const a = c * Math.cos(hRad);
|
|
206
|
+
const b = c * Math.sin(hRad);
|
|
207
|
+
const l_ = l + .3963377774 * a + .2158037573 * b;
|
|
208
|
+
const m_ = l - .1055613458 * a - .0638541728 * b;
|
|
209
|
+
const s_ = l - .0894841775 * a - 1.291485548 * b;
|
|
210
|
+
const ll = l_ ** 3;
|
|
211
|
+
const mm = m_ ** 3;
|
|
212
|
+
const ss = s_ ** 3;
|
|
213
|
+
const r_lin = 4.0767416621 * ll - 3.3077115913 * mm + .2309699292 * ss;
|
|
214
|
+
const g_lin = -1.2684380046 * ll + 2.6097574011 * mm - .3413193965 * ss;
|
|
215
|
+
const b_lin = -.0041960863 * ll - .7034186147 * mm + 1.707614701 * ss;
|
|
216
|
+
const toSRGB = (v) => {
|
|
217
|
+
const clamped = clamp(v, 0, 1);
|
|
218
|
+
return Math.round((clamped <= .0031308 ? 12.92 * clamped : 1.055 * clamped ** (1 / 2.4) - .055) * 255);
|
|
219
|
+
};
|
|
220
|
+
return parseColor(`rgba(${toSRGB(r_lin)}, ${toSRGB(g_lin)}, ${toSRGB(b_lin)}, ${alpha})`);
|
|
249
221
|
}
|
|
250
222
|
/**
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
223
|
+
* Returns an array of `steps` colors interpolated in OKLCH space between `from` and `to`.
|
|
224
|
+
* OKLCH is a perceptually uniform color space, so the steps appear more evenly distributed
|
|
225
|
+
* to the human eye than a plain RGB scale.
|
|
226
|
+
*
|
|
227
|
+
* Hue interpolation takes the shortest arc around the color wheel.
|
|
228
|
+
* Alpha is linearly interpolated. The returned colors are in RGB.
|
|
229
|
+
*
|
|
230
|
+
* @throws if `steps` is not a finite integer >= 2.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* // A scale from red to blue that passes through purple rather than through green
|
|
235
|
+
* perceptualColorScale(parseColor("hsl(0, 80%, 50%)"), parseColor("hsl(240, 80%, 50%)"), 5)
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
function perceptualColorScale(from, to, steps) {
|
|
239
|
+
if (!Number.isFinite(steps) || !Number.isInteger(steps) || steps < 2) throw new Error("perceptualColorScale: steps must be a finite integer >= 2");
|
|
240
|
+
const fromOKLCH = colorToOKLCH(from);
|
|
241
|
+
const toOKLCH = colorToOKLCH(to);
|
|
242
|
+
const alphaFrom = from.getChannelValue("alpha");
|
|
243
|
+
const alphaTo = to.getChannelValue("alpha");
|
|
244
|
+
return Array.from({ length: steps }, (_, i) => {
|
|
245
|
+
const t = i / (steps - 1);
|
|
246
|
+
const [l, c, h] = lerpOKLCH(fromOKLCH, toOKLCH, t);
|
|
247
|
+
return oklchToColor(l, c, h, alphaFrom + (alphaTo - alphaFrom) * t);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
//#endregion
|
|
251
|
+
export { colorScale, complement, contrastRatio, darken, desaturate, detectColorFormat, isReadable, isValidColor, lighten, mix, perceptualColorScale, saturate, tryParseColor };
|