@zag-js/color-utils 2.0.0-next.3 → 2.0.0-next.4
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/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.js +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPrimitive.js +13 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPrimitive.mjs +13 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPropertyKey.js +8 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPropertyKey.mjs +8 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/typeof.js +15 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/typeof.mjs +10 -0
- package/dist/area-gradient.d.mts +9 -11
- package/dist/area-gradient.d.ts +9 -11
- package/dist/area-gradient.js +58 -89
- package/dist/area-gradient.mjs +54 -77
- package/dist/color-format-gradient.d.mts +76 -78
- package/dist/color-format-gradient.d.ts +76 -78
- package/dist/color-format-gradient.js +118 -185
- package/dist/color-format-gradient.mjs +104 -152
- package/dist/color.d.mts +20 -23
- package/dist/color.d.ts +20 -23
- package/dist/color.js +57 -83
- package/dist/color.mjs +53 -60
- package/dist/gamut.d.mts +2 -5
- package/dist/gamut.d.ts +2 -5
- package/dist/gamut.js +12 -34
- package/dist/gamut.mjs +8 -11
- package/dist/hsb-color.d.mts +37 -40
- package/dist/hsb-color.d.ts +37 -40
- package/dist/hsb-color.js +164 -200
- package/dist/hsb-color.mjs +158 -175
- package/dist/hsl-color.d.mts +33 -36
- package/dist/hsl-color.d.ts +33 -36
- package/dist/hsl-color.js +139 -168
- package/dist/hsl-color.mjs +132 -142
- package/dist/index.d.mts +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.js +23 -60
- package/dist/index.mjs +2 -28
- package/dist/native-color.d.mts +1 -3
- package/dist/native-color.d.ts +1 -3
- package/dist/native-color.js +16 -39
- package/dist/native-color.mjs +12 -16
- package/dist/oklab-color.d.mts +57 -60
- package/dist/oklab-color.d.ts +57 -60
- package/dist/oklab-color.js +363 -379
- package/dist/oklab-color.mjs +353 -350
- package/dist/oklab-math.d.mts +26 -28
- package/dist/oklab-math.d.ts +26 -28
- package/dist/oklab-math.js +123 -142
- package/dist/oklab-math.mjs +108 -108
- package/dist/parse-color.d.mts +3 -6
- package/dist/parse-color.d.ts +3 -6
- package/dist/parse-color.js +22 -48
- package/dist/parse-color.mjs +13 -20
- package/dist/rgb-color.d.mts +33 -36
- package/dist/rgb-color.d.ts +33 -36
- package/dist/rgb-color.js +182 -220
- package/dist/rgb-color.mjs +174 -193
- package/dist/types.d.mts +82 -84
- package/dist/types.d.ts +82 -84
- package/dist/types.js +4 -18
- package/dist/types.mjs +1 -0
- package/package.json +3 -3
- package/dist/chunk-QZ7TP4HQ.mjs +0 -7
package/dist/oklab-color.mjs
CHANGED
|
@@ -1,370 +1,373 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "./chunk-QZ7TP4HQ.mjs";
|
|
4
|
-
|
|
5
|
-
// src/oklab-color.ts
|
|
6
|
-
import { clampValue, mod, toFixedNumber } from "@zag-js/utils";
|
|
7
1
|
import { Color } from "./color.mjs";
|
|
8
2
|
import { HSBColor } from "./hsb-color.mjs";
|
|
9
3
|
import { RGBColor } from "./rgb-color.mjs";
|
|
10
4
|
import { linearChannelToSrgb, linearRgbToSrgb, oklabToLinearRgb, oklabToOklch, oklchToOklab } from "./oklab-math.mjs";
|
|
5
|
+
import _defineProperty from "./_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.mjs";
|
|
6
|
+
import { clampValue, mod, toFixedNumber } from "@zag-js/utils";
|
|
7
|
+
// src/oklab-color.ts
|
|
8
|
+
/** Convert oklab → HSB via float sRGB (avoids integer 0-255 precision loss). */
|
|
11
9
|
function oklabToHsb(L, a, b, alpha) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
linearChannelToSrgb(lin.r),
|
|
15
|
-
linearChannelToSrgb(lin.g),
|
|
16
|
-
linearChannelToSrgb(lin.b),
|
|
17
|
-
alpha
|
|
18
|
-
);
|
|
10
|
+
const lin = oklabToLinearRgb(L, a, b);
|
|
11
|
+
return HSBColor.fromSrgbFloat(linearChannelToSrgb(lin.r), linearChannelToSrgb(lin.g), linearChannelToSrgb(lin.b), alpha);
|
|
19
12
|
}
|
|
20
|
-
|
|
13
|
+
const OKLAB_HEAD = /^oklab\(\s*/i;
|
|
21
14
|
function parseCssNumber(token) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
const t = token.trim().toLowerCase();
|
|
16
|
+
if (t === "none") return void 0;
|
|
17
|
+
if (t.endsWith("%")) return Number.parseFloat(t) / 100;
|
|
18
|
+
const n = Number.parseFloat(t);
|
|
19
|
+
return Number.isFinite(n) ? n : void 0;
|
|
27
20
|
}
|
|
21
|
+
/** Parse `oklab(L a b)` / `oklab(L a b / alpha)` body (inside parentheses). */
|
|
28
22
|
function parseOklabParams(inner) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
const [main, alphaPart] = inner.split(/\s*\/\s*/);
|
|
24
|
+
const tokens = main.trim().split(/\s+/).filter(Boolean);
|
|
25
|
+
if (tokens.length < 3) return;
|
|
26
|
+
const L = parseCssNumber(tokens[0]);
|
|
27
|
+
const a = parseCssNumber(tokens[1]);
|
|
28
|
+
const b = parseCssNumber(tokens[2]);
|
|
29
|
+
if (L === void 0 || a === void 0 || b === void 0) return;
|
|
30
|
+
let alpha = 1;
|
|
31
|
+
if (alphaPart) {
|
|
32
|
+
const at = alphaPart.trim().toLowerCase();
|
|
33
|
+
if (at !== "none") {
|
|
34
|
+
const av = at.endsWith("%") ? Number.parseFloat(at) / 100 : Number.parseFloat(at);
|
|
35
|
+
if (Number.isFinite(av)) alpha = clampValue(av, 0, 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
L: clampValue(L, 0, 1),
|
|
40
|
+
a,
|
|
41
|
+
b,
|
|
42
|
+
alpha
|
|
43
|
+
};
|
|
45
44
|
}
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
isEqual(color) {
|
|
173
|
-
if (color.getFormat() !== "oklab") return super.isEqual(color);
|
|
174
|
-
const o = color;
|
|
175
|
-
return this.lightness === o.lightness && this.a === o.a && this.b === o.b && this.alpha === o.alpha;
|
|
176
|
-
}
|
|
45
|
+
var OklabColor = class OklabColor extends Color {
|
|
46
|
+
constructor(lightness, a, b, alpha) {
|
|
47
|
+
super();
|
|
48
|
+
this.lightness = lightness;
|
|
49
|
+
this.a = a;
|
|
50
|
+
this.b = b;
|
|
51
|
+
this.alpha = alpha;
|
|
52
|
+
}
|
|
53
|
+
static parse(value) {
|
|
54
|
+
if (!OKLAB_HEAD.test(value)) return;
|
|
55
|
+
const end = value.lastIndexOf(")");
|
|
56
|
+
if (end < 0) return;
|
|
57
|
+
const p = parseOklabParams(value.slice(value.indexOf("(") + 1, end));
|
|
58
|
+
if (!p) return;
|
|
59
|
+
return new OklabColor(p.L, p.a, p.b, p.alpha);
|
|
60
|
+
}
|
|
61
|
+
toString(format = "css") {
|
|
62
|
+
switch (format) {
|
|
63
|
+
case "hex": return this.toFormat("rgba").toString("hex");
|
|
64
|
+
case "hexa": return this.toFormat("rgba").toString("hexa");
|
|
65
|
+
case "oklab": return formatOklabCss(this.lightness, this.a, this.b, this.alpha);
|
|
66
|
+
case "css": return formatOklabCss(this.lightness, this.a, this.b, this.alpha);
|
|
67
|
+
default: return this.toFormat(format).toString(format);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
toFormat(format) {
|
|
71
|
+
switch (format) {
|
|
72
|
+
case "oklab": return this;
|
|
73
|
+
case "oklch": return this.toOklch();
|
|
74
|
+
case "rgba": return this.toRgb();
|
|
75
|
+
case "hsla": return this.toRgb().toFormat("hsla");
|
|
76
|
+
case "hsba": return oklabToHsb(this.lightness, this.a, this.b, this.alpha);
|
|
77
|
+
default: throw new Error("Unsupported color conversion: oklab -> " + format);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
toRgb() {
|
|
81
|
+
const { r, g, b } = linearRgbToSrgb(oklabToLinearRgb(this.lightness, this.a, this.b));
|
|
82
|
+
return new RGBColor(r, g, b, toFixedNumber(this.alpha, 4));
|
|
83
|
+
}
|
|
84
|
+
toOklch() {
|
|
85
|
+
const { L, C, H } = oklabToOklch(this.lightness, this.a, this.b);
|
|
86
|
+
return new OklchColor(L, H, C, toFixedNumber(this.alpha, 4));
|
|
87
|
+
}
|
|
88
|
+
clone() {
|
|
89
|
+
return new OklabColor(this.lightness, this.a, this.b, this.alpha);
|
|
90
|
+
}
|
|
91
|
+
getChannelValue(channel) {
|
|
92
|
+
switch (channel) {
|
|
93
|
+
case "lightness": return this.lightness;
|
|
94
|
+
case "a": return this.a;
|
|
95
|
+
case "b": return this.b;
|
|
96
|
+
case "alpha": return this.alpha;
|
|
97
|
+
default: throw new Error("Unsupported color channel: " + channel);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
withChannelValue(channel, value) {
|
|
101
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
102
|
+
const v = clampValue(value, minValue, maxValue);
|
|
103
|
+
switch (channel) {
|
|
104
|
+
case "lightness": return new OklabColor(v, this.a, this.b, this.alpha);
|
|
105
|
+
case "a": return new OklabColor(this.lightness, v, this.b, this.alpha);
|
|
106
|
+
case "b": return new OklabColor(this.lightness, this.a, v, this.alpha);
|
|
107
|
+
case "alpha": return new OklabColor(this.lightness, this.a, this.b, v);
|
|
108
|
+
default: throw new Error("Unsupported color channel: " + channel);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
formatChannelValue(channel, locale) {
|
|
112
|
+
const options = this.getChannelFormatOptions(channel);
|
|
113
|
+
const val = this.getChannelValue(channel);
|
|
114
|
+
return new Intl.NumberFormat(locale, options).format(val);
|
|
115
|
+
}
|
|
116
|
+
getChannelFormatOptions(channel) {
|
|
117
|
+
switch (channel) {
|
|
118
|
+
case "lightness":
|
|
119
|
+
case "a":
|
|
120
|
+
case "b":
|
|
121
|
+
case "alpha": return {
|
|
122
|
+
style: "decimal",
|
|
123
|
+
maximumFractionDigits: 4
|
|
124
|
+
};
|
|
125
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
getChannelRange(channel) {
|
|
129
|
+
switch (channel) {
|
|
130
|
+
case "lightness": return {
|
|
131
|
+
minValue: 0,
|
|
132
|
+
maxValue: 1,
|
|
133
|
+
step: .001,
|
|
134
|
+
pageSize: .05
|
|
135
|
+
};
|
|
136
|
+
case "a":
|
|
137
|
+
case "b": return {
|
|
138
|
+
minValue: -.4,
|
|
139
|
+
maxValue: .4,
|
|
140
|
+
step: .001,
|
|
141
|
+
pageSize: .02
|
|
142
|
+
};
|
|
143
|
+
case "alpha": return {
|
|
144
|
+
minValue: 0,
|
|
145
|
+
maxValue: 1,
|
|
146
|
+
step: .01,
|
|
147
|
+
pageSize: .1
|
|
148
|
+
};
|
|
149
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
toJSON() {
|
|
153
|
+
return {
|
|
154
|
+
l: this.lightness,
|
|
155
|
+
a: this.a,
|
|
156
|
+
b: this.b,
|
|
157
|
+
alpha: this.alpha
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
getFormat() {
|
|
161
|
+
return "oklab";
|
|
162
|
+
}
|
|
163
|
+
getChannels() {
|
|
164
|
+
return OklabColor.colorChannels;
|
|
165
|
+
}
|
|
166
|
+
isEqual(color) {
|
|
167
|
+
if (color.getFormat() !== "oklab") return super.isEqual(color);
|
|
168
|
+
const o = color;
|
|
169
|
+
return this.lightness === o.lightness && this.a === o.a && this.b === o.b && this.alpha === o.alpha;
|
|
170
|
+
}
|
|
177
171
|
};
|
|
178
|
-
|
|
179
|
-
|
|
172
|
+
_defineProperty(OklabColor, "colorChannels", [
|
|
173
|
+
"lightness",
|
|
174
|
+
"a",
|
|
175
|
+
"b"
|
|
176
|
+
]);
|
|
180
177
|
function formatOklabCss(L, a, b, alpha) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
178
|
+
const l = toFixedNumber(L, 4);
|
|
179
|
+
const aa = toFixedNumber(a, 4);
|
|
180
|
+
const bb = toFixedNumber(b, 4);
|
|
181
|
+
if (alpha >= 1) return `oklab(${l} ${aa} ${bb})`;
|
|
182
|
+
return `oklab(${l} ${aa} ${bb} / ${toFixedNumber(alpha, 4)})`;
|
|
186
183
|
}
|
|
187
|
-
|
|
184
|
+
const OKLCH_HEAD = /^oklch\(\s*/i;
|
|
188
185
|
function parseCssNumberOrAngle(token) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
186
|
+
const t = token.trim().toLowerCase();
|
|
187
|
+
if (t === "none") return void 0;
|
|
188
|
+
if (t.endsWith("deg")) return Number.parseFloat(t);
|
|
189
|
+
if (t.endsWith("turn")) return Number.parseFloat(t) * 360;
|
|
190
|
+
if (t.endsWith("grad")) return Number.parseFloat(t) / 400 * 360;
|
|
191
|
+
if (t.endsWith("rad")) return Number.parseFloat(t) * 180 / Math.PI;
|
|
192
|
+
if (t.endsWith("%")) return Number.parseFloat(t) / 100;
|
|
193
|
+
const n = Number.parseFloat(t);
|
|
194
|
+
return Number.isFinite(n) ? n : void 0;
|
|
198
195
|
}
|
|
199
196
|
function parseOklchParams(inner) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
197
|
+
const [main, alphaPart] = inner.split(/\s*\/\s*/);
|
|
198
|
+
const tokens = main.trim().split(/\s+/).filter(Boolean);
|
|
199
|
+
if (tokens.length < 3) return;
|
|
200
|
+
const Lraw = parseCssNumberOrAngle(tokens[0]);
|
|
201
|
+
const Craw = parseCssNumberOrAngle(tokens[1]);
|
|
202
|
+
const Hraw = parseCssNumberOrAngle(tokens[2]);
|
|
203
|
+
if (Lraw === void 0 || Craw === void 0 || Hraw === void 0) return;
|
|
204
|
+
const L = tokens[0].trim().endsWith("%") ? Lraw : clampValue(Lraw, 0, 1);
|
|
205
|
+
const C = tokens[1].trim().endsWith("%") ? Craw * .5 : Craw;
|
|
206
|
+
const H = mod(Hraw, 360);
|
|
207
|
+
let alpha = 1;
|
|
208
|
+
if (alphaPart) {
|
|
209
|
+
const at = alphaPart.trim().toLowerCase();
|
|
210
|
+
if (at !== "none") {
|
|
211
|
+
const av = at.endsWith("%") ? Number.parseFloat(at) / 100 : Number.parseFloat(at);
|
|
212
|
+
if (Number.isFinite(av)) alpha = clampValue(av, 0, 1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
L,
|
|
217
|
+
C: clampValue(C, 0, .5),
|
|
218
|
+
H,
|
|
219
|
+
alpha
|
|
220
|
+
};
|
|
219
221
|
}
|
|
220
|
-
var
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
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
|
-
|
|
222
|
+
var OklchColor = class OklchColor extends Color {
|
|
223
|
+
constructor(lightness, hue, chroma, alpha) {
|
|
224
|
+
super();
|
|
225
|
+
this.lightness = lightness;
|
|
226
|
+
this.hue = hue;
|
|
227
|
+
this.chroma = chroma;
|
|
228
|
+
this.alpha = alpha;
|
|
229
|
+
}
|
|
230
|
+
static parse(value) {
|
|
231
|
+
if (!OKLCH_HEAD.test(value)) return;
|
|
232
|
+
const end = value.lastIndexOf(")");
|
|
233
|
+
if (end < 0) return;
|
|
234
|
+
const p = parseOklchParams(value.slice(value.indexOf("(") + 1, end));
|
|
235
|
+
if (!p) return;
|
|
236
|
+
return new OklchColor(p.L, p.H, p.C, p.alpha);
|
|
237
|
+
}
|
|
238
|
+
toString(format = "css") {
|
|
239
|
+
switch (format) {
|
|
240
|
+
case "hex": return this.toFormat("rgba").toString("hex");
|
|
241
|
+
case "hexa": return this.toFormat("rgba").toString("hexa");
|
|
242
|
+
case "oklch": return formatOklchCss(this.lightness, this.chroma, this.hue, this.alpha);
|
|
243
|
+
case "css": return formatOklchCss(this.lightness, this.chroma, this.hue, this.alpha);
|
|
244
|
+
default: return this.toFormat(format).toString(format);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
toFormat(format) {
|
|
248
|
+
switch (format) {
|
|
249
|
+
case "oklch": return this;
|
|
250
|
+
case "oklab": return this.toOklab();
|
|
251
|
+
case "rgba": return this.toRgb();
|
|
252
|
+
case "hsla": return this.toRgb().toFormat("hsla");
|
|
253
|
+
case "hsba": {
|
|
254
|
+
const { a, b, L } = oklchToOklab(this.lightness, this.chroma, this.hue);
|
|
255
|
+
return oklabToHsb(L, a, b, this.alpha);
|
|
256
|
+
}
|
|
257
|
+
default: throw new Error("Unsupported color conversion: oklch -> " + format);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
toOklab() {
|
|
261
|
+
const { a, b, L } = oklchToOklab(this.lightness, this.chroma, this.hue);
|
|
262
|
+
return new OklabColor(L, a, b, toFixedNumber(this.alpha, 4));
|
|
263
|
+
}
|
|
264
|
+
toRgb() {
|
|
265
|
+
return this.toOklab().toFormat("rgba");
|
|
266
|
+
}
|
|
267
|
+
clone() {
|
|
268
|
+
return new OklchColor(this.lightness, this.hue, this.chroma, this.alpha);
|
|
269
|
+
}
|
|
270
|
+
getChannelValue(channel) {
|
|
271
|
+
switch (channel) {
|
|
272
|
+
case "lightness": return this.lightness;
|
|
273
|
+
case "hue": return this.hue;
|
|
274
|
+
case "chroma": return this.chroma;
|
|
275
|
+
case "alpha": return this.alpha;
|
|
276
|
+
default: throw new Error("Unsupported color channel: " + channel);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
withChannelValue(channel, value) {
|
|
280
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
281
|
+
let v = clampValue(value, minValue, maxValue);
|
|
282
|
+
if (channel === "hue") v = mod(v, 360);
|
|
283
|
+
switch (channel) {
|
|
284
|
+
case "lightness": return new OklchColor(v, this.hue, this.chroma, this.alpha);
|
|
285
|
+
case "hue": return new OklchColor(this.lightness, v, this.chroma, this.alpha);
|
|
286
|
+
case "chroma": return new OklchColor(this.lightness, this.hue, v, this.alpha);
|
|
287
|
+
case "alpha": return new OklchColor(this.lightness, this.hue, this.chroma, v);
|
|
288
|
+
default: throw new Error("Unsupported color channel: " + channel);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
formatChannelValue(channel, locale) {
|
|
292
|
+
const options = this.getChannelFormatOptions(channel);
|
|
293
|
+
const val = this.getChannelValue(channel);
|
|
294
|
+
return new Intl.NumberFormat(locale, options).format(val);
|
|
295
|
+
}
|
|
296
|
+
getChannelFormatOptions(channel) {
|
|
297
|
+
switch (channel) {
|
|
298
|
+
case "lightness":
|
|
299
|
+
case "chroma":
|
|
300
|
+
case "alpha": return {
|
|
301
|
+
style: "decimal",
|
|
302
|
+
maximumFractionDigits: 4
|
|
303
|
+
};
|
|
304
|
+
case "hue": return {
|
|
305
|
+
style: "unit",
|
|
306
|
+
unit: "degree",
|
|
307
|
+
unitDisplay: "narrow"
|
|
308
|
+
};
|
|
309
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
getChannelRange(channel) {
|
|
313
|
+
switch (channel) {
|
|
314
|
+
case "lightness": return {
|
|
315
|
+
minValue: 0,
|
|
316
|
+
maxValue: 1,
|
|
317
|
+
step: .001,
|
|
318
|
+
pageSize: .05
|
|
319
|
+
};
|
|
320
|
+
case "hue": return {
|
|
321
|
+
minValue: 0,
|
|
322
|
+
maxValue: 360,
|
|
323
|
+
step: .01,
|
|
324
|
+
pageSize: 15
|
|
325
|
+
};
|
|
326
|
+
case "chroma": return {
|
|
327
|
+
minValue: 0,
|
|
328
|
+
maxValue: .5,
|
|
329
|
+
step: .001,
|
|
330
|
+
pageSize: .02
|
|
331
|
+
};
|
|
332
|
+
case "alpha": return {
|
|
333
|
+
minValue: 0,
|
|
334
|
+
maxValue: 1,
|
|
335
|
+
step: .01,
|
|
336
|
+
pageSize: .1
|
|
337
|
+
};
|
|
338
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
toJSON() {
|
|
342
|
+
return {
|
|
343
|
+
l: this.lightness,
|
|
344
|
+
h: this.hue,
|
|
345
|
+
c: this.chroma,
|
|
346
|
+
alpha: this.alpha
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
getFormat() {
|
|
350
|
+
return "oklch";
|
|
351
|
+
}
|
|
352
|
+
getChannels() {
|
|
353
|
+
return OklchColor.colorChannels;
|
|
354
|
+
}
|
|
355
|
+
isEqual(color) {
|
|
356
|
+
if (color.getFormat() !== "oklch") return super.isEqual(color);
|
|
357
|
+
const o = color;
|
|
358
|
+
return this.lightness === o.lightness && this.hue === o.hue && this.chroma === o.chroma && this.alpha === o.alpha;
|
|
359
|
+
}
|
|
355
360
|
};
|
|
356
|
-
|
|
357
|
-
|
|
361
|
+
_defineProperty(OklchColor, "colorChannels", [
|
|
362
|
+
"lightness",
|
|
363
|
+
"hue",
|
|
364
|
+
"chroma"
|
|
365
|
+
]);
|
|
358
366
|
function formatOklchCss(L, C, H, alpha) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
367
|
+
const l = toFixedNumber(L, 4);
|
|
368
|
+
const c = toFixedNumber(C, 4);
|
|
369
|
+
const h = toFixedNumber(H, 2);
|
|
370
|
+
if (alpha >= 1) return `oklch(${l} ${c} ${h})`;
|
|
371
|
+
return `oklch(${l} ${c} ${h} / ${toFixedNumber(alpha, 4)})`;
|
|
364
372
|
}
|
|
365
|
-
export {
|
|
366
|
-
OklabColor,
|
|
367
|
-
OklchColor,
|
|
368
|
-
parseOklabParams,
|
|
369
|
-
parseOklchParams
|
|
370
|
-
};
|
|
373
|
+
export { OklabColor, OklchColor, parseOklabParams, parseOklchParams };
|