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