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