@zag-js/color-utils 0.10.4 → 0.11.0
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/dist/{types.d.ts → index.d.mts} +27 -5
- package/dist/index.d.ts +84 -3
- package/dist/index.js +456 -7
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +434 -2
- package/dist/index.mjs.map +1 -0
- package/package.json +2 -2
- package/src/hsb-color.ts +6 -1
- package/src/hsl-color.ts +6 -1
- package/src/rgb-color.ts +6 -1
- package/dist/color.d.ts +0 -17
- package/dist/color.js +0 -35
- package/dist/color.mjs +0 -31
- package/dist/hsb-color.d.ts +0 -29
- package/dist/hsb-color.js +0 -111
- package/dist/hsb-color.mjs +0 -107
- package/dist/hsl-color.d.ts +0 -30
- package/dist/hsl-color.js +0 -112
- package/dist/hsl-color.mjs +0 -107
- package/dist/parse-color.d.ts +0 -3
- package/dist/parse-color.js +0 -25
- package/dist/parse-color.mjs +0 -20
- package/dist/rgb-color.d.ts +0 -30
- package/dist/rgb-color.js +0 -166
- package/dist/rgb-color.mjs +0 -162
- package/dist/utils.d.ts +0 -3
- package/dist/utils.js +0 -17
- package/dist/utils.mjs +0 -11
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,434 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/color.ts
|
|
9
|
+
var Color = class {
|
|
10
|
+
toHexInt() {
|
|
11
|
+
return this.toFormat("rgb").toHexInt();
|
|
12
|
+
}
|
|
13
|
+
getChannelValue(channel) {
|
|
14
|
+
if (channel in this) {
|
|
15
|
+
return this[channel];
|
|
16
|
+
}
|
|
17
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
18
|
+
}
|
|
19
|
+
withChannelValue(channel, value) {
|
|
20
|
+
if (channel in this) {
|
|
21
|
+
let clone = this.clone();
|
|
22
|
+
clone[channel] = value;
|
|
23
|
+
return clone;
|
|
24
|
+
}
|
|
25
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
26
|
+
}
|
|
27
|
+
getColorSpaceAxes(xyChannels) {
|
|
28
|
+
let { xChannel, yChannel } = xyChannels;
|
|
29
|
+
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
30
|
+
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
31
|
+
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
32
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
33
|
+
}
|
|
34
|
+
isEqual(color) {
|
|
35
|
+
return this.toHexInt() === color.toHexInt();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/utils.ts
|
|
40
|
+
function mod(n, m) {
|
|
41
|
+
return (n % m + m) % m;
|
|
42
|
+
}
|
|
43
|
+
function toFixedNumber(num, digits) {
|
|
44
|
+
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
45
|
+
}
|
|
46
|
+
function clampValue(value, min, max) {
|
|
47
|
+
return Math.min(Math.max(value, min), max);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/rgb-color.ts
|
|
51
|
+
var _RGBColor = class _RGBColor extends Color {
|
|
52
|
+
constructor(red, green, blue, alpha) {
|
|
53
|
+
super();
|
|
54
|
+
this.red = red;
|
|
55
|
+
this.green = green;
|
|
56
|
+
this.blue = blue;
|
|
57
|
+
this.alpha = alpha;
|
|
58
|
+
}
|
|
59
|
+
static parse(value) {
|
|
60
|
+
let colors = [];
|
|
61
|
+
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
62
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
63
|
+
while (values.length > 0) {
|
|
64
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
65
|
+
}
|
|
66
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
67
|
+
}
|
|
68
|
+
const match = value.match(/^rgba?\((.*)\)$/);
|
|
69
|
+
if (match?.[1]) {
|
|
70
|
+
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
|
|
71
|
+
}
|
|
72
|
+
return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
73
|
+
}
|
|
74
|
+
toString(format) {
|
|
75
|
+
switch (format) {
|
|
76
|
+
case "hex":
|
|
77
|
+
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
78
|
+
case "hexa":
|
|
79
|
+
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();
|
|
80
|
+
case "rgb":
|
|
81
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
82
|
+
case "css":
|
|
83
|
+
case "rgba":
|
|
84
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
85
|
+
default:
|
|
86
|
+
return this.toFormat(format).toString(format);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
toFormat(format) {
|
|
90
|
+
switch (format) {
|
|
91
|
+
case "hex":
|
|
92
|
+
case "hexa":
|
|
93
|
+
case "rgb":
|
|
94
|
+
case "rgba":
|
|
95
|
+
return this;
|
|
96
|
+
case "hsb":
|
|
97
|
+
case "hsba":
|
|
98
|
+
return this.toHSB();
|
|
99
|
+
case "hsl":
|
|
100
|
+
case "hsla":
|
|
101
|
+
return this.toHSL();
|
|
102
|
+
default:
|
|
103
|
+
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
toHexInt() {
|
|
107
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts an RGB color value to HSB.
|
|
111
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
112
|
+
* @returns An HSBColor object.
|
|
113
|
+
*/
|
|
114
|
+
toHSB() {
|
|
115
|
+
const red = this.red / 255;
|
|
116
|
+
const green = this.green / 255;
|
|
117
|
+
const blue = this.blue / 255;
|
|
118
|
+
const min = Math.min(red, green, blue);
|
|
119
|
+
const brightness = Math.max(red, green, blue);
|
|
120
|
+
const chroma = brightness - min;
|
|
121
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
122
|
+
let hue = 0;
|
|
123
|
+
if (chroma !== 0) {
|
|
124
|
+
switch (brightness) {
|
|
125
|
+
case red:
|
|
126
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
127
|
+
break;
|
|
128
|
+
case green:
|
|
129
|
+
hue = (blue - red) / chroma + 2;
|
|
130
|
+
break;
|
|
131
|
+
case blue:
|
|
132
|
+
hue = (red - green) / chroma + 4;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
hue /= 6;
|
|
136
|
+
}
|
|
137
|
+
return new HSBColor(
|
|
138
|
+
toFixedNumber(hue * 360, 2),
|
|
139
|
+
toFixedNumber(saturation * 100, 2),
|
|
140
|
+
toFixedNumber(brightness * 100, 2),
|
|
141
|
+
this.alpha
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Converts an RGB color value to HSL.
|
|
146
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
147
|
+
* @returns An HSLColor object.
|
|
148
|
+
*/
|
|
149
|
+
toHSL() {
|
|
150
|
+
const red = this.red / 255;
|
|
151
|
+
const green = this.green / 255;
|
|
152
|
+
const blue = this.blue / 255;
|
|
153
|
+
const min = Math.min(red, green, blue);
|
|
154
|
+
const max = Math.max(red, green, blue);
|
|
155
|
+
const lightness = (max + min) / 2;
|
|
156
|
+
const chroma = max - min;
|
|
157
|
+
let hue = -1;
|
|
158
|
+
let saturation = -1;
|
|
159
|
+
if (chroma === 0) {
|
|
160
|
+
hue = saturation = 0;
|
|
161
|
+
} else {
|
|
162
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
163
|
+
switch (max) {
|
|
164
|
+
case red:
|
|
165
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
166
|
+
break;
|
|
167
|
+
case green:
|
|
168
|
+
hue = (blue - red) / chroma + 2;
|
|
169
|
+
break;
|
|
170
|
+
case blue:
|
|
171
|
+
hue = (red - green) / chroma + 4;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
hue /= 6;
|
|
175
|
+
}
|
|
176
|
+
return new HSLColor(
|
|
177
|
+
toFixedNumber(hue * 360, 2),
|
|
178
|
+
toFixedNumber(saturation * 100, 2),
|
|
179
|
+
toFixedNumber(lightness * 100, 2),
|
|
180
|
+
this.alpha
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
clone() {
|
|
184
|
+
return new _RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
185
|
+
}
|
|
186
|
+
getChannelRange(channel) {
|
|
187
|
+
switch (channel) {
|
|
188
|
+
case "red":
|
|
189
|
+
case "green":
|
|
190
|
+
case "blue":
|
|
191
|
+
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
192
|
+
case "alpha":
|
|
193
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
194
|
+
default:
|
|
195
|
+
throw new Error("Unknown color channel: " + channel);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
getColorSpace() {
|
|
199
|
+
return "rgb";
|
|
200
|
+
}
|
|
201
|
+
getColorChannels() {
|
|
202
|
+
return _RGBColor.colorChannels;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
__publicField(_RGBColor, "colorChannels", ["red", "green", "blue"]);
|
|
206
|
+
var RGBColor = _RGBColor;
|
|
207
|
+
|
|
208
|
+
// src/hsl-color.ts
|
|
209
|
+
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+)?)\)/;
|
|
210
|
+
var _HSLColor = class _HSLColor extends Color {
|
|
211
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
212
|
+
super();
|
|
213
|
+
this.hue = hue;
|
|
214
|
+
this.saturation = saturation;
|
|
215
|
+
this.lightness = lightness;
|
|
216
|
+
this.alpha = alpha;
|
|
217
|
+
}
|
|
218
|
+
static parse(value) {
|
|
219
|
+
let m;
|
|
220
|
+
if (m = value.match(HSL_REGEX)) {
|
|
221
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
222
|
+
return new _HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
toString(format) {
|
|
226
|
+
switch (format) {
|
|
227
|
+
case "hex":
|
|
228
|
+
return this.toRGB().toString("hex");
|
|
229
|
+
case "hexa":
|
|
230
|
+
return this.toRGB().toString("hexa");
|
|
231
|
+
case "hsl":
|
|
232
|
+
return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
|
|
233
|
+
case "css":
|
|
234
|
+
case "hsla":
|
|
235
|
+
return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
236
|
+
default:
|
|
237
|
+
return this.toFormat(format).toString(format);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
toFormat(format) {
|
|
241
|
+
switch (format) {
|
|
242
|
+
case "hsl":
|
|
243
|
+
case "hsla":
|
|
244
|
+
return this;
|
|
245
|
+
case "hsb":
|
|
246
|
+
case "hsba":
|
|
247
|
+
return this.toHSB();
|
|
248
|
+
case "rgb":
|
|
249
|
+
case "rgba":
|
|
250
|
+
return this.toRGB();
|
|
251
|
+
default:
|
|
252
|
+
throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Converts a HSL color to HSB.
|
|
257
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
258
|
+
* @returns An HSBColor object.
|
|
259
|
+
*/
|
|
260
|
+
toHSB() {
|
|
261
|
+
let saturation = this.saturation / 100;
|
|
262
|
+
let lightness = this.lightness / 100;
|
|
263
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
264
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
265
|
+
return new HSBColor(
|
|
266
|
+
toFixedNumber(this.hue, 2),
|
|
267
|
+
toFixedNumber(saturation * 100, 2),
|
|
268
|
+
toFixedNumber(brightness * 100, 2),
|
|
269
|
+
this.alpha
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Converts a HSL color to RGB.
|
|
274
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
275
|
+
* @returns An RGBColor object.
|
|
276
|
+
*/
|
|
277
|
+
toRGB() {
|
|
278
|
+
let hue = this.hue;
|
|
279
|
+
let saturation = this.saturation / 100;
|
|
280
|
+
let lightness = this.lightness / 100;
|
|
281
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
282
|
+
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
283
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
284
|
+
}
|
|
285
|
+
clone() {
|
|
286
|
+
return new _HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
287
|
+
}
|
|
288
|
+
getChannelRange(channel) {
|
|
289
|
+
switch (channel) {
|
|
290
|
+
case "hue":
|
|
291
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
292
|
+
case "saturation":
|
|
293
|
+
case "lightness":
|
|
294
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
295
|
+
case "alpha":
|
|
296
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
297
|
+
default:
|
|
298
|
+
throw new Error("Unknown color channel: " + channel);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
getColorSpace() {
|
|
302
|
+
return "hsl";
|
|
303
|
+
}
|
|
304
|
+
getColorChannels() {
|
|
305
|
+
return _HSLColor.colorChannels;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
__publicField(_HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
|
|
309
|
+
var HSLColor = _HSLColor;
|
|
310
|
+
|
|
311
|
+
// src/hsb-color.ts
|
|
312
|
+
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+)?)\)/;
|
|
313
|
+
var _HSBColor = class _HSBColor extends Color {
|
|
314
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
315
|
+
super();
|
|
316
|
+
this.hue = hue;
|
|
317
|
+
this.saturation = saturation;
|
|
318
|
+
this.brightness = brightness;
|
|
319
|
+
this.alpha = alpha;
|
|
320
|
+
}
|
|
321
|
+
static parse(value) {
|
|
322
|
+
let m;
|
|
323
|
+
if (m = value.match(HSB_REGEX)) {
|
|
324
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
325
|
+
return new _HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
toString(format) {
|
|
329
|
+
switch (format) {
|
|
330
|
+
case "css":
|
|
331
|
+
return this.toHSL().toString("css");
|
|
332
|
+
case "hex":
|
|
333
|
+
return this.toRGB().toString("hex");
|
|
334
|
+
case "hexa":
|
|
335
|
+
return this.toRGB().toString("hexa");
|
|
336
|
+
case "hsb":
|
|
337
|
+
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
|
|
338
|
+
case "hsba":
|
|
339
|
+
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
340
|
+
default:
|
|
341
|
+
return this.toFormat(format).toString(format);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
toFormat(format) {
|
|
345
|
+
switch (format) {
|
|
346
|
+
case "hsb":
|
|
347
|
+
case "hsba":
|
|
348
|
+
return this;
|
|
349
|
+
case "hsl":
|
|
350
|
+
case "hsla":
|
|
351
|
+
return this.toHSL();
|
|
352
|
+
case "rgb":
|
|
353
|
+
case "rgba":
|
|
354
|
+
return this.toRGB();
|
|
355
|
+
default:
|
|
356
|
+
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Converts a HSB color to HSL.
|
|
361
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
362
|
+
* @returns An HSLColor object.
|
|
363
|
+
*/
|
|
364
|
+
toHSL() {
|
|
365
|
+
let saturation = this.saturation / 100;
|
|
366
|
+
let brightness = this.brightness / 100;
|
|
367
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
368
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
369
|
+
return new HSLColor(
|
|
370
|
+
toFixedNumber(this.hue, 2),
|
|
371
|
+
toFixedNumber(saturation * 100, 2),
|
|
372
|
+
toFixedNumber(lightness * 100, 2),
|
|
373
|
+
this.alpha
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Converts a HSV color value to RGB.
|
|
378
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
379
|
+
* @returns An RGBColor object.
|
|
380
|
+
*/
|
|
381
|
+
toRGB() {
|
|
382
|
+
let hue = this.hue;
|
|
383
|
+
let saturation = this.saturation / 100;
|
|
384
|
+
let brightness = this.brightness / 100;
|
|
385
|
+
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
386
|
+
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
387
|
+
}
|
|
388
|
+
clone() {
|
|
389
|
+
return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
390
|
+
}
|
|
391
|
+
getChannelRange(channel) {
|
|
392
|
+
switch (channel) {
|
|
393
|
+
case "hue":
|
|
394
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
395
|
+
case "saturation":
|
|
396
|
+
case "brightness":
|
|
397
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
398
|
+
case "alpha":
|
|
399
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
400
|
+
default:
|
|
401
|
+
throw new Error("Unknown color channel: " + channel);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
getColorSpace() {
|
|
405
|
+
return "hsb";
|
|
406
|
+
}
|
|
407
|
+
getColorChannels() {
|
|
408
|
+
return _HSBColor.colorChannels;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
__publicField(_HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
|
|
412
|
+
var HSBColor = _HSBColor;
|
|
413
|
+
|
|
414
|
+
// src/parse-color.ts
|
|
415
|
+
function parseColor(value) {
|
|
416
|
+
let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
417
|
+
if (!result) {
|
|
418
|
+
throw new Error("Invalid color value: " + value);
|
|
419
|
+
}
|
|
420
|
+
return result;
|
|
421
|
+
}
|
|
422
|
+
function normalizeColor(v) {
|
|
423
|
+
if (typeof v === "string") {
|
|
424
|
+
return parseColor(v);
|
|
425
|
+
} else {
|
|
426
|
+
return v;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
export {
|
|
430
|
+
Color,
|
|
431
|
+
normalizeColor,
|
|
432
|
+
parseColor
|
|
433
|
+
};
|
|
434
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/color.ts","../src/utils.ts","../src/rgb-color.ts","../src/hsl-color.ts","../src/hsb-color.ts","../src/parse-color.ts"],"sourcesContent":["import type { ColorType, ColorFormat, ColorChannel, ColorChannelRange, ColorAxes } from \"./types\"\n\nexport abstract class Color implements ColorType {\n abstract toFormat(format: ColorFormat): ColorType\n abstract toString(format: ColorFormat | \"css\"): string\n abstract clone(): ColorType\n abstract getChannelRange(channel: ColorChannel): ColorChannelRange\n abstract getColorSpace(): ColorFormat\n abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]\n\n toHexInt(): number {\n return this.toFormat(\"rgb\").toHexInt()\n }\n\n getChannelValue(channel: ColorChannel): number {\n if (channel in this) {\n return this[channel]\n }\n\n throw new Error(\"Unsupported color channel: \" + channel)\n }\n\n withChannelValue(channel: ColorChannel, value: number): ColorType {\n if (channel in this) {\n let clone = this.clone()\n clone[channel] = value\n return clone\n }\n\n throw new Error(\"Unsupported color channel: \" + channel)\n }\n\n getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {\n let { xChannel, yChannel } = xyChannels\n let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel)\n let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh)\n let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh)\n return { xChannel: xCh!, yChannel: yCh!, zChannel: zCh! }\n }\n\n isEqual(color: ColorType): boolean {\n return this.toHexInt() === color.toHexInt()\n }\n}\n","export function mod(n: number, m: number) {\n return ((n % m) + m) % m\n}\n\nexport function toFixedNumber(num: number, digits: number) {\n return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits)\n}\n\nexport function clampValue(value: number, min: number, max: number) {\n return Math.min(Math.max(value, min), max)\n}\n","import { Color } from \"./color\"\nimport { HSBColor } from \"./hsb-color\"\nimport { HSLColor } from \"./hsl-color\"\nimport { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from \"./types\"\nimport { clampValue, toFixedNumber } from \"./utils\"\n\nexport class RGBColor extends Color {\n constructor(\n private red: number,\n private green: number,\n private blue: number,\n private alpha: number,\n ) {\n super()\n }\n\n static parse(value: string) {\n let colors: (number | undefined)[] = []\n\n // matching #rgb, #rgba, #rrggbb, #rrggbbaa\n if (/^#[\\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {\n const values = (value.length < 6 ? value.replace(/[^#]/gi, \"$&$&\") : value).slice(1).split(\"\")\n while (values.length > 0) {\n colors.push(parseInt(values.splice(0, 2).join(\"\"), 16))\n }\n colors[3] = colors[3] !== undefined ? colors[3] / 255 : undefined\n }\n\n // matching rgb(rrr, ggg, bbb), rgba(rrr, ggg, bbb, 0.a)\n const match = value.match(/^rgba?\\((.*)\\)$/)\n\n if (match?.[1]) {\n colors = match[1]\n .split(\",\")\n .map((value) => Number(value.trim()))\n .map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1))\n }\n\n //@ts-expect-error\n return colors.length < 3 ? undefined : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1)\n }\n\n toString(format: ColorFormat | \"css\") {\n switch (format) {\n case \"hex\":\n return (\n \"#\" +\n (\n this.red.toString(16).padStart(2, \"0\") +\n this.green.toString(16).padStart(2, \"0\") +\n this.blue.toString(16).padStart(2, \"0\")\n ).toUpperCase()\n )\n case \"hexa\":\n return (\n \"#\" +\n (\n this.red.toString(16).padStart(2, \"0\") +\n this.green.toString(16).padStart(2, \"0\") +\n this.blue.toString(16).padStart(2, \"0\") +\n Math.round(this.alpha * 255)\n .toString(16)\n .padStart(2, \"0\")\n ).toUpperCase()\n )\n case \"rgb\":\n return `rgb(${this.red}, ${this.green}, ${this.blue})`\n case \"css\":\n case \"rgba\":\n return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`\n default:\n return this.toFormat(format).toString(format)\n }\n }\n\n toFormat(format: ColorFormat): ColorType {\n switch (format) {\n case \"hex\":\n case \"hexa\":\n case \"rgb\":\n case \"rgba\":\n return this\n case \"hsb\":\n case \"hsba\":\n return this.toHSB()\n case \"hsl\":\n case \"hsla\":\n return this.toHSL()\n default:\n throw new Error(\"Unsupported color conversion: rgb -> \" + format)\n }\n }\n\n toHexInt(): number {\n return (this.red << 16) | (this.green << 8) | this.blue\n }\n\n /**\n * Converts an RGB color value to HSB.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.\n * @returns An HSBColor object.\n */\n private toHSB(): ColorType {\n const red = this.red / 255\n const green = this.green / 255\n const blue = this.blue / 255\n const min = Math.min(red, green, blue)\n const brightness = Math.max(red, green, blue)\n const chroma = brightness - min\n const saturation = brightness === 0 ? 0 : chroma / brightness\n let hue = 0 // achromatic\n\n if (chroma !== 0) {\n switch (brightness) {\n case red:\n hue = (green - blue) / chroma + (green < blue ? 6 : 0)\n break\n case green:\n hue = (blue - red) / chroma + 2\n break\n case blue:\n hue = (red - green) / chroma + 4\n break\n }\n\n hue /= 6\n }\n\n return new HSBColor(\n toFixedNumber(hue * 360, 2),\n toFixedNumber(saturation * 100, 2),\n toFixedNumber(brightness * 100, 2),\n this.alpha,\n )\n }\n\n /**\n * Converts an RGB color value to HSL.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.\n * @returns An HSLColor object.\n */\n private toHSL(): ColorType {\n const red = this.red / 255\n const green = this.green / 255\n const blue = this.blue / 255\n const min = Math.min(red, green, blue)\n const max = Math.max(red, green, blue)\n const lightness = (max + min) / 2\n const chroma = max - min\n\n let hue = -1\n let saturation = -1\n\n if (chroma === 0) {\n hue = saturation = 0 // achromatic\n } else {\n saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min)\n\n switch (max) {\n case red:\n hue = (green - blue) / chroma + (green < blue ? 6 : 0)\n break\n case green:\n hue = (blue - red) / chroma + 2\n break\n case blue:\n hue = (red - green) / chroma + 4\n break\n }\n\n hue /= 6\n }\n\n return new HSLColor(\n toFixedNumber(hue * 360, 2),\n toFixedNumber(saturation * 100, 2),\n toFixedNumber(lightness * 100, 2),\n this.alpha,\n )\n }\n\n clone(): ColorType {\n return new RGBColor(this.red, this.green, this.blue, this.alpha)\n }\n\n getChannelRange(channel: ColorChannel): ColorChannelRange {\n switch (channel) {\n case \"red\":\n case \"green\":\n case \"blue\":\n return { minValue: 0x0, maxValue: 0xff, step: 0x1, pageSize: 0x11 }\n case \"alpha\":\n return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }\n default:\n throw new Error(\"Unknown color channel: \" + channel)\n }\n }\n\n getColorSpace(): ColorFormat {\n return \"rgb\"\n }\n\n private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = [\"red\", \"green\", \"blue\"]\n\n getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {\n return RGBColor.colorChannels\n }\n}\n","import { Color } from \"./color\"\nimport { HSBColor } from \"./hsb-color\"\nimport { RGBColor } from \"./rgb-color\"\nimport { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from \"./types\"\nimport { clampValue, mod, toFixedNumber } from \"./utils\"\n\nexport const HSL_REGEX =\n /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+)?)\\)/\n\nexport class HSLColor extends Color {\n constructor(\n private hue: number,\n private saturation: number,\n private lightness: number,\n private alpha: number,\n ) {\n super()\n }\n\n static parse(value: string): HSLColor | void {\n let m: RegExpMatchArray | null\n if ((m = value.match(HSL_REGEX))) {\n const [h, s, l, a] = (m[1] ?? m[2]).split(\",\").map((n) => Number(n.trim().replace(\"%\", \"\")))\n return new HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1))\n }\n }\n\n toString(format: ColorFormat | \"css\") {\n switch (format) {\n case \"hex\":\n return this.toRGB().toString(\"hex\")\n case \"hexa\":\n return this.toRGB().toString(\"hexa\")\n case \"hsl\":\n return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`\n case \"css\":\n case \"hsla\":\n return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${\n this.alpha\n })`\n default:\n return this.toFormat(format).toString(format)\n }\n }\n toFormat(format: ColorFormat): ColorType {\n switch (format) {\n case \"hsl\":\n case \"hsla\":\n return this\n case \"hsb\":\n case \"hsba\":\n return this.toHSB()\n case \"rgb\":\n case \"rgba\":\n return this.toRGB()\n default:\n throw new Error(\"Unsupported color conversion: hsl -> \" + format)\n }\n }\n\n /**\n * Converts a HSL color to HSB.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.\n * @returns An HSBColor object.\n */\n private toHSB(): ColorType {\n let saturation = this.saturation / 100\n let lightness = this.lightness / 100\n let brightness = lightness + saturation * Math.min(lightness, 1 - lightness)\n saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness)\n return new HSBColor(\n toFixedNumber(this.hue, 2),\n toFixedNumber(saturation * 100, 2),\n toFixedNumber(brightness * 100, 2),\n this.alpha,\n )\n }\n\n /**\n * Converts a HSL color to RGB.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.\n * @returns An RGBColor object.\n */\n private toRGB(): ColorType {\n let hue = this.hue\n let saturation = this.saturation / 100\n let lightness = this.lightness / 100\n let a = saturation * Math.min(lightness, 1 - lightness)\n let fn = (n: number, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)\n return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha)\n }\n\n clone(): ColorType {\n return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha)\n }\n\n getChannelRange(channel: ColorChannel): ColorChannelRange {\n switch (channel) {\n case \"hue\":\n return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 }\n case \"saturation\":\n case \"lightness\":\n return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 }\n case \"alpha\":\n return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }\n default:\n throw new Error(\"Unknown color channel: \" + channel)\n }\n }\n\n getColorSpace(): ColorFormat {\n return \"hsl\"\n }\n\n private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = [\"hue\", \"saturation\", \"lightness\"]\n\n getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {\n return HSLColor.colorChannels\n }\n}\n","import { Color } from \"./color\"\nimport { HSLColor } from \"./hsl-color\"\nimport { RGBColor } from \"./rgb-color\"\nimport { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from \"./types\"\nimport { clampValue, mod, toFixedNumber } from \"./utils\"\n\nconst HSB_REGEX =\n /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+)?)\\)/\n\nexport class HSBColor extends Color {\n constructor(\n private hue: number,\n private saturation: number,\n private brightness: number,\n private alpha: number,\n ) {\n super()\n }\n\n static parse(value: string): HSBColor | void {\n let m: RegExpMatchArray | null\n if ((m = value.match(HSB_REGEX))) {\n const [h, s, b, a] = (m[1] ?? m[2]).split(\",\").map((n) => Number(n.trim().replace(\"%\", \"\")))\n return new HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1))\n }\n }\n\n toString(format: ColorFormat | \"css\") {\n switch (format) {\n case \"css\":\n return this.toHSL().toString(\"css\")\n case \"hex\":\n return this.toRGB().toString(\"hex\")\n case \"hexa\":\n return this.toRGB().toString(\"hexa\")\n case \"hsb\":\n return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`\n case \"hsba\":\n return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${\n this.alpha\n })`\n default:\n return this.toFormat(format).toString(format)\n }\n }\n\n toFormat(format: ColorFormat): ColorType {\n switch (format) {\n case \"hsb\":\n case \"hsba\":\n return this\n case \"hsl\":\n case \"hsla\":\n return this.toHSL()\n case \"rgb\":\n case \"rgba\":\n return this.toRGB()\n default:\n throw new Error(\"Unsupported color conversion: hsb -> \" + format)\n }\n }\n\n /**\n * Converts a HSB color to HSL.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.\n * @returns An HSLColor object.\n */\n private toHSL(): ColorType {\n let saturation = this.saturation / 100\n let brightness = this.brightness / 100\n let lightness = brightness * (1 - saturation / 2)\n saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness)\n\n return new HSLColor(\n toFixedNumber(this.hue, 2),\n toFixedNumber(saturation * 100, 2),\n toFixedNumber(lightness * 100, 2),\n this.alpha,\n )\n }\n\n /**\n * Converts a HSV color value to RGB.\n * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.\n * @returns An RGBColor object.\n */\n private toRGB(): ColorType {\n let hue = this.hue\n let saturation = this.saturation / 100\n let brightness = this.brightness / 100\n\n let fn = (n: number, k = (n + hue / 60) % 6) =>\n brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0)\n\n return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha)\n }\n\n clone(): ColorType {\n return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha)\n }\n\n getChannelRange(channel: ColorChannel): ColorChannelRange {\n switch (channel) {\n case \"hue\":\n return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 }\n case \"saturation\":\n case \"brightness\":\n return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 }\n case \"alpha\":\n return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }\n default:\n throw new Error(\"Unknown color channel: \" + channel)\n }\n }\n\n getColorSpace(): ColorFormat {\n return \"hsb\"\n }\n\n private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = [\"hue\", \"saturation\", \"brightness\"]\n\n getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {\n return HSBColor.colorChannels\n }\n}\n","import { HSBColor } from \"./hsb-color\"\nimport { HSLColor } from \"./hsl-color\"\nimport { RGBColor } from \"./rgb-color\"\nimport { ColorType } from \"./types\"\n\nexport function parseColor(value: string): ColorType {\n let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value)\n if (!result) {\n throw new Error(\"Invalid color value: \" + value)\n }\n return result\n}\n\nexport function normalizeColor(v: string | ColorType) {\n if (typeof v === \"string\") {\n return parseColor(v)\n } else {\n return v\n }\n}\n"],"mappings":";;;;;;;;AAEO,IAAe,QAAf,MAA0C;AAAA,EAQ/C,WAAmB;AACjB,WAAO,KAAK,SAAS,KAAK,EAAE,SAAS;AAAA,EACvC;AAAA,EAEA,gBAAgB,SAA+B;AAC7C,QAAI,WAAW,MAAM;AACnB,aAAO,KAAK,OAAO;AAAA,IACrB;AAEA,UAAM,IAAI,MAAM,gCAAgC,OAAO;AAAA,EACzD;AAAA,EAEA,iBAAiB,SAAuB,OAA0B;AAChE,QAAI,WAAW,MAAM;AACnB,UAAI,QAAQ,KAAK,MAAM;AACvB,YAAM,OAAO,IAAI;AACjB,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,gCAAgC,OAAO;AAAA,EACzD;AAAA,EAEA,kBAAkB,YAA6E;AAC7F,QAAI,EAAE,UAAU,SAAS,IAAI;AAC7B,QAAI,MAAM,YAAY,KAAK,iBAAiB,EAAE,KAAK,CAAC,MAAM,MAAM,QAAQ;AACxE,QAAI,MAAM,YAAY,KAAK,iBAAiB,EAAE,KAAK,CAAC,MAAM,MAAM,GAAG;AACnE,QAAI,MAAM,KAAK,iBAAiB,EAAE,KAAK,CAAC,MAAM,MAAM,O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,CAAC,IAAI,GAAG,GAAG,KAAK,KAAK;AAAA,EAC3G;AAAA,EAEA,QAAmB;AACjB,WAAO,IAAI,UAAS,KAAK,KAAK,KAAK,YAAY,KAAK,YAAY,KAAK,KAAK;AAAA,EAC5E;AAAA,EAEA,gBAAgB,SAA0C;AACxD,YAAQ,SAAS;AAAA,MACf,KAAK;AACH,eAAO,EAAE,UAAU,GAAG,UAAU,KAAK,MAAM,GAAG,UAAU,GAAG;AAAA,MAC7D,KAAK;AAAA,MACL,KAAK;AACH,eAAO,EAAE,UAAU,GAAG,UAAU,KAAK,MAAM,GAAG,UAAU,GAAG;AAAA,MAC7D,KAAK;AACH,eAAO,EAAE,UAAU,GAAG,UAAU,GAAG,MAAM,MAAM,UAAU,IAAI;AAAA,MAC/D;AACE,cAAM,IAAI,MAAM,4BAA4B,OAAO;AAAA,IACvD;AAAA,EACF;AAAA,EAEA,gBAA6B;AAC3B,WAAO;AAAA,EACT;AAAA,EAIA,mBAA+D;AAC7D,WAAO,UAAS;AAAA,EAClB;AACF;AALE,cA9GW,WA8GI,iBAA4D,CAAC,OAAO,cAAc,YAAY;AA9GxG,IAAM,WAAN;;;ACJA,SAAS,WAAW,OAA0B;AACnD,MAAI,SAAS,SAAS,MAAM,KAAK,KAAK,SAAS,MAAM,KAAK,KAAK,SAAS,MAAM,KAAK;AACnF,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,0BAA0B,KAAK;AAAA,EACjD;AACA,SAAO;AACT;AAEO,SAAS,eAAe,GAAuB;AACpD,MAAI,OAAO,MAAM,UAAU;AACzB,WAAO,WAAW,CAAC;AAAA,EACrB,OAAO;AACL,WAAO;AAAA,EACT;AACF;","names":["value"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/color-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Color utilities for zag.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"./package.json": "./package.json"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
|
-
"build": "
|
|
41
|
+
"build": "tsup",
|
|
42
42
|
"lint": "eslint src --ext .ts,.tsx",
|
|
43
43
|
"typecheck": "tsc --noEmit"
|
|
44
44
|
}
|
package/src/hsb-color.ts
CHANGED
|
@@ -8,7 +8,12 @@ const HSB_REGEX =
|
|
|
8
8
|
/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+)?)\)/
|
|
9
9
|
|
|
10
10
|
export class HSBColor extends Color {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(
|
|
12
|
+
private hue: number,
|
|
13
|
+
private saturation: number,
|
|
14
|
+
private brightness: number,
|
|
15
|
+
private alpha: number,
|
|
16
|
+
) {
|
|
12
17
|
super()
|
|
13
18
|
}
|
|
14
19
|
|
package/src/hsl-color.ts
CHANGED
|
@@ -8,7 +8,12 @@ export const HSL_REGEX =
|
|
|
8
8
|
/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+)?)\)/
|
|
9
9
|
|
|
10
10
|
export class HSLColor extends Color {
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(
|
|
12
|
+
private hue: number,
|
|
13
|
+
private saturation: number,
|
|
14
|
+
private lightness: number,
|
|
15
|
+
private alpha: number,
|
|
16
|
+
) {
|
|
12
17
|
super()
|
|
13
18
|
}
|
|
14
19
|
|
package/src/rgb-color.ts
CHANGED
|
@@ -5,7 +5,12 @@ import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types
|
|
|
5
5
|
import { clampValue, toFixedNumber } from "./utils"
|
|
6
6
|
|
|
7
7
|
export class RGBColor extends Color {
|
|
8
|
-
constructor(
|
|
8
|
+
constructor(
|
|
9
|
+
private red: number,
|
|
10
|
+
private green: number,
|
|
11
|
+
private blue: number,
|
|
12
|
+
private alpha: number,
|
|
13
|
+
) {
|
|
9
14
|
super()
|
|
10
15
|
}
|
|
11
16
|
|
package/dist/color.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { ColorType, ColorFormat, ColorChannel, ColorChannelRange, ColorAxes } from "./types";
|
|
2
|
-
export declare abstract class Color implements ColorType {
|
|
3
|
-
abstract toFormat(format: ColorFormat): ColorType;
|
|
4
|
-
abstract toString(format: ColorFormat | "css"): string;
|
|
5
|
-
abstract clone(): ColorType;
|
|
6
|
-
abstract getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
7
|
-
abstract getColorSpace(): ColorFormat;
|
|
8
|
-
abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
9
|
-
toHexInt(): number;
|
|
10
|
-
getChannelValue(channel: ColorChannel): number;
|
|
11
|
-
withChannelValue(channel: ColorChannel, value: number): ColorType;
|
|
12
|
-
getColorSpaceAxes(xyChannels: {
|
|
13
|
-
xChannel?: ColorChannel;
|
|
14
|
-
yChannel?: ColorChannel;
|
|
15
|
-
}): ColorAxes;
|
|
16
|
-
isEqual(color: ColorType): boolean;
|
|
17
|
-
}
|
package/dist/color.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
class Color {
|
|
6
|
-
toHexInt() {
|
|
7
|
-
return this.toFormat("rgb").toHexInt();
|
|
8
|
-
}
|
|
9
|
-
getChannelValue(channel) {
|
|
10
|
-
if (channel in this) {
|
|
11
|
-
return this[channel];
|
|
12
|
-
}
|
|
13
|
-
throw new Error("Unsupported color channel: " + channel);
|
|
14
|
-
}
|
|
15
|
-
withChannelValue(channel, value) {
|
|
16
|
-
if (channel in this) {
|
|
17
|
-
let clone = this.clone();
|
|
18
|
-
clone[channel] = value;
|
|
19
|
-
return clone;
|
|
20
|
-
}
|
|
21
|
-
throw new Error("Unsupported color channel: " + channel);
|
|
22
|
-
}
|
|
23
|
-
getColorSpaceAxes(xyChannels) {
|
|
24
|
-
let { xChannel, yChannel } = xyChannels;
|
|
25
|
-
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
26
|
-
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
27
|
-
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
28
|
-
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
29
|
-
}
|
|
30
|
-
isEqual(color) {
|
|
31
|
-
return this.toHexInt() === color.toHexInt();
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
exports.Color = Color;
|
package/dist/color.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
class Color {
|
|
2
|
-
toHexInt() {
|
|
3
|
-
return this.toFormat("rgb").toHexInt();
|
|
4
|
-
}
|
|
5
|
-
getChannelValue(channel) {
|
|
6
|
-
if (channel in this) {
|
|
7
|
-
return this[channel];
|
|
8
|
-
}
|
|
9
|
-
throw new Error("Unsupported color channel: " + channel);
|
|
10
|
-
}
|
|
11
|
-
withChannelValue(channel, value) {
|
|
12
|
-
if (channel in this) {
|
|
13
|
-
let clone = this.clone();
|
|
14
|
-
clone[channel] = value;
|
|
15
|
-
return clone;
|
|
16
|
-
}
|
|
17
|
-
throw new Error("Unsupported color channel: " + channel);
|
|
18
|
-
}
|
|
19
|
-
getColorSpaceAxes(xyChannels) {
|
|
20
|
-
let { xChannel, yChannel } = xyChannels;
|
|
21
|
-
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
22
|
-
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
23
|
-
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
24
|
-
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
25
|
-
}
|
|
26
|
-
isEqual(color) {
|
|
27
|
-
return this.toHexInt() === color.toHexInt();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { Color };
|
package/dist/hsb-color.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Color } from "./color";
|
|
2
|
-
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types";
|
|
3
|
-
export declare class HSBColor extends Color {
|
|
4
|
-
private hue;
|
|
5
|
-
private saturation;
|
|
6
|
-
private brightness;
|
|
7
|
-
private alpha;
|
|
8
|
-
constructor(hue: number, saturation: number, brightness: number, alpha: number);
|
|
9
|
-
static parse(value: string): HSBColor | void;
|
|
10
|
-
toString(format: ColorFormat | "css"): string;
|
|
11
|
-
toFormat(format: ColorFormat): ColorType;
|
|
12
|
-
/**
|
|
13
|
-
* Converts a HSB color to HSL.
|
|
14
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
15
|
-
* @returns An HSLColor object.
|
|
16
|
-
*/
|
|
17
|
-
private toHSL;
|
|
18
|
-
/**
|
|
19
|
-
* Converts a HSV color value to RGB.
|
|
20
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
21
|
-
* @returns An RGBColor object.
|
|
22
|
-
*/
|
|
23
|
-
private toRGB;
|
|
24
|
-
clone(): ColorType;
|
|
25
|
-
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
26
|
-
getColorSpace(): ColorFormat;
|
|
27
|
-
private static colorChannels;
|
|
28
|
-
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
29
|
-
}
|