@zag-js/color-utils 0.10.1 → 0.10.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.
@@ -1,444 +1,13 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
20
- var __publicField = (obj, key, value) => {
21
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
- return value;
23
- };
24
-
25
- // src/parse-color.ts
26
- var parse_color_exports = {};
27
- __export(parse_color_exports, {
28
- normalizeColor: () => normalizeColor,
29
- parseColor: () => parseColor
30
- });
31
- module.exports = __toCommonJS(parse_color_exports);
1
+ 'use strict';
32
2
 
33
- // src/color.ts
34
- var Color = class {
35
- toHexInt() {
36
- return this.toFormat("rgb").toHexInt();
37
- }
38
- getChannelValue(channel) {
39
- if (channel in this) {
40
- return this[channel];
41
- }
42
- throw new Error("Unsupported color channel: " + channel);
43
- }
44
- withChannelValue(channel, value) {
45
- if (channel in this) {
46
- let clone = this.clone();
47
- clone[channel] = value;
48
- return clone;
49
- }
50
- throw new Error("Unsupported color channel: " + channel);
51
- }
52
- getColorSpaceAxes(xyChannels) {
53
- let { xChannel, yChannel } = xyChannels;
54
- let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
55
- let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
56
- let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
57
- return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
58
- }
59
- isEqual(color) {
60
- return this.toHexInt() === color.toHexInt();
61
- }
62
- };
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
63
4
 
64
- // src/utils.ts
65
- function mod(n, m) {
66
- return (n % m + m) % m;
67
- }
68
- function toFixedNumber(num, digits) {
69
- return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
70
- }
71
- function clampValue(value, min, max) {
72
- return Math.min(Math.max(value, min), max);
73
- }
5
+ const hsbColor = require('./hsb-color.js');
6
+ const hslColor = require('./hsl-color.js');
7
+ const rgbColor = require('./rgb-color.js');
74
8
 
75
- // src/rgb-color.ts
76
- var _RGBColor = class extends Color {
77
- constructor(red, green, blue, alpha) {
78
- super();
79
- this.red = red;
80
- this.green = green;
81
- this.blue = blue;
82
- this.alpha = alpha;
83
- }
84
- static parse(value) {
85
- let colors = [];
86
- if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
87
- const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
88
- while (values.length > 0) {
89
- colors.push(parseInt(values.splice(0, 2).join(""), 16));
90
- }
91
- colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
92
- }
93
- const match = value.match(/^rgba?\((.*)\)$/);
94
- if (match?.[1]) {
95
- colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
96
- }
97
- return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
98
- }
99
- toString(format) {
100
- switch (format) {
101
- case "hex":
102
- return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
103
- case "hexa":
104
- 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();
105
- case "rgb":
106
- return `rgb(${this.red}, ${this.green}, ${this.blue})`;
107
- case "css":
108
- case "rgba":
109
- return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
110
- default:
111
- return this.toFormat(format).toString(format);
112
- }
113
- }
114
- toFormat(format) {
115
- switch (format) {
116
- case "hex":
117
- case "hexa":
118
- case "rgb":
119
- case "rgba":
120
- return this;
121
- case "hsb":
122
- case "hsba":
123
- return this.toHSB();
124
- case "hsl":
125
- case "hsla":
126
- return this.toHSL();
127
- default:
128
- throw new Error("Unsupported color conversion: rgb -> " + format);
129
- }
130
- }
131
- toHexInt() {
132
- return this.red << 16 | this.green << 8 | this.blue;
133
- }
134
- /**
135
- * Converts an RGB color value to HSB.
136
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
137
- * @returns An HSBColor object.
138
- */
139
- toHSB() {
140
- const red = this.red / 255;
141
- const green = this.green / 255;
142
- const blue = this.blue / 255;
143
- const min = Math.min(red, green, blue);
144
- const brightness = Math.max(red, green, blue);
145
- const chroma = brightness - min;
146
- const saturation = brightness === 0 ? 0 : chroma / brightness;
147
- let hue = 0;
148
- if (chroma !== 0) {
149
- switch (brightness) {
150
- case red:
151
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
152
- break;
153
- case green:
154
- hue = (blue - red) / chroma + 2;
155
- break;
156
- case blue:
157
- hue = (red - green) / chroma + 4;
158
- break;
159
- }
160
- hue /= 6;
161
- }
162
- return new HSBColor(
163
- toFixedNumber(hue * 360, 2),
164
- toFixedNumber(saturation * 100, 2),
165
- toFixedNumber(brightness * 100, 2),
166
- this.alpha
167
- );
168
- }
169
- /**
170
- * Converts an RGB color value to HSL.
171
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
172
- * @returns An HSLColor object.
173
- */
174
- toHSL() {
175
- const red = this.red / 255;
176
- const green = this.green / 255;
177
- const blue = this.blue / 255;
178
- const min = Math.min(red, green, blue);
179
- const max = Math.max(red, green, blue);
180
- const lightness = (max + min) / 2;
181
- const chroma = max - min;
182
- let hue = -1;
183
- let saturation = -1;
184
- if (chroma === 0) {
185
- hue = saturation = 0;
186
- } else {
187
- saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
188
- switch (max) {
189
- case red:
190
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
191
- break;
192
- case green:
193
- hue = (blue - red) / chroma + 2;
194
- break;
195
- case blue:
196
- hue = (red - green) / chroma + 4;
197
- break;
198
- }
199
- hue /= 6;
200
- }
201
- return new HSLColor(
202
- toFixedNumber(hue * 360, 2),
203
- toFixedNumber(saturation * 100, 2),
204
- toFixedNumber(lightness * 100, 2),
205
- this.alpha
206
- );
207
- }
208
- clone() {
209
- return new _RGBColor(this.red, this.green, this.blue, this.alpha);
210
- }
211
- getChannelRange(channel) {
212
- switch (channel) {
213
- case "red":
214
- case "green":
215
- case "blue":
216
- return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
217
- case "alpha":
218
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
219
- default:
220
- throw new Error("Unknown color channel: " + channel);
221
- }
222
- }
223
- getColorSpace() {
224
- return "rgb";
225
- }
226
- getColorChannels() {
227
- return _RGBColor.colorChannels;
228
- }
229
- };
230
- var RGBColor = _RGBColor;
231
- __publicField(RGBColor, "colorChannels", ["red", "green", "blue"]);
232
-
233
- // src/hsl-color.ts
234
- 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+)?)\)/;
235
- var _HSLColor = class extends Color {
236
- constructor(hue, saturation, lightness, alpha) {
237
- super();
238
- this.hue = hue;
239
- this.saturation = saturation;
240
- this.lightness = lightness;
241
- this.alpha = alpha;
242
- }
243
- static parse(value) {
244
- let m;
245
- if (m = value.match(HSL_REGEX)) {
246
- const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
247
- return new _HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
248
- }
249
- }
250
- toString(format) {
251
- switch (format) {
252
- case "hex":
253
- return this.toRGB().toString("hex");
254
- case "hexa":
255
- return this.toRGB().toString("hexa");
256
- case "hsl":
257
- return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
258
- case "css":
259
- case "hsla":
260
- return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
261
- default:
262
- return this.toFormat(format).toString(format);
263
- }
264
- }
265
- toFormat(format) {
266
- switch (format) {
267
- case "hsl":
268
- case "hsla":
269
- return this;
270
- case "hsb":
271
- case "hsba":
272
- return this.toHSB();
273
- case "rgb":
274
- case "rgba":
275
- return this.toRGB();
276
- default:
277
- throw new Error("Unsupported color conversion: hsl -> " + format);
278
- }
279
- }
280
- /**
281
- * Converts a HSL color to HSB.
282
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
283
- * @returns An HSBColor object.
284
- */
285
- toHSB() {
286
- let saturation = this.saturation / 100;
287
- let lightness = this.lightness / 100;
288
- let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
289
- saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
290
- return new HSBColor(
291
- toFixedNumber(this.hue, 2),
292
- toFixedNumber(saturation * 100, 2),
293
- toFixedNumber(brightness * 100, 2),
294
- this.alpha
295
- );
296
- }
297
- /**
298
- * Converts a HSL color to RGB.
299
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
300
- * @returns An RGBColor object.
301
- */
302
- toRGB() {
303
- let hue = this.hue;
304
- let saturation = this.saturation / 100;
305
- let lightness = this.lightness / 100;
306
- let a = saturation * Math.min(lightness, 1 - lightness);
307
- let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
308
- return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
309
- }
310
- clone() {
311
- return new _HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
312
- }
313
- getChannelRange(channel) {
314
- switch (channel) {
315
- case "hue":
316
- return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
317
- case "saturation":
318
- case "lightness":
319
- return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
320
- case "alpha":
321
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
322
- default:
323
- throw new Error("Unknown color channel: " + channel);
324
- }
325
- }
326
- getColorSpace() {
327
- return "hsl";
328
- }
329
- getColorChannels() {
330
- return _HSLColor.colorChannels;
331
- }
332
- };
333
- var HSLColor = _HSLColor;
334
- __publicField(HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
335
-
336
- // src/hsb-color.ts
337
- 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+)?)\)/;
338
- var _HSBColor = class extends Color {
339
- constructor(hue, saturation, brightness, alpha) {
340
- super();
341
- this.hue = hue;
342
- this.saturation = saturation;
343
- this.brightness = brightness;
344
- this.alpha = alpha;
345
- }
346
- static parse(value) {
347
- let m;
348
- if (m = value.match(HSB_REGEX)) {
349
- const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
350
- return new _HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
351
- }
352
- }
353
- toString(format) {
354
- switch (format) {
355
- case "css":
356
- return this.toHSL().toString("css");
357
- case "hex":
358
- return this.toRGB().toString("hex");
359
- case "hexa":
360
- return this.toRGB().toString("hexa");
361
- case "hsb":
362
- return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
363
- case "hsba":
364
- return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
365
- default:
366
- return this.toFormat(format).toString(format);
367
- }
368
- }
369
- toFormat(format) {
370
- switch (format) {
371
- case "hsb":
372
- case "hsba":
373
- return this;
374
- case "hsl":
375
- case "hsla":
376
- return this.toHSL();
377
- case "rgb":
378
- case "rgba":
379
- return this.toRGB();
380
- default:
381
- throw new Error("Unsupported color conversion: hsb -> " + format);
382
- }
383
- }
384
- /**
385
- * Converts a HSB color to HSL.
386
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
387
- * @returns An HSLColor object.
388
- */
389
- toHSL() {
390
- let saturation = this.saturation / 100;
391
- let brightness = this.brightness / 100;
392
- let lightness = brightness * (1 - saturation / 2);
393
- saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
394
- return new HSLColor(
395
- toFixedNumber(this.hue, 2),
396
- toFixedNumber(saturation * 100, 2),
397
- toFixedNumber(lightness * 100, 2),
398
- this.alpha
399
- );
400
- }
401
- /**
402
- * Converts a HSV color value to RGB.
403
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
404
- * @returns An RGBColor object.
405
- */
406
- toRGB() {
407
- let hue = this.hue;
408
- let saturation = this.saturation / 100;
409
- let brightness = this.brightness / 100;
410
- let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
411
- return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
412
- }
413
- clone() {
414
- return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
415
- }
416
- getChannelRange(channel) {
417
- switch (channel) {
418
- case "hue":
419
- return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
420
- case "saturation":
421
- case "brightness":
422
- return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
423
- case "alpha":
424
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
425
- default:
426
- throw new Error("Unknown color channel: " + channel);
427
- }
428
- }
429
- getColorSpace() {
430
- return "hsb";
431
- }
432
- getColorChannels() {
433
- return _HSBColor.colorChannels;
434
- }
435
- };
436
- var HSBColor = _HSBColor;
437
- __publicField(HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
438
-
439
- // src/parse-color.ts
440
9
  function parseColor(value) {
441
- let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
10
+ let result = rgbColor.RGBColor.parse(value) || hsbColor.HSBColor.parse(value) || hslColor.HSLColor.parse(value);
442
11
  if (!result) {
443
12
  throw new Error("Invalid color value: " + value);
444
13
  }
@@ -451,8 +20,6 @@ function normalizeColor(v) {
451
20
  return v;
452
21
  }
453
22
  }
454
- // Annotate the CommonJS export names for ESM import in node:
455
- 0 && (module.exports = {
456
- normalizeColor,
457
- parseColor
458
- });
23
+
24
+ exports.normalizeColor = normalizeColor;
25
+ exports.parseColor = parseColor;
@@ -1,12 +1,20 @@
1
- import {
2
- normalizeColor,
3
- parseColor
4
- } from "./chunk-WWI4IDFU.mjs";
5
- import "./chunk-7VB2GGWQ.mjs";
6
- import "./chunk-NSR5W5ST.mjs";
7
- import "./chunk-TXOVH6IE.mjs";
8
- import "./chunk-NHABU752.mjs";
9
- export {
10
- normalizeColor,
11
- parseColor
12
- };
1
+ import { HSBColor } from './hsb-color.mjs';
2
+ import { HSLColor } from './hsl-color.mjs';
3
+ import { RGBColor } from './rgb-color.mjs';
4
+
5
+ function parseColor(value) {
6
+ let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
7
+ if (!result) {
8
+ throw new Error("Invalid color value: " + value);
9
+ }
10
+ return result;
11
+ }
12
+ function normalizeColor(v) {
13
+ if (typeof v === "string") {
14
+ return parseColor(v);
15
+ } else {
16
+ return v;
17
+ }
18
+ }
19
+
20
+ export { normalizeColor, parseColor };
@@ -1,7 +1,6 @@
1
- import { Color } from './color.js';
2
- import { ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.js';
3
-
4
- declare class RGBColor extends Color {
1
+ import { Color } from "./color";
2
+ import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types";
3
+ export declare class RGBColor extends Color {
5
4
  private red;
6
5
  private green;
7
6
  private blue;
@@ -29,5 +28,3 @@ declare class RGBColor extends Color {
29
28
  private static colorChannels;
30
29
  getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
31
30
  }
32
-
33
- export { RGBColor };