@zag-js/color-utils 0.0.0-dev-20230414151834
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/chunk-7VB2GGWQ.mjs +382 -0
- package/dist/chunk-NHABU752.mjs +10 -0
- package/dist/chunk-NSR5W5ST.mjs +34 -0
- package/dist/chunk-TXOVH6IE.mjs +16 -0
- package/dist/chunk-WWI4IDFU.mjs +26 -0
- package/dist/color.d.ts +20 -0
- package/dist/color.js +58 -0
- package/dist/color.mjs +7 -0
- package/dist/hsb-color.d.ts +32 -0
- package/dist/hsb-color.js +440 -0
- package/dist/hsb-color.mjs +9 -0
- package/dist/hsl-color.d.ts +33 -0
- package/dist/hsl-color.js +442 -0
- package/dist/hsl-color.mjs +11 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +460 -0
- package/dist/index.mjs +15 -0
- package/dist/parse-color.d.ts +6 -0
- package/dist/parse-color.js +458 -0
- package/dist/parse-color.mjs +12 -0
- package/dist/rgb-color.d.ts +33 -0
- package/dist/rgb-color.js +440 -0
- package/dist/rgb-color.mjs +9 -0
- package/dist/types.d.ts +64 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +42 -0
- package/dist/utils.mjs +11 -0
- package/package.json +49 -0
|
@@ -0,0 +1,440 @@
|
|
|
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/rgb-color.ts
|
|
26
|
+
var rgb_color_exports = {};
|
|
27
|
+
__export(rgb_color_exports, {
|
|
28
|
+
RGBColor: () => RGBColor
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(rgb_color_exports);
|
|
31
|
+
|
|
32
|
+
// src/color.ts
|
|
33
|
+
var Color = class {
|
|
34
|
+
toHexInt() {
|
|
35
|
+
return this.toFormat("rgb").toHexInt();
|
|
36
|
+
}
|
|
37
|
+
getChannelValue(channel) {
|
|
38
|
+
if (channel in this) {
|
|
39
|
+
return this[channel];
|
|
40
|
+
}
|
|
41
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
42
|
+
}
|
|
43
|
+
withChannelValue(channel, value) {
|
|
44
|
+
if (channel in this) {
|
|
45
|
+
let clone = this.clone();
|
|
46
|
+
clone[channel] = value;
|
|
47
|
+
return clone;
|
|
48
|
+
}
|
|
49
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
50
|
+
}
|
|
51
|
+
getColorSpaceAxes(xyChannels) {
|
|
52
|
+
let { xChannel, yChannel } = xyChannels;
|
|
53
|
+
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
54
|
+
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
55
|
+
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
56
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
57
|
+
}
|
|
58
|
+
isEqual(color) {
|
|
59
|
+
return this.toHexInt() === color.toHexInt();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/utils.ts
|
|
64
|
+
function mod(n, m) {
|
|
65
|
+
return (n % m + m) % m;
|
|
66
|
+
}
|
|
67
|
+
function toFixedNumber(num, digits) {
|
|
68
|
+
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
69
|
+
}
|
|
70
|
+
function clampValue(value, min, max) {
|
|
71
|
+
return Math.min(Math.max(value, min), max);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/hsl-color.ts
|
|
75
|
+
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+)?)\)/;
|
|
76
|
+
var _HSLColor = class extends Color {
|
|
77
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
78
|
+
super();
|
|
79
|
+
this.hue = hue;
|
|
80
|
+
this.saturation = saturation;
|
|
81
|
+
this.lightness = lightness;
|
|
82
|
+
this.alpha = alpha;
|
|
83
|
+
}
|
|
84
|
+
static parse(value) {
|
|
85
|
+
let m;
|
|
86
|
+
if (m = value.match(HSL_REGEX)) {
|
|
87
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
88
|
+
return new _HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
toString(format) {
|
|
92
|
+
switch (format) {
|
|
93
|
+
case "hex":
|
|
94
|
+
return this.toRGB().toString("hex");
|
|
95
|
+
case "hexa":
|
|
96
|
+
return this.toRGB().toString("hexa");
|
|
97
|
+
case "hsl":
|
|
98
|
+
return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
|
|
99
|
+
case "css":
|
|
100
|
+
case "hsla":
|
|
101
|
+
return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
102
|
+
default:
|
|
103
|
+
return this.toFormat(format).toString(format);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
toFormat(format) {
|
|
107
|
+
switch (format) {
|
|
108
|
+
case "hsl":
|
|
109
|
+
case "hsla":
|
|
110
|
+
return this;
|
|
111
|
+
case "hsb":
|
|
112
|
+
case "hsba":
|
|
113
|
+
return this.toHSB();
|
|
114
|
+
case "rgb":
|
|
115
|
+
case "rgba":
|
|
116
|
+
return this.toRGB();
|
|
117
|
+
default:
|
|
118
|
+
throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Converts a HSL color to HSB.
|
|
123
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
124
|
+
* @returns An HSBColor object.
|
|
125
|
+
*/
|
|
126
|
+
toHSB() {
|
|
127
|
+
let saturation = this.saturation / 100;
|
|
128
|
+
let lightness = this.lightness / 100;
|
|
129
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
130
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
131
|
+
return new HSBColor(
|
|
132
|
+
toFixedNumber(this.hue, 2),
|
|
133
|
+
toFixedNumber(saturation * 100, 2),
|
|
134
|
+
toFixedNumber(brightness * 100, 2),
|
|
135
|
+
this.alpha
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Converts a HSL color to RGB.
|
|
140
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
141
|
+
* @returns An RGBColor object.
|
|
142
|
+
*/
|
|
143
|
+
toRGB() {
|
|
144
|
+
let hue = this.hue;
|
|
145
|
+
let saturation = this.saturation / 100;
|
|
146
|
+
let lightness = this.lightness / 100;
|
|
147
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
148
|
+
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
149
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
150
|
+
}
|
|
151
|
+
clone() {
|
|
152
|
+
return new _HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
153
|
+
}
|
|
154
|
+
getChannelRange(channel) {
|
|
155
|
+
switch (channel) {
|
|
156
|
+
case "hue":
|
|
157
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
158
|
+
case "saturation":
|
|
159
|
+
case "lightness":
|
|
160
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
161
|
+
case "alpha":
|
|
162
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
163
|
+
default:
|
|
164
|
+
throw new Error("Unknown color channel: " + channel);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
getColorSpace() {
|
|
168
|
+
return "hsl";
|
|
169
|
+
}
|
|
170
|
+
getColorChannels() {
|
|
171
|
+
return _HSLColor.colorChannels;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var HSLColor = _HSLColor;
|
|
175
|
+
__publicField(HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
|
|
176
|
+
|
|
177
|
+
// src/hsb-color.ts
|
|
178
|
+
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+)?)\)/;
|
|
179
|
+
var _HSBColor = class extends Color {
|
|
180
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
181
|
+
super();
|
|
182
|
+
this.hue = hue;
|
|
183
|
+
this.saturation = saturation;
|
|
184
|
+
this.brightness = brightness;
|
|
185
|
+
this.alpha = alpha;
|
|
186
|
+
}
|
|
187
|
+
static parse(value) {
|
|
188
|
+
let m;
|
|
189
|
+
if (m = value.match(HSB_REGEX)) {
|
|
190
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
191
|
+
return new _HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
toString(format) {
|
|
195
|
+
switch (format) {
|
|
196
|
+
case "css":
|
|
197
|
+
return this.toHSL().toString("css");
|
|
198
|
+
case "hex":
|
|
199
|
+
return this.toRGB().toString("hex");
|
|
200
|
+
case "hexa":
|
|
201
|
+
return this.toRGB().toString("hexa");
|
|
202
|
+
case "hsb":
|
|
203
|
+
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
|
|
204
|
+
case "hsba":
|
|
205
|
+
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
206
|
+
default:
|
|
207
|
+
return this.toFormat(format).toString(format);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
toFormat(format) {
|
|
211
|
+
switch (format) {
|
|
212
|
+
case "hsb":
|
|
213
|
+
case "hsba":
|
|
214
|
+
return this;
|
|
215
|
+
case "hsl":
|
|
216
|
+
case "hsla":
|
|
217
|
+
return this.toHSL();
|
|
218
|
+
case "rgb":
|
|
219
|
+
case "rgba":
|
|
220
|
+
return this.toRGB();
|
|
221
|
+
default:
|
|
222
|
+
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Converts a HSB color to HSL.
|
|
227
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
228
|
+
* @returns An HSLColor object.
|
|
229
|
+
*/
|
|
230
|
+
toHSL() {
|
|
231
|
+
let saturation = this.saturation / 100;
|
|
232
|
+
let brightness = this.brightness / 100;
|
|
233
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
234
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
235
|
+
return new HSLColor(
|
|
236
|
+
toFixedNumber(this.hue, 2),
|
|
237
|
+
toFixedNumber(saturation * 100, 2),
|
|
238
|
+
toFixedNumber(lightness * 100, 2),
|
|
239
|
+
this.alpha
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Converts a HSV color value to RGB.
|
|
244
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
245
|
+
* @returns An RGBColor object.
|
|
246
|
+
*/
|
|
247
|
+
toRGB() {
|
|
248
|
+
let hue = this.hue;
|
|
249
|
+
let saturation = this.saturation / 100;
|
|
250
|
+
let brightness = this.brightness / 100;
|
|
251
|
+
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
252
|
+
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
253
|
+
}
|
|
254
|
+
clone() {
|
|
255
|
+
return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
256
|
+
}
|
|
257
|
+
getChannelRange(channel) {
|
|
258
|
+
switch (channel) {
|
|
259
|
+
case "hue":
|
|
260
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
261
|
+
case "saturation":
|
|
262
|
+
case "brightness":
|
|
263
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
264
|
+
case "alpha":
|
|
265
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
266
|
+
default:
|
|
267
|
+
throw new Error("Unknown color channel: " + channel);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
getColorSpace() {
|
|
271
|
+
return "hsb";
|
|
272
|
+
}
|
|
273
|
+
getColorChannels() {
|
|
274
|
+
return _HSBColor.colorChannels;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
var HSBColor = _HSBColor;
|
|
278
|
+
__publicField(HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
|
|
279
|
+
|
|
280
|
+
// src/rgb-color.ts
|
|
281
|
+
var _RGBColor = class extends Color {
|
|
282
|
+
constructor(red, green, blue, alpha) {
|
|
283
|
+
super();
|
|
284
|
+
this.red = red;
|
|
285
|
+
this.green = green;
|
|
286
|
+
this.blue = blue;
|
|
287
|
+
this.alpha = alpha;
|
|
288
|
+
}
|
|
289
|
+
static parse(value) {
|
|
290
|
+
let colors = [];
|
|
291
|
+
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
292
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
293
|
+
while (values.length > 0) {
|
|
294
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
295
|
+
}
|
|
296
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
297
|
+
}
|
|
298
|
+
const match = value.match(/^rgba?\((.*)\)$/);
|
|
299
|
+
if (match?.[1]) {
|
|
300
|
+
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
|
|
301
|
+
}
|
|
302
|
+
return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
303
|
+
}
|
|
304
|
+
toString(format) {
|
|
305
|
+
switch (format) {
|
|
306
|
+
case "hex":
|
|
307
|
+
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
308
|
+
case "hexa":
|
|
309
|
+
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();
|
|
310
|
+
case "rgb":
|
|
311
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
312
|
+
case "css":
|
|
313
|
+
case "rgba":
|
|
314
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
315
|
+
default:
|
|
316
|
+
return this.toFormat(format).toString(format);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
toFormat(format) {
|
|
320
|
+
switch (format) {
|
|
321
|
+
case "hex":
|
|
322
|
+
case "hexa":
|
|
323
|
+
case "rgb":
|
|
324
|
+
case "rgba":
|
|
325
|
+
return this;
|
|
326
|
+
case "hsb":
|
|
327
|
+
case "hsba":
|
|
328
|
+
return this.toHSB();
|
|
329
|
+
case "hsl":
|
|
330
|
+
case "hsla":
|
|
331
|
+
return this.toHSL();
|
|
332
|
+
default:
|
|
333
|
+
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
toHexInt() {
|
|
337
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Converts an RGB color value to HSB.
|
|
341
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
342
|
+
* @returns An HSBColor object.
|
|
343
|
+
*/
|
|
344
|
+
toHSB() {
|
|
345
|
+
const red = this.red / 255;
|
|
346
|
+
const green = this.green / 255;
|
|
347
|
+
const blue = this.blue / 255;
|
|
348
|
+
const min = Math.min(red, green, blue);
|
|
349
|
+
const brightness = Math.max(red, green, blue);
|
|
350
|
+
const chroma = brightness - min;
|
|
351
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
352
|
+
let hue = 0;
|
|
353
|
+
if (chroma !== 0) {
|
|
354
|
+
switch (brightness) {
|
|
355
|
+
case red:
|
|
356
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
357
|
+
break;
|
|
358
|
+
case green:
|
|
359
|
+
hue = (blue - red) / chroma + 2;
|
|
360
|
+
break;
|
|
361
|
+
case blue:
|
|
362
|
+
hue = (red - green) / chroma + 4;
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
hue /= 6;
|
|
366
|
+
}
|
|
367
|
+
return new HSBColor(
|
|
368
|
+
toFixedNumber(hue * 360, 2),
|
|
369
|
+
toFixedNumber(saturation * 100, 2),
|
|
370
|
+
toFixedNumber(brightness * 100, 2),
|
|
371
|
+
this.alpha
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Converts an RGB color value to HSL.
|
|
376
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
377
|
+
* @returns An HSLColor object.
|
|
378
|
+
*/
|
|
379
|
+
toHSL() {
|
|
380
|
+
const red = this.red / 255;
|
|
381
|
+
const green = this.green / 255;
|
|
382
|
+
const blue = this.blue / 255;
|
|
383
|
+
const min = Math.min(red, green, blue);
|
|
384
|
+
const max = Math.max(red, green, blue);
|
|
385
|
+
const lightness = (max + min) / 2;
|
|
386
|
+
const chroma = max - min;
|
|
387
|
+
let hue = -1;
|
|
388
|
+
let saturation = -1;
|
|
389
|
+
if (chroma === 0) {
|
|
390
|
+
hue = saturation = 0;
|
|
391
|
+
} else {
|
|
392
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
393
|
+
switch (max) {
|
|
394
|
+
case red:
|
|
395
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
396
|
+
break;
|
|
397
|
+
case green:
|
|
398
|
+
hue = (blue - red) / chroma + 2;
|
|
399
|
+
break;
|
|
400
|
+
case blue:
|
|
401
|
+
hue = (red - green) / chroma + 4;
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
hue /= 6;
|
|
405
|
+
}
|
|
406
|
+
return new HSLColor(
|
|
407
|
+
toFixedNumber(hue * 360, 2),
|
|
408
|
+
toFixedNumber(saturation * 100, 2),
|
|
409
|
+
toFixedNumber(lightness * 100, 2),
|
|
410
|
+
this.alpha
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
clone() {
|
|
414
|
+
return new _RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
415
|
+
}
|
|
416
|
+
getChannelRange(channel) {
|
|
417
|
+
switch (channel) {
|
|
418
|
+
case "red":
|
|
419
|
+
case "green":
|
|
420
|
+
case "blue":
|
|
421
|
+
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
422
|
+
case "alpha":
|
|
423
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
424
|
+
default:
|
|
425
|
+
throw new Error("Unknown color channel: " + channel);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
getColorSpace() {
|
|
429
|
+
return "rgb";
|
|
430
|
+
}
|
|
431
|
+
getColorChannels() {
|
|
432
|
+
return _RGBColor.colorChannels;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
var RGBColor = _RGBColor;
|
|
436
|
+
__publicField(RGBColor, "colorChannels", ["red", "green", "blue"]);
|
|
437
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
438
|
+
0 && (module.exports = {
|
|
439
|
+
RGBColor
|
|
440
|
+
});
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type ColorFormat = "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hsb" | "hsba";
|
|
2
|
+
type ColorChannel = "hue" | "saturation" | "brightness" | "lightness" | "red" | "green" | "blue" | "alpha";
|
|
3
|
+
type ColorAxes = {
|
|
4
|
+
xChannel: ColorChannel;
|
|
5
|
+
yChannel: ColorChannel;
|
|
6
|
+
zChannel: ColorChannel;
|
|
7
|
+
};
|
|
8
|
+
interface ColorChannelRange {
|
|
9
|
+
/** The minimum value of the color channel. */
|
|
10
|
+
minValue: number;
|
|
11
|
+
/** The maximum value of the color channel. */
|
|
12
|
+
maxValue: number;
|
|
13
|
+
/** The step value of the color channel, used when incrementing and decrementing. */
|
|
14
|
+
step: number;
|
|
15
|
+
/** The page step value of the color channel, used when incrementing and decrementing. */
|
|
16
|
+
pageSize: number;
|
|
17
|
+
}
|
|
18
|
+
interface ColorType {
|
|
19
|
+
/** Converts the color to the given color format, and returns a new Color object. */
|
|
20
|
+
toFormat(format: ColorFormat): ColorType;
|
|
21
|
+
/** Converts the color to a string in the given format. */
|
|
22
|
+
toString(format: ColorFormat | "css"): string;
|
|
23
|
+
/** Converts the color to hex, and returns an integer representation. */
|
|
24
|
+
toHexInt(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the numeric value for a given channel.
|
|
27
|
+
* Throws an error if the channel is unsupported in the current color format.
|
|
28
|
+
*/
|
|
29
|
+
getChannelValue(channel: ColorChannel): number;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the numeric value for a given channel, and returns a new Color object.
|
|
32
|
+
* Throws an error if the channel is unsupported in the current color format.
|
|
33
|
+
*/
|
|
34
|
+
withChannelValue(channel: ColorChannel, value: number): ColorType;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the minimum, maximum, and step values for a given channel.
|
|
37
|
+
*/
|
|
38
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the color space, 'rgb', 'hsb' or 'hsl', for the current color.
|
|
41
|
+
*/
|
|
42
|
+
getColorSpace(): ColorFormat;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the color space axes, xChannel, yChannel, zChannel.
|
|
45
|
+
*/
|
|
46
|
+
getColorSpaceAxes(xyChannels: {
|
|
47
|
+
xChannel?: ColorChannel;
|
|
48
|
+
yChannel?: ColorChannel;
|
|
49
|
+
}): ColorAxes;
|
|
50
|
+
/**
|
|
51
|
+
* Returns an array of the color channels within the current color space space.
|
|
52
|
+
*/
|
|
53
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
54
|
+
/**
|
|
55
|
+
* Returns a new Color object with the same values as the current color.
|
|
56
|
+
*/
|
|
57
|
+
clone(): ColorType;
|
|
58
|
+
/**
|
|
59
|
+
* Whether the color is equal to another color.
|
|
60
|
+
*/
|
|
61
|
+
isEqual(color: ColorType): boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, ColorType };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/types.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
package/dist/types.mjs
ADDED
|
File without changes
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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 __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 = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
|
19
|
+
|
|
20
|
+
// src/utils.ts
|
|
21
|
+
var utils_exports = {};
|
|
22
|
+
__export(utils_exports, {
|
|
23
|
+
clampValue: () => clampValue,
|
|
24
|
+
mod: () => mod,
|
|
25
|
+
toFixedNumber: () => toFixedNumber
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(utils_exports);
|
|
28
|
+
function mod(n, m) {
|
|
29
|
+
return (n % m + m) % m;
|
|
30
|
+
}
|
|
31
|
+
function toFixedNumber(num, digits) {
|
|
32
|
+
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
33
|
+
}
|
|
34
|
+
function clampValue(value, min, max) {
|
|
35
|
+
return Math.min(Math.max(value, min), max);
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
clampValue,
|
|
40
|
+
mod,
|
|
41
|
+
toFixedNumber
|
|
42
|
+
});
|
package/dist/utils.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zag-js/color-utils",
|
|
3
|
+
"version": "0.0.0-dev-20230414151834",
|
|
4
|
+
"description": "Color utilities for zag.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"js",
|
|
7
|
+
"utils",
|
|
8
|
+
"color-utils"
|
|
9
|
+
],
|
|
10
|
+
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
11
|
+
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"main": "dist/index.js",
|
|
14
|
+
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/color-utils",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/chakra-ui/zag/issues"
|
|
24
|
+
},
|
|
25
|
+
"clean-package": "../../../clean-package.config.json",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"clean-package": "2.2.0"
|
|
28
|
+
},
|
|
29
|
+
"module": "dist/index.mjs",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.mjs",
|
|
35
|
+
"require": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build-fast": "tsup src",
|
|
41
|
+
"start": "pnpm build --watch",
|
|
42
|
+
"build": "tsup src --dts",
|
|
43
|
+
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
44
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
45
|
+
"test-ci": "pnpm test --ci --runInBand",
|
|
46
|
+
"test-watch": "pnpm test --watch -u",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|