@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Chakra UI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @zag-js/color-utils
|
|
2
|
+
|
|
3
|
+
Color utilities for zag.js
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add @zag-js/color-utils
|
|
9
|
+
# or
|
|
10
|
+
npm i @zag-js/color-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Contribution
|
|
14
|
+
|
|
15
|
+
Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
|
|
16
|
+
|
|
17
|
+
## Licence
|
|
18
|
+
|
|
19
|
+
This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
|
|
20
|
+
|
|
21
|
+
## Credits
|
|
22
|
+
|
|
23
|
+
This package is largely inspired by the
|
|
24
|
+
[Color utilities](https://github.com/adobe/react-spectrum/tree/main/packages/%40react-stately/color) in the React Aria
|
|
25
|
+
project. You can find the license for this project [here](https://github.com/adobe/react-spectrum/blob/main/LICENSE)
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Color
|
|
3
|
+
} from "./chunk-NSR5W5ST.mjs";
|
|
4
|
+
import {
|
|
5
|
+
clampValue,
|
|
6
|
+
mod,
|
|
7
|
+
toFixedNumber
|
|
8
|
+
} from "./chunk-TXOVH6IE.mjs";
|
|
9
|
+
import {
|
|
10
|
+
__publicField
|
|
11
|
+
} from "./chunk-NHABU752.mjs";
|
|
12
|
+
|
|
13
|
+
// src/hsl-color.ts
|
|
14
|
+
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+)?)\)/;
|
|
15
|
+
var _HSLColor = class extends Color {
|
|
16
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
17
|
+
super();
|
|
18
|
+
this.hue = hue;
|
|
19
|
+
this.saturation = saturation;
|
|
20
|
+
this.lightness = lightness;
|
|
21
|
+
this.alpha = alpha;
|
|
22
|
+
}
|
|
23
|
+
static parse(value) {
|
|
24
|
+
let m;
|
|
25
|
+
if (m = value.match(HSL_REGEX)) {
|
|
26
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
27
|
+
return new _HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
toString(format) {
|
|
31
|
+
switch (format) {
|
|
32
|
+
case "hex":
|
|
33
|
+
return this.toRGB().toString("hex");
|
|
34
|
+
case "hexa":
|
|
35
|
+
return this.toRGB().toString("hexa");
|
|
36
|
+
case "hsl":
|
|
37
|
+
return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
|
|
38
|
+
case "css":
|
|
39
|
+
case "hsla":
|
|
40
|
+
return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
41
|
+
default:
|
|
42
|
+
return this.toFormat(format).toString(format);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
toFormat(format) {
|
|
46
|
+
switch (format) {
|
|
47
|
+
case "hsl":
|
|
48
|
+
case "hsla":
|
|
49
|
+
return this;
|
|
50
|
+
case "hsb":
|
|
51
|
+
case "hsba":
|
|
52
|
+
return this.toHSB();
|
|
53
|
+
case "rgb":
|
|
54
|
+
case "rgba":
|
|
55
|
+
return this.toRGB();
|
|
56
|
+
default:
|
|
57
|
+
throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts a HSL color to HSB.
|
|
62
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
63
|
+
* @returns An HSBColor object.
|
|
64
|
+
*/
|
|
65
|
+
toHSB() {
|
|
66
|
+
let saturation = this.saturation / 100;
|
|
67
|
+
let lightness = this.lightness / 100;
|
|
68
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
69
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
70
|
+
return new HSBColor(
|
|
71
|
+
toFixedNumber(this.hue, 2),
|
|
72
|
+
toFixedNumber(saturation * 100, 2),
|
|
73
|
+
toFixedNumber(brightness * 100, 2),
|
|
74
|
+
this.alpha
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Converts a HSL color to RGB.
|
|
79
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
80
|
+
* @returns An RGBColor object.
|
|
81
|
+
*/
|
|
82
|
+
toRGB() {
|
|
83
|
+
let hue = this.hue;
|
|
84
|
+
let saturation = this.saturation / 100;
|
|
85
|
+
let lightness = this.lightness / 100;
|
|
86
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
87
|
+
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
88
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
89
|
+
}
|
|
90
|
+
clone() {
|
|
91
|
+
return new _HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
92
|
+
}
|
|
93
|
+
getChannelRange(channel) {
|
|
94
|
+
switch (channel) {
|
|
95
|
+
case "hue":
|
|
96
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
97
|
+
case "saturation":
|
|
98
|
+
case "lightness":
|
|
99
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
100
|
+
case "alpha":
|
|
101
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
102
|
+
default:
|
|
103
|
+
throw new Error("Unknown color channel: " + channel);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
getColorSpace() {
|
|
107
|
+
return "hsl";
|
|
108
|
+
}
|
|
109
|
+
getColorChannels() {
|
|
110
|
+
return _HSLColor.colorChannels;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
var HSLColor = _HSLColor;
|
|
114
|
+
__publicField(HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
|
|
115
|
+
|
|
116
|
+
// src/hsb-color.ts
|
|
117
|
+
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+)?)\)/;
|
|
118
|
+
var _HSBColor = class extends Color {
|
|
119
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
120
|
+
super();
|
|
121
|
+
this.hue = hue;
|
|
122
|
+
this.saturation = saturation;
|
|
123
|
+
this.brightness = brightness;
|
|
124
|
+
this.alpha = alpha;
|
|
125
|
+
}
|
|
126
|
+
static parse(value) {
|
|
127
|
+
let m;
|
|
128
|
+
if (m = value.match(HSB_REGEX)) {
|
|
129
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
130
|
+
return new _HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
toString(format) {
|
|
134
|
+
switch (format) {
|
|
135
|
+
case "css":
|
|
136
|
+
return this.toHSL().toString("css");
|
|
137
|
+
case "hex":
|
|
138
|
+
return this.toRGB().toString("hex");
|
|
139
|
+
case "hexa":
|
|
140
|
+
return this.toRGB().toString("hexa");
|
|
141
|
+
case "hsb":
|
|
142
|
+
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
|
|
143
|
+
case "hsba":
|
|
144
|
+
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
145
|
+
default:
|
|
146
|
+
return this.toFormat(format).toString(format);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
toFormat(format) {
|
|
150
|
+
switch (format) {
|
|
151
|
+
case "hsb":
|
|
152
|
+
case "hsba":
|
|
153
|
+
return this;
|
|
154
|
+
case "hsl":
|
|
155
|
+
case "hsla":
|
|
156
|
+
return this.toHSL();
|
|
157
|
+
case "rgb":
|
|
158
|
+
case "rgba":
|
|
159
|
+
return this.toRGB();
|
|
160
|
+
default:
|
|
161
|
+
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Converts a HSB color to HSL.
|
|
166
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
167
|
+
* @returns An HSLColor object.
|
|
168
|
+
*/
|
|
169
|
+
toHSL() {
|
|
170
|
+
let saturation = this.saturation / 100;
|
|
171
|
+
let brightness = this.brightness / 100;
|
|
172
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
173
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
174
|
+
return new HSLColor(
|
|
175
|
+
toFixedNumber(this.hue, 2),
|
|
176
|
+
toFixedNumber(saturation * 100, 2),
|
|
177
|
+
toFixedNumber(lightness * 100, 2),
|
|
178
|
+
this.alpha
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Converts a HSV color value to RGB.
|
|
183
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
184
|
+
* @returns An RGBColor object.
|
|
185
|
+
*/
|
|
186
|
+
toRGB() {
|
|
187
|
+
let hue = this.hue;
|
|
188
|
+
let saturation = this.saturation / 100;
|
|
189
|
+
let brightness = this.brightness / 100;
|
|
190
|
+
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
191
|
+
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
192
|
+
}
|
|
193
|
+
clone() {
|
|
194
|
+
return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
195
|
+
}
|
|
196
|
+
getChannelRange(channel) {
|
|
197
|
+
switch (channel) {
|
|
198
|
+
case "hue":
|
|
199
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
200
|
+
case "saturation":
|
|
201
|
+
case "brightness":
|
|
202
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
203
|
+
case "alpha":
|
|
204
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
205
|
+
default:
|
|
206
|
+
throw new Error("Unknown color channel: " + channel);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
getColorSpace() {
|
|
210
|
+
return "hsb";
|
|
211
|
+
}
|
|
212
|
+
getColorChannels() {
|
|
213
|
+
return _HSBColor.colorChannels;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
var HSBColor = _HSBColor;
|
|
217
|
+
__publicField(HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
|
|
218
|
+
|
|
219
|
+
// src/rgb-color.ts
|
|
220
|
+
var _RGBColor = class extends Color {
|
|
221
|
+
constructor(red, green, blue, alpha) {
|
|
222
|
+
super();
|
|
223
|
+
this.red = red;
|
|
224
|
+
this.green = green;
|
|
225
|
+
this.blue = blue;
|
|
226
|
+
this.alpha = alpha;
|
|
227
|
+
}
|
|
228
|
+
static parse(value) {
|
|
229
|
+
let colors = [];
|
|
230
|
+
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
231
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
232
|
+
while (values.length > 0) {
|
|
233
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
234
|
+
}
|
|
235
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
236
|
+
}
|
|
237
|
+
const match = value.match(/^rgba?\((.*)\)$/);
|
|
238
|
+
if (match?.[1]) {
|
|
239
|
+
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
|
|
240
|
+
}
|
|
241
|
+
return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
242
|
+
}
|
|
243
|
+
toString(format) {
|
|
244
|
+
switch (format) {
|
|
245
|
+
case "hex":
|
|
246
|
+
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
247
|
+
case "hexa":
|
|
248
|
+
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();
|
|
249
|
+
case "rgb":
|
|
250
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
251
|
+
case "css":
|
|
252
|
+
case "rgba":
|
|
253
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
254
|
+
default:
|
|
255
|
+
return this.toFormat(format).toString(format);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
toFormat(format) {
|
|
259
|
+
switch (format) {
|
|
260
|
+
case "hex":
|
|
261
|
+
case "hexa":
|
|
262
|
+
case "rgb":
|
|
263
|
+
case "rgba":
|
|
264
|
+
return this;
|
|
265
|
+
case "hsb":
|
|
266
|
+
case "hsba":
|
|
267
|
+
return this.toHSB();
|
|
268
|
+
case "hsl":
|
|
269
|
+
case "hsla":
|
|
270
|
+
return this.toHSL();
|
|
271
|
+
default:
|
|
272
|
+
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
toHexInt() {
|
|
276
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Converts an RGB color value to HSB.
|
|
280
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
281
|
+
* @returns An HSBColor object.
|
|
282
|
+
*/
|
|
283
|
+
toHSB() {
|
|
284
|
+
const red = this.red / 255;
|
|
285
|
+
const green = this.green / 255;
|
|
286
|
+
const blue = this.blue / 255;
|
|
287
|
+
const min = Math.min(red, green, blue);
|
|
288
|
+
const brightness = Math.max(red, green, blue);
|
|
289
|
+
const chroma = brightness - min;
|
|
290
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
291
|
+
let hue = 0;
|
|
292
|
+
if (chroma !== 0) {
|
|
293
|
+
switch (brightness) {
|
|
294
|
+
case red:
|
|
295
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
296
|
+
break;
|
|
297
|
+
case green:
|
|
298
|
+
hue = (blue - red) / chroma + 2;
|
|
299
|
+
break;
|
|
300
|
+
case blue:
|
|
301
|
+
hue = (red - green) / chroma + 4;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
hue /= 6;
|
|
305
|
+
}
|
|
306
|
+
return new HSBColor(
|
|
307
|
+
toFixedNumber(hue * 360, 2),
|
|
308
|
+
toFixedNumber(saturation * 100, 2),
|
|
309
|
+
toFixedNumber(brightness * 100, 2),
|
|
310
|
+
this.alpha
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Converts an RGB color value to HSL.
|
|
315
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
316
|
+
* @returns An HSLColor object.
|
|
317
|
+
*/
|
|
318
|
+
toHSL() {
|
|
319
|
+
const red = this.red / 255;
|
|
320
|
+
const green = this.green / 255;
|
|
321
|
+
const blue = this.blue / 255;
|
|
322
|
+
const min = Math.min(red, green, blue);
|
|
323
|
+
const max = Math.max(red, green, blue);
|
|
324
|
+
const lightness = (max + min) / 2;
|
|
325
|
+
const chroma = max - min;
|
|
326
|
+
let hue = -1;
|
|
327
|
+
let saturation = -1;
|
|
328
|
+
if (chroma === 0) {
|
|
329
|
+
hue = saturation = 0;
|
|
330
|
+
} else {
|
|
331
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
332
|
+
switch (max) {
|
|
333
|
+
case red:
|
|
334
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
335
|
+
break;
|
|
336
|
+
case green:
|
|
337
|
+
hue = (blue - red) / chroma + 2;
|
|
338
|
+
break;
|
|
339
|
+
case blue:
|
|
340
|
+
hue = (red - green) / chroma + 4;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
hue /= 6;
|
|
344
|
+
}
|
|
345
|
+
return new HSLColor(
|
|
346
|
+
toFixedNumber(hue * 360, 2),
|
|
347
|
+
toFixedNumber(saturation * 100, 2),
|
|
348
|
+
toFixedNumber(lightness * 100, 2),
|
|
349
|
+
this.alpha
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
clone() {
|
|
353
|
+
return new _RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
354
|
+
}
|
|
355
|
+
getChannelRange(channel) {
|
|
356
|
+
switch (channel) {
|
|
357
|
+
case "red":
|
|
358
|
+
case "green":
|
|
359
|
+
case "blue":
|
|
360
|
+
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
361
|
+
case "alpha":
|
|
362
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
363
|
+
default:
|
|
364
|
+
throw new Error("Unknown color channel: " + channel);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
getColorSpace() {
|
|
368
|
+
return "rgb";
|
|
369
|
+
}
|
|
370
|
+
getColorChannels() {
|
|
371
|
+
return _RGBColor.colorChannels;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
var RGBColor = _RGBColor;
|
|
375
|
+
__publicField(RGBColor, "colorChannels", ["red", "green", "blue"]);
|
|
376
|
+
|
|
377
|
+
export {
|
|
378
|
+
RGBColor,
|
|
379
|
+
HSL_REGEX,
|
|
380
|
+
HSLColor,
|
|
381
|
+
HSBColor
|
|
382
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
export {
|
|
9
|
+
__publicField
|
|
10
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/color.ts
|
|
2
|
+
var Color = class {
|
|
3
|
+
toHexInt() {
|
|
4
|
+
return this.toFormat("rgb").toHexInt();
|
|
5
|
+
}
|
|
6
|
+
getChannelValue(channel) {
|
|
7
|
+
if (channel in this) {
|
|
8
|
+
return this[channel];
|
|
9
|
+
}
|
|
10
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
11
|
+
}
|
|
12
|
+
withChannelValue(channel, value) {
|
|
13
|
+
if (channel in this) {
|
|
14
|
+
let clone = this.clone();
|
|
15
|
+
clone[channel] = value;
|
|
16
|
+
return clone;
|
|
17
|
+
}
|
|
18
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
19
|
+
}
|
|
20
|
+
getColorSpaceAxes(xyChannels) {
|
|
21
|
+
let { xChannel, yChannel } = xyChannels;
|
|
22
|
+
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
23
|
+
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
24
|
+
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
25
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
26
|
+
}
|
|
27
|
+
isEqual(color) {
|
|
28
|
+
return this.toHexInt() === color.toHexInt();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
Color
|
|
34
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function mod(n, m) {
|
|
3
|
+
return (n % m + m) % m;
|
|
4
|
+
}
|
|
5
|
+
function toFixedNumber(num, digits) {
|
|
6
|
+
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
7
|
+
}
|
|
8
|
+
function clampValue(value, min, max) {
|
|
9
|
+
return Math.min(Math.max(value, min), max);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
mod,
|
|
14
|
+
toFixedNumber,
|
|
15
|
+
clampValue
|
|
16
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HSBColor,
|
|
3
|
+
HSLColor,
|
|
4
|
+
RGBColor
|
|
5
|
+
} from "./chunk-7VB2GGWQ.mjs";
|
|
6
|
+
|
|
7
|
+
// src/parse-color.ts
|
|
8
|
+
function parseColor(value) {
|
|
9
|
+
let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
10
|
+
if (!result) {
|
|
11
|
+
throw new Error("Invalid color value: " + value);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function normalizeColor(v) {
|
|
16
|
+
if (typeof v === "string") {
|
|
17
|
+
return parseColor(v);
|
|
18
|
+
} else {
|
|
19
|
+
return v;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
parseColor,
|
|
25
|
+
normalizeColor
|
|
26
|
+
};
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ColorType, ColorFormat, ColorChannel, ColorChannelRange, ColorAxes } from './types.js';
|
|
2
|
+
|
|
3
|
+
declare abstract class Color implements ColorType {
|
|
4
|
+
abstract toFormat(format: ColorFormat): ColorType;
|
|
5
|
+
abstract toString(format: ColorFormat | "css"): string;
|
|
6
|
+
abstract clone(): ColorType;
|
|
7
|
+
abstract getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
8
|
+
abstract getColorSpace(): ColorFormat;
|
|
9
|
+
abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
10
|
+
toHexInt(): number;
|
|
11
|
+
getChannelValue(channel: ColorChannel): number;
|
|
12
|
+
withChannelValue(channel: ColorChannel, value: number): ColorType;
|
|
13
|
+
getColorSpaceAxes(xyChannels: {
|
|
14
|
+
xChannel?: ColorChannel;
|
|
15
|
+
yChannel?: ColorChannel;
|
|
16
|
+
}): ColorAxes;
|
|
17
|
+
isEqual(color: ColorType): boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { Color };
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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 = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/color.ts
|
|
21
|
+
var color_exports = {};
|
|
22
|
+
__export(color_exports, {
|
|
23
|
+
Color: () => Color
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(color_exports);
|
|
26
|
+
var Color = class {
|
|
27
|
+
toHexInt() {
|
|
28
|
+
return this.toFormat("rgb").toHexInt();
|
|
29
|
+
}
|
|
30
|
+
getChannelValue(channel) {
|
|
31
|
+
if (channel in this) {
|
|
32
|
+
return this[channel];
|
|
33
|
+
}
|
|
34
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
35
|
+
}
|
|
36
|
+
withChannelValue(channel, value) {
|
|
37
|
+
if (channel in this) {
|
|
38
|
+
let clone = this.clone();
|
|
39
|
+
clone[channel] = value;
|
|
40
|
+
return clone;
|
|
41
|
+
}
|
|
42
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
43
|
+
}
|
|
44
|
+
getColorSpaceAxes(xyChannels) {
|
|
45
|
+
let { xChannel, yChannel } = xyChannels;
|
|
46
|
+
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel);
|
|
47
|
+
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh);
|
|
48
|
+
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh);
|
|
49
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
50
|
+
}
|
|
51
|
+
isEqual(color) {
|
|
52
|
+
return this.toHexInt() === color.toHexInt();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
56
|
+
0 && (module.exports = {
|
|
57
|
+
Color
|
|
58
|
+
});
|
package/dist/color.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Color } from './color.js';
|
|
2
|
+
import { ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare class HSBColor extends Color {
|
|
5
|
+
private hue;
|
|
6
|
+
private saturation;
|
|
7
|
+
private brightness;
|
|
8
|
+
private alpha;
|
|
9
|
+
constructor(hue: number, saturation: number, brightness: number, alpha: number);
|
|
10
|
+
static parse(value: string): HSBColor | void;
|
|
11
|
+
toString(format: ColorFormat | "css"): string;
|
|
12
|
+
toFormat(format: ColorFormat): ColorType;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a HSB color to HSL.
|
|
15
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
16
|
+
* @returns An HSLColor object.
|
|
17
|
+
*/
|
|
18
|
+
private toHSL;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a HSV color value to RGB.
|
|
21
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
22
|
+
* @returns An RGBColor object.
|
|
23
|
+
*/
|
|
24
|
+
private toRGB;
|
|
25
|
+
clone(): ColorType;
|
|
26
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
27
|
+
getColorSpace(): ColorFormat;
|
|
28
|
+
private static colorChannels;
|
|
29
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { HSBColor };
|