@zag-js/color-utils 1.34.1 → 1.35.1

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 (42) hide show
  1. package/dist/area-gradient.d.mts +15 -0
  2. package/dist/area-gradient.d.ts +15 -0
  3. package/dist/area-gradient.js +87 -0
  4. package/dist/area-gradient.mjs +74 -0
  5. package/dist/chunk-QZ7TP4HQ.mjs +7 -0
  6. package/dist/color-format-gradient.d.mts +68 -0
  7. package/dist/color-format-gradient.d.ts +68 -0
  8. package/dist/color-format-gradient.js +164 -0
  9. package/dist/color-format-gradient.mjs +133 -0
  10. package/dist/color.d.mts +23 -0
  11. package/dist/color.d.ts +23 -0
  12. package/dist/color.js +87 -0
  13. package/dist/color.mjs +64 -0
  14. package/dist/hsb-color.d.mts +35 -0
  15. package/dist/hsb-color.d.ts +35 -0
  16. package/dist/hsb-color.js +165 -0
  17. package/dist/hsb-color.mjs +142 -0
  18. package/dist/hsl-color.d.mts +36 -0
  19. package/dist/hsl-color.d.ts +36 -0
  20. package/dist/hsl-color.js +167 -0
  21. package/dist/hsl-color.mjs +143 -0
  22. package/dist/index.d.mts +4 -121
  23. package/dist/index.d.ts +4 -121
  24. package/dist/index.js +37 -721
  25. package/dist/index.mjs +11 -716
  26. package/dist/native-color.d.mts +3 -0
  27. package/dist/native-color.d.ts +3 -0
  28. package/dist/native-color.js +41 -0
  29. package/dist/native-color.mjs +18 -0
  30. package/dist/parse-color.d.mts +6 -0
  31. package/dist/parse-color.d.ts +6 -0
  32. package/dist/parse-color.js +50 -0
  33. package/dist/parse-color.mjs +26 -0
  34. package/dist/rgb-color.d.mts +36 -0
  35. package/dist/rgb-color.d.ts +36 -0
  36. package/dist/rgb-color.js +213 -0
  37. package/dist/rgb-color.mjs +190 -0
  38. package/dist/types.d.mts +87 -0
  39. package/dist/types.d.ts +87 -0
  40. package/dist/types.js +18 -0
  41. package/dist/types.mjs +0 -0
  42. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,722 +1,38 @@
1
- 'use strict';
2
-
3
- var utils = require('@zag-js/utils');
4
-
1
+ "use strict";
5
2
  var __defProp = Object.defineProperty;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
8
-
9
- // src/color-format-gradient.ts
10
- var generateRGB_R = (orientation, dir, zValue) => {
11
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
12
- const result = {
13
- areaStyles: {
14
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,0),rgb(${zValue},255,0))`
15
- },
16
- areaGradientStyles: {
17
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,255),rgb(${zValue},255,255))`,
18
- WebkitMaskImage: maskImage,
19
- maskImage
20
- }
21
- };
22
- return result;
23
- };
24
- var generateRGB_G = (orientation, dir, zValue) => {
25
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
26
- const result = {
27
- areaStyles: {
28
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},0),rgb(255,${zValue},0))`
29
- },
30
- areaGradientStyles: {
31
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},255),rgb(255,${zValue},255))`,
32
- WebkitMaskImage: maskImage,
33
- maskImage
34
- }
35
- };
36
- return result;
37
- };
38
- var generateRGB_B = (orientation, dir, zValue) => {
39
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
40
- const result = {
41
- areaStyles: {
42
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,0,${zValue}),rgb(255,0,${zValue}))`
43
- },
44
- areaGradientStyles: {
45
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,255,${zValue}),rgb(255,255,${zValue}))`,
46
- WebkitMaskImage: maskImage,
47
- maskImage
48
- }
49
- };
50
- return result;
51
- };
52
- var generateHSL_H = (orientation, dir, zValue) => {
53
- const result = {
54
- areaStyles: {},
55
- areaGradientStyles: {
56
- background: [
57
- `linear-gradient(to ${orientation[Number(dir)]}, hsla(0,0%,0%,1) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,1) 100%)`,
58
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,50%),hsla(0,0%,50%,0))`,
59
- `hsl(${zValue}, 100%, 50%)`
60
- ].join(",")
61
- }
62
- };
63
- return result;
64
- };
65
- var generateHSL_S = (orientation, dir, alphaValue) => {
66
- const result = {
67
- areaStyles: {},
68
- areaGradientStyles: {
69
- background: [
70
- `linear-gradient(to ${orientation[Number(!dir)]}, hsla(0,0%,0%,${alphaValue}) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,${alphaValue}) 100%)`,
71
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
72
- "hsl(0, 0%, 50%)"
73
- ].join(",")
74
- }
75
- };
76
- return result;
77
- };
78
- var generateHSL_L = (orientation, dir, zValue) => {
79
- const result = {
80
- areaStyles: {},
81
- areaGradientStyles: {
82
- backgroundImage: [
83
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,${zValue}%),hsla(0,0%,${zValue}%,0))`,
84
- `linear-gradient(to ${orientation[Number(dir)]},hsl(0,100%,${zValue}%),hsl(60,100%,${zValue}%),hsl(120,100%,${zValue}%),hsl(180,100%,${zValue}%),hsl(240,100%,${zValue}%),hsl(300,100%,${zValue}%),hsl(360,100%,${zValue}%))`
85
- ].join(",")
86
- }
87
- };
88
- return result;
89
- };
90
- var generateHSB_H = (orientation, dir, zValue) => {
91
- const result = {
92
- areaStyles: {},
93
- areaGradientStyles: {
94
- background: [
95
- `linear-gradient(to ${orientation[Number(dir)]},hsl(0,0%,0%),hsla(0,0%,0%,0))`,
96
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,100%),hsla(0,0%,100%,0))`,
97
- `hsl(${zValue}, 100%, 50%)`
98
- ].join(",")
99
- }
100
- };
101
- return result;
102
- };
103
- var generateHSB_S = (orientation, dir, alphaValue) => {
104
- const result = {
105
- areaStyles: {},
106
- areaGradientStyles: {
107
- background: [
108
- `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,0%,${alphaValue}),hsla(0,0%,0%,0))`,
109
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
110
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,0%),hsl(0,0%,100%))`
111
- ].join(",")
112
- }
113
- };
114
- return result;
115
- };
116
- var generateHSB_B = (orientation, dir, alphaValue) => {
117
- const result = {
118
- areaStyles: {},
119
- areaGradientStyles: {
120
- background: [
121
- `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,100%,${alphaValue}),hsla(0,0%,100%,0))`,
122
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
123
- "#000"
124
- ].join(",")
125
- }
126
- };
127
- return result;
128
- };
129
-
130
- // src/area-gradient.ts
131
- function getColorAreaGradient(color, options) {
132
- const { xChannel, yChannel, dir: dirProp = "ltr" } = options;
133
- const { zChannel } = color.getColorAxes({ xChannel, yChannel });
134
- const zValue = color.getChannelValue(zChannel);
135
- const { minValue: zMin, maxValue: zMax } = color.getChannelRange(zChannel);
136
- const orientation = ["top", dirProp === "rtl" ? "left" : "right"];
137
- let dir = false;
138
- let background = { areaStyles: {}, areaGradientStyles: {} };
139
- let alphaValue = (zValue - zMin) / (zMax - zMin);
140
- let isHSL = color.getFormat() === "hsla";
141
- switch (zChannel) {
142
- case "red": {
143
- dir = xChannel === "green";
144
- background = generateRGB_R(orientation, dir, zValue);
145
- break;
146
- }
147
- case "green": {
148
- dir = xChannel === "red";
149
- background = generateRGB_G(orientation, dir, zValue);
150
- break;
151
- }
152
- case "blue": {
153
- dir = xChannel === "red";
154
- background = generateRGB_B(orientation, dir, zValue);
155
- break;
156
- }
157
- case "hue": {
158
- dir = xChannel !== "saturation";
159
- if (isHSL) {
160
- background = generateHSL_H(orientation, dir, zValue);
161
- } else {
162
- background = generateHSB_H(orientation, dir, zValue);
163
- }
164
- break;
165
- }
166
- case "saturation": {
167
- dir = xChannel === "hue";
168
- if (isHSL) {
169
- background = generateHSL_S(orientation, dir, alphaValue);
170
- } else {
171
- background = generateHSB_S(orientation, dir, alphaValue);
172
- }
173
- break;
174
- }
175
- case "brightness": {
176
- dir = xChannel === "hue";
177
- background = generateHSB_B(orientation, dir, alphaValue);
178
- break;
179
- }
180
- case "lightness": {
181
- dir = xChannel === "hue";
182
- background = generateHSL_L(orientation, dir, zValue);
183
- break;
184
- }
185
- }
186
- return background;
187
- }
188
- var isEqualObject = (a, b) => {
189
- if (Object.keys(a).length !== Object.keys(b).length) return false;
190
- for (let key in a) if (a[key] !== b[key]) return false;
191
- return true;
192
- };
193
- var Color = class {
194
- toHexInt() {
195
- return this.toFormat("rgba").toHexInt();
196
- }
197
- getChannelValue(channel) {
198
- if (channel in this) return this[channel];
199
- throw new Error("Unsupported color channel: " + channel);
200
- }
201
- getChannelValuePercent(channel, valueToCheck) {
202
- const value = valueToCheck ?? this.getChannelValue(channel);
203
- const { minValue, maxValue } = this.getChannelRange(channel);
204
- return utils.getValuePercent(value, minValue, maxValue);
205
- }
206
- getChannelPercentValue(channel, percentToCheck) {
207
- const { minValue, maxValue, step } = this.getChannelRange(channel);
208
- const percentValue = utils.getPercentValue(percentToCheck, minValue, maxValue, step);
209
- return utils.snapValueToStep(percentValue, minValue, maxValue, step);
210
- }
211
- withChannelValue(channel, value) {
212
- const { minValue, maxValue } = this.getChannelRange(channel);
213
- if (channel in this) {
214
- let clone = this.clone();
215
- clone[channel] = utils.clampValue(value, minValue, maxValue);
216
- return clone;
217
- }
218
- throw new Error("Unsupported color channel: " + channel);
219
- }
220
- getColorAxes(xyChannels) {
221
- let { xChannel, yChannel } = xyChannels;
222
- let xCh = xChannel || this.getChannels().find((c) => c !== yChannel);
223
- let yCh = yChannel || this.getChannels().find((c) => c !== xCh);
224
- let zCh = this.getChannels().find((c) => c !== xCh && c !== yCh);
225
- return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
226
- }
227
- incrementChannel(channel, stepSize) {
228
- const { minValue, maxValue, step } = this.getChannelRange(channel);
229
- const value = utils.snapValueToStep(
230
- utils.clampValue(this.getChannelValue(channel) + stepSize, minValue, maxValue),
231
- minValue,
232
- maxValue,
233
- step
234
- );
235
- return this.withChannelValue(channel, value);
236
- }
237
- decrementChannel(channel, stepSize) {
238
- return this.incrementChannel(channel, -stepSize);
239
- }
240
- isEqual(color) {
241
- const isSame = isEqualObject(this.toJSON(), color.toJSON());
242
- return isSame && this.getChannelValue("alpha") === color.getChannelValue("alpha");
243
- }
244
- };
245
- var HEX_COLOR_REGEX = /^#[\da-f]+$/i;
246
- var RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
247
- var HEX_STARTING_REGEX = /[^#]/gi;
248
- var _RGBColor = class _RGBColor extends Color {
249
- constructor(red, green, blue, alpha) {
250
- super();
251
- this.red = red;
252
- this.green = green;
253
- this.blue = blue;
254
- this.alpha = alpha;
255
- }
256
- static parse(value) {
257
- let colors = [];
258
- if (HEX_COLOR_REGEX.test(value) && [4, 5, 7, 9].includes(value.length)) {
259
- const values = (value.length < 6 ? value.replace(HEX_STARTING_REGEX, "$&$&") : value).slice(1).split("");
260
- while (values.length > 0) {
261
- colors.push(parseInt(values.splice(0, 2).join(""), 16));
262
- }
263
- colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
264
- }
265
- const match = value.match(RGB_COLOR_REGEX);
266
- if (match?.[1]) {
267
- colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => utils.clampValue(num, 0, i < 3 ? 255 : 1));
268
- }
269
- return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
270
- }
271
- toString(format = "css") {
272
- switch (format) {
273
- case "hex":
274
- return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
275
- case "hexa":
276
- 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();
277
- case "rgb":
278
- return `rgb(${this.red}, ${this.green}, ${this.blue})`;
279
- case "css":
280
- case "rgba":
281
- return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
282
- case "hsl":
283
- return this.toHSL().toString("hsl");
284
- case "hsb":
285
- return this.toHSB().toString("hsb");
286
- default:
287
- return this.toFormat(format).toString(format);
288
- }
289
- }
290
- toFormat(format) {
291
- switch (format) {
292
- case "rgba":
293
- return this;
294
- case "hsba":
295
- return this.toHSB();
296
- case "hsla":
297
- return this.toHSL();
298
- default:
299
- throw new Error("Unsupported color conversion: rgb -> " + format);
300
- }
301
- }
302
- toHexInt() {
303
- return this.red << 16 | this.green << 8 | this.blue;
304
- }
305
- /**
306
- * Converts an RGB color value to HSB.
307
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
308
- * @returns An HSBColor object.
309
- */
310
- toHSB() {
311
- const red = this.red / 255;
312
- const green = this.green / 255;
313
- const blue = this.blue / 255;
314
- const min = Math.min(red, green, blue);
315
- const brightness = Math.max(red, green, blue);
316
- const chroma = brightness - min;
317
- const saturation = brightness === 0 ? 0 : chroma / brightness;
318
- let hue = 0;
319
- if (chroma !== 0) {
320
- switch (brightness) {
321
- case red:
322
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
323
- break;
324
- case green:
325
- hue = (blue - red) / chroma + 2;
326
- break;
327
- case blue:
328
- hue = (red - green) / chroma + 4;
329
- break;
330
- }
331
- hue /= 6;
332
- }
333
- return new HSBColor(
334
- utils.toFixedNumber(hue * 360, 2),
335
- utils.toFixedNumber(saturation * 100, 2),
336
- utils.toFixedNumber(brightness * 100, 2),
337
- utils.toFixedNumber(this.alpha, 2)
338
- );
339
- }
340
- /**
341
- * Converts an RGB color value to HSL.
342
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
343
- * @returns An HSLColor object.
344
- */
345
- toHSL() {
346
- const red = this.red / 255;
347
- const green = this.green / 255;
348
- const blue = this.blue / 255;
349
- const min = Math.min(red, green, blue);
350
- const max = Math.max(red, green, blue);
351
- const lightness = (max + min) / 2;
352
- const chroma = max - min;
353
- let hue = -1;
354
- let saturation = -1;
355
- if (chroma === 0) {
356
- hue = saturation = 0;
357
- } else {
358
- saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
359
- switch (max) {
360
- case red:
361
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
362
- break;
363
- case green:
364
- hue = (blue - red) / chroma + 2;
365
- break;
366
- case blue:
367
- hue = (red - green) / chroma + 4;
368
- break;
369
- }
370
- hue /= 6;
371
- }
372
- return new HSLColor(
373
- utils.toFixedNumber(hue * 360, 2),
374
- utils.toFixedNumber(saturation * 100, 2),
375
- utils.toFixedNumber(lightness * 100, 2),
376
- utils.toFixedNumber(this.alpha, 2)
377
- );
378
- }
379
- clone() {
380
- return new _RGBColor(this.red, this.green, this.blue, this.alpha);
381
- }
382
- getChannelFormatOptions(channel) {
383
- switch (channel) {
384
- case "red":
385
- case "green":
386
- case "blue":
387
- return { style: "decimal" };
388
- case "alpha":
389
- return { style: "percent" };
390
- default:
391
- throw new Error("Unknown color channel: " + channel);
392
- }
393
- }
394
- formatChannelValue(channel, locale) {
395
- let options = this.getChannelFormatOptions(channel);
396
- let value = this.getChannelValue(channel);
397
- return new Intl.NumberFormat(locale, options).format(value);
398
- }
399
- getChannelRange(channel) {
400
- switch (channel) {
401
- case "red":
402
- case "green":
403
- case "blue":
404
- return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
405
- case "alpha":
406
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
407
- default:
408
- throw new Error("Unknown color channel: " + channel);
409
- }
410
- }
411
- toJSON() {
412
- return { r: this.red, g: this.green, b: this.blue, a: this.alpha };
413
- }
414
- getFormat() {
415
- return "rgba";
416
- }
417
- getChannels() {
418
- return _RGBColor.colorChannels;
419
- }
420
- };
421
- __publicField(_RGBColor, "colorChannels", ["red", "green", "blue"]);
422
- var RGBColor = _RGBColor;
423
-
424
- // src/hsl-color.ts
425
- var HSL_REGEX = /hsl\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsla\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
426
- var _HSLColor = class _HSLColor extends Color {
427
- constructor(hue, saturation, lightness, alpha) {
428
- super();
429
- this.hue = hue;
430
- this.saturation = saturation;
431
- this.lightness = lightness;
432
- this.alpha = alpha;
433
- }
434
- static parse(value) {
435
- let m;
436
- if (m = value.match(HSL_REGEX)) {
437
- const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
438
- return new _HSLColor(utils.mod(h, 360), utils.clampValue(s, 0, 100), utils.clampValue(l, 0, 100), utils.clampValue(a ?? 1, 0, 1));
439
- }
440
- }
441
- toString(format = "css") {
442
- switch (format) {
443
- case "hex":
444
- return this.toRGB().toString("hex");
445
- case "hexa":
446
- return this.toRGB().toString("hexa");
447
- case "hsl":
448
- return `hsl(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.lightness, 2)}%)`;
449
- case "css":
450
- case "hsla":
451
- return `hsla(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
452
- case "hsb":
453
- return this.toHSB().toString("hsb");
454
- case "rgb":
455
- return this.toRGB().toString("rgb");
456
- default:
457
- return this.toFormat(format).toString(format);
458
- }
459
- }
460
- toFormat(format) {
461
- switch (format) {
462
- case "hsla":
463
- return this;
464
- case "hsba":
465
- return this.toHSB();
466
- case "rgba":
467
- return this.toRGB();
468
- default:
469
- throw new Error("Unsupported color conversion: hsl -> " + format);
470
- }
471
- }
472
- /**
473
- * Converts a HSL color to HSB.
474
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
475
- * @returns An HSBColor object.
476
- */
477
- toHSB() {
478
- let saturation = this.saturation / 100;
479
- let lightness = this.lightness / 100;
480
- let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
481
- saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
482
- return new HSBColor(
483
- utils.toFixedNumber(this.hue, 2),
484
- utils.toFixedNumber(saturation * 100, 2),
485
- utils.toFixedNumber(brightness * 100, 2),
486
- utils.toFixedNumber(this.alpha, 2)
487
- );
488
- }
489
- /**
490
- * Converts a HSL color to RGB.
491
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
492
- * @returns An RGBColor object.
493
- */
494
- toRGB() {
495
- let hue = this.hue;
496
- let saturation = this.saturation / 100;
497
- let lightness = this.lightness / 100;
498
- let a = saturation * Math.min(lightness, 1 - lightness);
499
- let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
500
- return new RGBColor(
501
- Math.round(fn(0) * 255),
502
- Math.round(fn(8) * 255),
503
- Math.round(fn(4) * 255),
504
- utils.toFixedNumber(this.alpha, 2)
505
- );
506
- }
507
- clone() {
508
- return new _HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
509
- }
510
- getChannelFormatOptions(channel) {
511
- switch (channel) {
512
- case "hue":
513
- return { style: "unit", unit: "degree", unitDisplay: "narrow" };
514
- case "saturation":
515
- case "lightness":
516
- case "alpha":
517
- return { style: "percent" };
518
- default:
519
- throw new Error("Unknown color channel: " + channel);
520
- }
521
- }
522
- formatChannelValue(channel, locale) {
523
- let options = this.getChannelFormatOptions(channel);
524
- let value = this.getChannelValue(channel);
525
- if (channel === "saturation" || channel === "lightness") {
526
- value /= 100;
527
- }
528
- return new Intl.NumberFormat(locale, options).format(value);
529
- }
530
- getChannelRange(channel) {
531
- switch (channel) {
532
- case "hue":
533
- return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
534
- case "saturation":
535
- case "lightness":
536
- return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
537
- case "alpha":
538
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
539
- default:
540
- throw new Error("Unknown color channel: " + channel);
541
- }
542
- }
543
- toJSON() {
544
- return { h: this.hue, s: this.saturation, l: this.lightness, a: this.alpha };
545
- }
546
- getFormat() {
547
- return "hsla";
548
- }
549
- getChannels() {
550
- return _HSLColor.colorChannels;
551
- }
552
- };
553
- __publicField(_HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
554
- var HSLColor = _HSLColor;
555
-
556
- // src/hsb-color.ts
557
- var HSB_REGEX = /hsb\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsba\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
558
- var _HSBColor = class _HSBColor extends Color {
559
- constructor(hue, saturation, brightness, alpha) {
560
- super();
561
- this.hue = hue;
562
- this.saturation = saturation;
563
- this.brightness = brightness;
564
- this.alpha = alpha;
565
- }
566
- static parse(value) {
567
- let m;
568
- if (m = value.match(HSB_REGEX)) {
569
- const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
570
- return new _HSBColor(utils.mod(h, 360), utils.clampValue(s, 0, 100), utils.clampValue(b, 0, 100), utils.clampValue(a ?? 1, 0, 1));
571
- }
572
- }
573
- toString(format = "css") {
574
- switch (format) {
575
- case "css":
576
- return this.toHSL().toString("css");
577
- case "hex":
578
- return this.toRGB().toString("hex");
579
- case "hexa":
580
- return this.toRGB().toString("hexa");
581
- case "hsb":
582
- return `hsb(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.brightness, 2)}%)`;
583
- case "hsba":
584
- return `hsba(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
585
- case "hsl":
586
- return this.toHSL().toString("hsl");
587
- case "rgb":
588
- return this.toRGB().toString("rgb");
589
- default:
590
- return this.toFormat(format).toString(format);
591
- }
592
- }
593
- toFormat(format) {
594
- switch (format) {
595
- case "hsba":
596
- return this;
597
- case "hsla":
598
- return this.toHSL();
599
- case "rgba":
600
- return this.toRGB();
601
- default:
602
- throw new Error("Unsupported color conversion: hsb -> " + format);
603
- }
604
- }
605
- /**
606
- * Converts a HSB color to HSL.
607
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
608
- * @returns An HSLColor object.
609
- */
610
- toHSL() {
611
- let saturation = this.saturation / 100;
612
- let brightness = this.brightness / 100;
613
- let lightness = brightness * (1 - saturation / 2);
614
- saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
615
- return new HSLColor(
616
- utils.toFixedNumber(this.hue, 2),
617
- utils.toFixedNumber(saturation * 100, 2),
618
- utils.toFixedNumber(lightness * 100, 2),
619
- utils.toFixedNumber(this.alpha, 2)
620
- );
621
- }
622
- /**
623
- * Converts a HSV color value to RGB.
624
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
625
- * @returns An RGBColor object.
626
- */
627
- toRGB() {
628
- let hue = this.hue;
629
- let saturation = this.saturation / 100;
630
- let brightness = this.brightness / 100;
631
- let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
632
- return new RGBColor(
633
- Math.round(fn(5) * 255),
634
- Math.round(fn(3) * 255),
635
- Math.round(fn(1) * 255),
636
- utils.toFixedNumber(this.alpha, 2)
637
- );
638
- }
639
- clone() {
640
- return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
641
- }
642
- getChannelFormatOptions(channel) {
643
- switch (channel) {
644
- case "hue":
645
- return { style: "unit", unit: "degree", unitDisplay: "narrow" };
646
- case "saturation":
647
- case "brightness":
648
- case "alpha":
649
- return { style: "percent" };
650
- default:
651
- throw new Error("Unknown color channel: " + channel);
652
- }
653
- }
654
- formatChannelValue(channel, locale) {
655
- let options = this.getChannelFormatOptions(channel);
656
- let value = this.getChannelValue(channel);
657
- if (channel === "saturation" || channel === "brightness") {
658
- value /= 100;
659
- }
660
- return new Intl.NumberFormat(locale, options).format(value);
661
- }
662
- getChannelRange(channel) {
663
- switch (channel) {
664
- case "hue":
665
- return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
666
- case "saturation":
667
- case "brightness":
668
- return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
669
- case "alpha":
670
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
671
- default:
672
- throw new Error("Unknown color channel: " + channel);
673
- }
674
- }
675
- toJSON() {
676
- return { h: this.hue, s: this.saturation, b: this.brightness, a: this.alpha };
677
- }
678
- getFormat() {
679
- return "hsba";
680
- }
681
- getChannels() {
682
- return _HSBColor.colorChannels;
683
- }
684
- };
685
- __publicField(_HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
686
- var HSBColor = _HSBColor;
687
-
688
- // src/native-color.ts
689
- var nativeColors = "aliceblue:f0f8ff,antiquewhite:faebd7,aqua:00ffff,aquamarine:7fffd4,azure:f0ffff,beige:f5f5dc,bisque:ffe4c4,black:000000,blanchedalmond:ffebcd,blue:0000ff,blueviolet:8a2be2,brown:a52a2a,burlywood:deb887,cadetblue:5f9ea0,chartreuse:7fff00,chocolate:d2691e,coral:ff7f50,cornflowerblue:6495ed,cornsilk:fff8dc,crimson:dc143c,cyan:00ffff,darkblue:00008b,darkcyan:008b8b,darkgoldenrod:b8860b,darkgray:a9a9a9,darkgreen:006400,darkkhaki:bdb76b,darkmagenta:8b008b,darkolivegreen:556b2f,darkorange:ff8c00,darkorchid:9932cc,darkred:8b0000,darksalmon:e9967a,darkseagreen:8fbc8f,darkslateblue:483d8b,darkslategray:2f4f4f,darkturquoise:00ced1,darkviolet:9400d3,deeppink:ff1493,deepskyblue:00bfff,dimgray:696969,dodgerblue:1e90ff,firebrick:b22222,floralwhite:fffaf0,forestgreen:228b22,fuchsia:ff00ff,gainsboro:dcdcdc,ghostwhite:f8f8ff,gold:ffd700,goldenrod:daa520,gray:808080,green:008000,greenyellow:adff2f,honeydew:f0fff0,hotpink:ff69b4,indianred:cd5c5c,indigo:4b0082,ivory:fffff0,khaki:f0e68c,lavender:e6e6fa,lavenderblush:fff0f5,lawngreen:7cfc00,lemonchiffon:fffacd,lightblue:add8e6,lightcoral:f08080,lightcyan:e0ffff,lightgoldenrodyellow:fafad2,lightgrey:d3d3d3,lightgreen:90ee90,lightpink:ffb6c1,lightsalmon:ffa07a,lightseagreen:20b2aa,lightskyblue:87cefa,lightslategray:778899,lightsteelblue:b0c4de,lightyellow:ffffe0,lime:00ff00,limegreen:32cd32,linen:faf0e6,magenta:ff00ff,maroon:800000,mediumaquamarine:66cdaa,mediumblue:0000cd,mediumorchid:ba55d3,mediumpurple:9370d8,mediumseagreen:3cb371,mediumslateblue:7b68ee,mediumspringgreen:00fa9a,mediumturquoise:48d1cc,mediumvioletred:c71585,midnightblue:191970,mintcream:f5fffa,mistyrose:ffe4e1,moccasin:ffe4b5,navajowhite:ffdead,navy:000080,oldlace:fdf5e6,olive:808000,olivedrab:6b8e23,orange:ffa500,orangered:ff4500,orchid:da70d6,palegoldenrod:eee8aa,palegreen:98fb98,paleturquoise:afeeee,palevioletred:d87093,papayawhip:ffefd5,peachpuff:ffdab9,peru:cd853f,pink:ffc0cb,plum:dda0dd,powderblue:b0e0e6,purple:800080,rebeccapurple:663399,red:ff0000,rosybrown:bc8f8f,royalblue:4169e1,saddlebrown:8b4513,salmon:fa8072,sandybrown:f4a460,seagreen:2e8b57,seashell:fff5ee,sienna:a0522d,silver:c0c0c0,skyblue:87ceeb,slateblue:6a5acd,slategray:708090,snow:fffafa,springgreen:00ff7f,steelblue:4682b4,tan:d2b48c,teal:008080,thistle:d8bfd8,tomato:ff6347,turquoise:40e0d0,violet:ee82ee,wheat:f5deb3,white:ffffff,whitesmoke:f5f5f5,yellow:ffff00,yellowgreen:9acd32";
690
- var makeMap = (str) => {
691
- const map = /* @__PURE__ */ new Map();
692
- const list = str.split(",");
693
- for (let i = 0; i < list.length; i++) {
694
- const [key, val] = list[i].split(":");
695
- map.set(key, `#${val}`);
696
- if (key.includes("gray")) map.set(key.replace("gray", "grey"), `#${val}`);
697
- }
698
- return map;
699
- };
700
- var nativeColorMap = makeMap(nativeColors);
701
-
702
- // src/parse-color.ts
703
- var parseColor = (value) => {
704
- if (nativeColorMap.has(value)) {
705
- return parseColor(nativeColorMap.get(value));
706
- }
707
- const result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
708
- if (!result) {
709
- const error = new Error("Invalid color value: " + value);
710
- Error.captureStackTrace?.(error, parseColor);
711
- throw error;
712
- }
713
- return result;
714
- };
715
- var normalizeColor = (v) => {
716
- return typeof v === "string" ? parseColor(v) : v;
717
- };
718
-
719
- exports.Color = Color;
720
- exports.getColorAreaGradient = getColorAreaGradient;
721
- exports.normalizeColor = normalizeColor;
722
- exports.parseColor = parseColor;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Color: () => import_color.Color,
24
+ getColorAreaGradient: () => import_area_gradient.getColorAreaGradient,
25
+ normalizeColor: () => import_parse_color.normalizeColor,
26
+ parseColor: () => import_parse_color.parseColor
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_area_gradient = require("./area-gradient.cjs");
30
+ var import_color = require("./color.cjs");
31
+ var import_parse_color = require("./parse-color.cjs");
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ Color,
35
+ getColorAreaGradient,
36
+ normalizeColor,
37
+ parseColor
38
+ });