@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
package/dist/rgb-color.js CHANGED
@@ -1,222 +1,184 @@
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 = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
-
22
- // src/rgb-color.ts
23
- var rgb_color_exports = {};
24
- __export(rgb_color_exports, {
25
- RGBColor: () => RGBColor
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
26
4
  });
27
- module.exports = __toCommonJS(rgb_color_exports);
28
- var import_utils = require("@zag-js/utils");
29
- var import_color = require("./color.js");
30
- var import_hsb_color = require("./hsb-color.js");
31
- var import_hsl_color = require("./hsl-color.js");
32
- var import_oklab_math = require("./oklab-math.js");
33
- var import_oklab_color = require("./oklab-color.js");
34
- var HEX_COLOR_REGEX = /^#[\da-f]+$/i;
35
- var RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
36
- var HEX_STARTING_REGEX = /[^#]/gi;
37
- var _RGBColor = class _RGBColor extends import_color.Color {
38
- constructor(red, green, blue, alpha) {
39
- super();
40
- __publicField(this, "red", red);
41
- __publicField(this, "green", green);
42
- __publicField(this, "blue", blue);
43
- __publicField(this, "alpha", alpha);
44
- }
45
- static parse(value) {
46
- let colors = [];
47
- if (HEX_COLOR_REGEX.test(value) && [4, 5, 7, 9].includes(value.length)) {
48
- const values = (value.length < 6 ? value.replace(HEX_STARTING_REGEX, "$&$&") : value).slice(1).split("");
49
- while (values.length > 0) {
50
- colors.push(parseInt(values.splice(0, 2).join(""), 16));
51
- }
52
- colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
53
- }
54
- const match = value.match(RGB_COLOR_REGEX);
55
- if (match?.[1]) {
56
- colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => (0, import_utils.clampValue)(num, 0, i < 3 ? 255 : 1));
57
- }
58
- return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
59
- }
60
- toString(format = "css") {
61
- switch (format) {
62
- case "hex":
63
- return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
64
- case "hexa":
65
- 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();
66
- case "rgb":
67
- return `rgb(${this.red}, ${this.green}, ${this.blue})`;
68
- case "css":
69
- case "rgba":
70
- return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
71
- case "hsl":
72
- return this.toHSL().toString("hsl");
73
- case "hsb":
74
- return this.toHSB().toString("hsb");
75
- default:
76
- return this.toFormat(format).toString(format);
77
- }
78
- }
79
- toFormat(format) {
80
- switch (format) {
81
- case "rgba":
82
- return this;
83
- case "hsba":
84
- return this.toHSB();
85
- case "hsla":
86
- return this.toHSL();
87
- case "oklab": {
88
- const lin = (0, import_oklab_math.srgbToLinearRgb)(this.red, this.green, this.blue);
89
- const { L, a, b } = (0, import_oklab_math.linearRgbToOklab)(lin);
90
- return new import_oklab_color.OklabColor(L, a, b, (0, import_utils.toFixedNumber)(this.alpha, 4));
91
- }
92
- case "oklch":
93
- return this.toFormat("oklab").toFormat("oklch");
94
- default:
95
- throw new Error("Unsupported color conversion: rgb -> " + format);
96
- }
97
- }
98
- toHexInt() {
99
- return this.red << 16 | this.green << 8 | this.blue;
100
- }
101
- /**
102
- * Converts an RGB color value to HSB.
103
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
104
- * @returns An HSBColor object.
105
- */
106
- toHSB() {
107
- const red = this.red / 255;
108
- const green = this.green / 255;
109
- const blue = this.blue / 255;
110
- const min = Math.min(red, green, blue);
111
- const brightness = Math.max(red, green, blue);
112
- const chroma = brightness - min;
113
- const saturation = brightness === 0 ? 0 : chroma / brightness;
114
- let hue = 0;
115
- if (chroma !== 0) {
116
- switch (brightness) {
117
- case red:
118
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
119
- break;
120
- case green:
121
- hue = (blue - red) / chroma + 2;
122
- break;
123
- case blue:
124
- hue = (red - green) / chroma + 4;
125
- break;
126
- }
127
- hue /= 6;
128
- }
129
- return new import_hsb_color.HSBColor(
130
- (0, import_utils.toFixedNumber)(hue * 360, 2),
131
- (0, import_utils.toFixedNumber)(saturation * 100, 2),
132
- (0, import_utils.toFixedNumber)(brightness * 100, 2),
133
- (0, import_utils.toFixedNumber)(this.alpha, 2)
134
- );
135
- }
136
- /**
137
- * Converts an RGB color value to HSL.
138
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
139
- * @returns An HSLColor object.
140
- */
141
- toHSL() {
142
- const red = this.red / 255;
143
- const green = this.green / 255;
144
- const blue = this.blue / 255;
145
- const min = Math.min(red, green, blue);
146
- const max = Math.max(red, green, blue);
147
- const lightness = (max + min) / 2;
148
- const chroma = max - min;
149
- let hue = -1;
150
- let saturation = -1;
151
- if (chroma === 0) {
152
- hue = saturation = 0;
153
- } else {
154
- saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
155
- switch (max) {
156
- case red:
157
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
158
- break;
159
- case green:
160
- hue = (blue - red) / chroma + 2;
161
- break;
162
- case blue:
163
- hue = (red - green) / chroma + 4;
164
- break;
165
- }
166
- hue /= 6;
167
- }
168
- return new import_hsl_color.HSLColor(
169
- (0, import_utils.toFixedNumber)(hue * 360, 2),
170
- (0, import_utils.toFixedNumber)(saturation * 100, 2),
171
- (0, import_utils.toFixedNumber)(lightness * 100, 2),
172
- (0, import_utils.toFixedNumber)(this.alpha, 2)
173
- );
174
- }
175
- clone() {
176
- return new _RGBColor(this.red, this.green, this.blue, this.alpha);
177
- }
178
- getChannelFormatOptions(channel) {
179
- switch (channel) {
180
- case "red":
181
- case "green":
182
- case "blue":
183
- return { style: "decimal" };
184
- case "alpha":
185
- return { style: "percent" };
186
- default:
187
- throw new Error("Unknown color channel: " + channel);
188
- }
189
- }
190
- formatChannelValue(channel, locale) {
191
- let options = this.getChannelFormatOptions(channel);
192
- let value = this.getChannelValue(channel);
193
- return new Intl.NumberFormat(locale, options).format(value);
194
- }
195
- getChannelRange(channel) {
196
- switch (channel) {
197
- case "red":
198
- case "green":
199
- case "blue":
200
- return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
201
- case "alpha":
202
- return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
203
- default:
204
- throw new Error("Unknown color channel: " + channel);
205
- }
206
- }
207
- toJSON() {
208
- return { r: this.red, g: this.green, b: this.blue, a: this.alpha };
209
- }
210
- getFormat() {
211
- return "rgba";
212
- }
213
- getChannels() {
214
- return _RGBColor.colorChannels;
215
- }
5
+ const require_color = require("./color.js");
6
+ const require_hsb_color = require("./hsb-color.js");
7
+ const require_hsl_color = require("./hsl-color.js");
8
+ const require_oklab_math = require("./oklab-math.js");
9
+ const require_oklab_color = require("./oklab-color.js");
10
+ const require_defineProperty = require("./_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.js");
11
+ let _zag_js_utils = require("@zag-js/utils");
12
+ // src/rgb-color.ts
13
+ const HEX_COLOR_REGEX = /^#[\da-f]+$/i;
14
+ const RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
15
+ const HEX_STARTING_REGEX = /[^#]/gi;
16
+ var RGBColor = class RGBColor extends require_color.Color {
17
+ constructor(red, green, blue, alpha) {
18
+ super();
19
+ this.red = red;
20
+ this.green = green;
21
+ this.blue = blue;
22
+ this.alpha = alpha;
23
+ }
24
+ static parse(value) {
25
+ let colors = [];
26
+ if (HEX_COLOR_REGEX.test(value) && [
27
+ 4,
28
+ 5,
29
+ 7,
30
+ 9
31
+ ].includes(value.length)) {
32
+ const values = (value.length < 6 ? value.replace(HEX_STARTING_REGEX, "$&$&") : value).slice(1).split("");
33
+ while (values.length > 0) colors.push(parseInt(values.splice(0, 2).join(""), 16));
34
+ colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
35
+ }
36
+ const match = value.match(RGB_COLOR_REGEX);
37
+ if (match?.[1]) colors = match[1].split(",").map((value) => Number(value.trim())).map((num, i) => (0, _zag_js_utils.clampValue)(num, 0, i < 3 ? 255 : 1));
38
+ return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
39
+ }
40
+ toString(format = "css") {
41
+ switch (format) {
42
+ case "hex": return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
43
+ case "hexa": 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();
44
+ case "rgb": return `rgb(${this.red}, ${this.green}, ${this.blue})`;
45
+ case "css":
46
+ case "rgba": return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
47
+ case "hsl": return this.toHSL().toString("hsl");
48
+ case "hsb": return this.toHSB().toString("hsb");
49
+ default: return this.toFormat(format).toString(format);
50
+ }
51
+ }
52
+ toFormat(format) {
53
+ switch (format) {
54
+ case "rgba": return this;
55
+ case "hsba": return this.toHSB();
56
+ case "hsla": return this.toHSL();
57
+ case "oklab": {
58
+ const lin = require_oklab_math.srgbToLinearRgb(this.red, this.green, this.blue);
59
+ const { L, a, b } = require_oklab_math.linearRgbToOklab(lin);
60
+ return new require_oklab_color.OklabColor(L, a, b, (0, _zag_js_utils.toFixedNumber)(this.alpha, 4));
61
+ }
62
+ case "oklch": return this.toFormat("oklab").toFormat("oklch");
63
+ default: throw new Error("Unsupported color conversion: rgb -> " + format);
64
+ }
65
+ }
66
+ toHexInt() {
67
+ return this.red << 16 | this.green << 8 | this.blue;
68
+ }
69
+ /**
70
+ * Converts an RGB color value to HSB.
71
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
72
+ * @returns An HSBColor object.
73
+ */
74
+ toHSB() {
75
+ const red = this.red / 255;
76
+ const green = this.green / 255;
77
+ const blue = this.blue / 255;
78
+ const min = Math.min(red, green, blue);
79
+ const brightness = Math.max(red, green, blue);
80
+ const chroma = brightness - min;
81
+ const saturation = brightness === 0 ? 0 : chroma / brightness;
82
+ let hue = 0;
83
+ if (chroma !== 0) {
84
+ switch (brightness) {
85
+ case red:
86
+ hue = (green - blue) / chroma + (green < blue ? 6 : 0);
87
+ break;
88
+ case green:
89
+ hue = (blue - red) / chroma + 2;
90
+ break;
91
+ case blue: hue = (red - green) / chroma + 4;
92
+ }
93
+ hue /= 6;
94
+ }
95
+ return new require_hsb_color.HSBColor((0, _zag_js_utils.toFixedNumber)(hue * 360, 2), (0, _zag_js_utils.toFixedNumber)(saturation * 100, 2), (0, _zag_js_utils.toFixedNumber)(brightness * 100, 2), (0, _zag_js_utils.toFixedNumber)(this.alpha, 2));
96
+ }
97
+ /**
98
+ * Converts an RGB color value to HSL.
99
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
100
+ * @returns An HSLColor object.
101
+ */
102
+ toHSL() {
103
+ const red = this.red / 255;
104
+ const green = this.green / 255;
105
+ const blue = this.blue / 255;
106
+ const min = Math.min(red, green, blue);
107
+ const max = Math.max(red, green, blue);
108
+ const lightness = (max + min) / 2;
109
+ const chroma = max - min;
110
+ let hue = -1;
111
+ let saturation = -1;
112
+ if (chroma === 0) hue = saturation = 0;
113
+ else {
114
+ saturation = chroma / (lightness < .5 ? max + min : 2 - max - min);
115
+ switch (max) {
116
+ case red:
117
+ hue = (green - blue) / chroma + (green < blue ? 6 : 0);
118
+ break;
119
+ case green:
120
+ hue = (blue - red) / chroma + 2;
121
+ break;
122
+ case blue: hue = (red - green) / chroma + 4;
123
+ }
124
+ hue /= 6;
125
+ }
126
+ return new require_hsl_color.HSLColor((0, _zag_js_utils.toFixedNumber)(hue * 360, 2), (0, _zag_js_utils.toFixedNumber)(saturation * 100, 2), (0, _zag_js_utils.toFixedNumber)(lightness * 100, 2), (0, _zag_js_utils.toFixedNumber)(this.alpha, 2));
127
+ }
128
+ clone() {
129
+ return new RGBColor(this.red, this.green, this.blue, this.alpha);
130
+ }
131
+ getChannelFormatOptions(channel) {
132
+ switch (channel) {
133
+ case "red":
134
+ case "green":
135
+ case "blue": return { style: "decimal" };
136
+ case "alpha": return { style: "percent" };
137
+ default: throw new Error("Unknown color channel: " + channel);
138
+ }
139
+ }
140
+ formatChannelValue(channel, locale) {
141
+ let options = this.getChannelFormatOptions(channel);
142
+ let value = this.getChannelValue(channel);
143
+ return new Intl.NumberFormat(locale, options).format(value);
144
+ }
145
+ getChannelRange(channel) {
146
+ switch (channel) {
147
+ case "red":
148
+ case "green":
149
+ case "blue": return {
150
+ minValue: 0,
151
+ maxValue: 255,
152
+ step: 1,
153
+ pageSize: 17
154
+ };
155
+ case "alpha": return {
156
+ minValue: 0,
157
+ maxValue: 1,
158
+ step: .01,
159
+ pageSize: .1
160
+ };
161
+ default: throw new Error("Unknown color channel: " + channel);
162
+ }
163
+ }
164
+ toJSON() {
165
+ return {
166
+ r: this.red,
167
+ g: this.green,
168
+ b: this.blue,
169
+ a: this.alpha
170
+ };
171
+ }
172
+ getFormat() {
173
+ return "rgba";
174
+ }
175
+ getChannels() {
176
+ return RGBColor.colorChannels;
177
+ }
216
178
  };
217
- __publicField(_RGBColor, "colorChannels", ["red", "green", "blue"]);
218
- var RGBColor = _RGBColor;
219
- // Annotate the CommonJS export names for ESM import in node:
220
- 0 && (module.exports = {
221
- RGBColor
222
- });
179
+ require_defineProperty.default(RGBColor, "colorChannels", [
180
+ "red",
181
+ "green",
182
+ "blue"
183
+ ]);
184
+ exports.RGBColor = RGBColor;