@zag-js/color-utils 0.10.5 → 0.11.1
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/rgb-color.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const color = require('./color.js');
|
|
6
|
-
const hsbColor = require('./hsb-color.js');
|
|
7
|
-
const hslColor = require('./hsl-color.js');
|
|
8
|
-
const utils = require('./utils.js');
|
|
9
|
-
|
|
10
|
-
class RGBColor extends color.Color {
|
|
11
|
-
constructor(red, green, blue, alpha) {
|
|
12
|
-
super();
|
|
13
|
-
this.red = red;
|
|
14
|
-
this.green = green;
|
|
15
|
-
this.blue = blue;
|
|
16
|
-
this.alpha = alpha;
|
|
17
|
-
}
|
|
18
|
-
static parse(value) {
|
|
19
|
-
let colors = [];
|
|
20
|
-
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
21
|
-
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
22
|
-
while (values.length > 0) {
|
|
23
|
-
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
24
|
-
}
|
|
25
|
-
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
26
|
-
}
|
|
27
|
-
const match = value.match(/^rgba?\((.*)\)$/);
|
|
28
|
-
if (match?.[1]) {
|
|
29
|
-
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => utils.clampValue(num, 0, i < 3 ? 255 : 1));
|
|
30
|
-
}
|
|
31
|
-
return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
32
|
-
}
|
|
33
|
-
toString(format) {
|
|
34
|
-
switch (format) {
|
|
35
|
-
case "hex":
|
|
36
|
-
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
37
|
-
case "hexa":
|
|
38
|
-
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();
|
|
39
|
-
case "rgb":
|
|
40
|
-
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
41
|
-
case "css":
|
|
42
|
-
case "rgba":
|
|
43
|
-
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
44
|
-
default:
|
|
45
|
-
return this.toFormat(format).toString(format);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
toFormat(format) {
|
|
49
|
-
switch (format) {
|
|
50
|
-
case "hex":
|
|
51
|
-
case "hexa":
|
|
52
|
-
case "rgb":
|
|
53
|
-
case "rgba":
|
|
54
|
-
return this;
|
|
55
|
-
case "hsb":
|
|
56
|
-
case "hsba":
|
|
57
|
-
return this.toHSB();
|
|
58
|
-
case "hsl":
|
|
59
|
-
case "hsla":
|
|
60
|
-
return this.toHSL();
|
|
61
|
-
default:
|
|
62
|
-
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
toHexInt() {
|
|
66
|
-
return this.red << 16 | this.green << 8 | this.blue;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Converts an RGB color value to HSB.
|
|
70
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
71
|
-
* @returns An HSBColor object.
|
|
72
|
-
*/
|
|
73
|
-
toHSB() {
|
|
74
|
-
const red = this.red / 255;
|
|
75
|
-
const green = this.green / 255;
|
|
76
|
-
const blue = this.blue / 255;
|
|
77
|
-
const min = Math.min(red, green, blue);
|
|
78
|
-
const brightness = Math.max(red, green, blue);
|
|
79
|
-
const chroma = brightness - min;
|
|
80
|
-
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
81
|
-
let hue = 0;
|
|
82
|
-
if (chroma !== 0) {
|
|
83
|
-
switch (brightness) {
|
|
84
|
-
case red:
|
|
85
|
-
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
86
|
-
break;
|
|
87
|
-
case green:
|
|
88
|
-
hue = (blue - red) / chroma + 2;
|
|
89
|
-
break;
|
|
90
|
-
case blue:
|
|
91
|
-
hue = (red - green) / chroma + 4;
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
hue /= 6;
|
|
95
|
-
}
|
|
96
|
-
return new hsbColor.HSBColor(
|
|
97
|
-
utils.toFixedNumber(hue * 360, 2),
|
|
98
|
-
utils.toFixedNumber(saturation * 100, 2),
|
|
99
|
-
utils.toFixedNumber(brightness * 100, 2),
|
|
100
|
-
this.alpha
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Converts an RGB color value to HSL.
|
|
105
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
106
|
-
* @returns An HSLColor object.
|
|
107
|
-
*/
|
|
108
|
-
toHSL() {
|
|
109
|
-
const red = this.red / 255;
|
|
110
|
-
const green = this.green / 255;
|
|
111
|
-
const blue = this.blue / 255;
|
|
112
|
-
const min = Math.min(red, green, blue);
|
|
113
|
-
const max = Math.max(red, green, blue);
|
|
114
|
-
const lightness = (max + min) / 2;
|
|
115
|
-
const chroma = max - min;
|
|
116
|
-
let hue = -1;
|
|
117
|
-
let saturation = -1;
|
|
118
|
-
if (chroma === 0) {
|
|
119
|
-
hue = saturation = 0;
|
|
120
|
-
} else {
|
|
121
|
-
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
122
|
-
switch (max) {
|
|
123
|
-
case red:
|
|
124
|
-
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
125
|
-
break;
|
|
126
|
-
case green:
|
|
127
|
-
hue = (blue - red) / chroma + 2;
|
|
128
|
-
break;
|
|
129
|
-
case blue:
|
|
130
|
-
hue = (red - green) / chroma + 4;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
hue /= 6;
|
|
134
|
-
}
|
|
135
|
-
return new hslColor.HSLColor(
|
|
136
|
-
utils.toFixedNumber(hue * 360, 2),
|
|
137
|
-
utils.toFixedNumber(saturation * 100, 2),
|
|
138
|
-
utils.toFixedNumber(lightness * 100, 2),
|
|
139
|
-
this.alpha
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
clone() {
|
|
143
|
-
return new RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
144
|
-
}
|
|
145
|
-
getChannelRange(channel) {
|
|
146
|
-
switch (channel) {
|
|
147
|
-
case "red":
|
|
148
|
-
case "green":
|
|
149
|
-
case "blue":
|
|
150
|
-
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
151
|
-
case "alpha":
|
|
152
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
153
|
-
default:
|
|
154
|
-
throw new Error("Unknown color channel: " + channel);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
getColorSpace() {
|
|
158
|
-
return "rgb";
|
|
159
|
-
}
|
|
160
|
-
static colorChannels = ["red", "green", "blue"];
|
|
161
|
-
getColorChannels() {
|
|
162
|
-
return RGBColor.colorChannels;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
exports.RGBColor = RGBColor;
|
package/dist/rgb-color.mjs
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { Color } from './color.mjs';
|
|
2
|
-
import { HSBColor } from './hsb-color.mjs';
|
|
3
|
-
import { HSLColor } from './hsl-color.mjs';
|
|
4
|
-
import { clampValue, toFixedNumber } from './utils.mjs';
|
|
5
|
-
|
|
6
|
-
class RGBColor extends Color {
|
|
7
|
-
constructor(red, green, blue, alpha) {
|
|
8
|
-
super();
|
|
9
|
-
this.red = red;
|
|
10
|
-
this.green = green;
|
|
11
|
-
this.blue = blue;
|
|
12
|
-
this.alpha = alpha;
|
|
13
|
-
}
|
|
14
|
-
static parse(value) {
|
|
15
|
-
let colors = [];
|
|
16
|
-
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
17
|
-
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("");
|
|
18
|
-
while (values.length > 0) {
|
|
19
|
-
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
20
|
-
}
|
|
21
|
-
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
22
|
-
}
|
|
23
|
-
const match = value.match(/^rgba?\((.*)\)$/);
|
|
24
|
-
if (match?.[1]) {
|
|
25
|
-
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
|
|
26
|
-
}
|
|
27
|
-
return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
28
|
-
}
|
|
29
|
-
toString(format) {
|
|
30
|
-
switch (format) {
|
|
31
|
-
case "hex":
|
|
32
|
-
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
33
|
-
case "hexa":
|
|
34
|
-
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();
|
|
35
|
-
case "rgb":
|
|
36
|
-
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
37
|
-
case "css":
|
|
38
|
-
case "rgba":
|
|
39
|
-
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
40
|
-
default:
|
|
41
|
-
return this.toFormat(format).toString(format);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
toFormat(format) {
|
|
45
|
-
switch (format) {
|
|
46
|
-
case "hex":
|
|
47
|
-
case "hexa":
|
|
48
|
-
case "rgb":
|
|
49
|
-
case "rgba":
|
|
50
|
-
return this;
|
|
51
|
-
case "hsb":
|
|
52
|
-
case "hsba":
|
|
53
|
-
return this.toHSB();
|
|
54
|
-
case "hsl":
|
|
55
|
-
case "hsla":
|
|
56
|
-
return this.toHSL();
|
|
57
|
-
default:
|
|
58
|
-
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
toHexInt() {
|
|
62
|
-
return this.red << 16 | this.green << 8 | this.blue;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Converts an RGB color value to HSB.
|
|
66
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
67
|
-
* @returns An HSBColor object.
|
|
68
|
-
*/
|
|
69
|
-
toHSB() {
|
|
70
|
-
const red = this.red / 255;
|
|
71
|
-
const green = this.green / 255;
|
|
72
|
-
const blue = this.blue / 255;
|
|
73
|
-
const min = Math.min(red, green, blue);
|
|
74
|
-
const brightness = Math.max(red, green, blue);
|
|
75
|
-
const chroma = brightness - min;
|
|
76
|
-
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
77
|
-
let hue = 0;
|
|
78
|
-
if (chroma !== 0) {
|
|
79
|
-
switch (brightness) {
|
|
80
|
-
case red:
|
|
81
|
-
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
82
|
-
break;
|
|
83
|
-
case green:
|
|
84
|
-
hue = (blue - red) / chroma + 2;
|
|
85
|
-
break;
|
|
86
|
-
case blue:
|
|
87
|
-
hue = (red - green) / chroma + 4;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
hue /= 6;
|
|
91
|
-
}
|
|
92
|
-
return new HSBColor(
|
|
93
|
-
toFixedNumber(hue * 360, 2),
|
|
94
|
-
toFixedNumber(saturation * 100, 2),
|
|
95
|
-
toFixedNumber(brightness * 100, 2),
|
|
96
|
-
this.alpha
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Converts an RGB color value to HSL.
|
|
101
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
102
|
-
* @returns An HSLColor object.
|
|
103
|
-
*/
|
|
104
|
-
toHSL() {
|
|
105
|
-
const red = this.red / 255;
|
|
106
|
-
const green = this.green / 255;
|
|
107
|
-
const blue = this.blue / 255;
|
|
108
|
-
const min = Math.min(red, green, blue);
|
|
109
|
-
const max = Math.max(red, green, blue);
|
|
110
|
-
const lightness = (max + min) / 2;
|
|
111
|
-
const chroma = max - min;
|
|
112
|
-
let hue = -1;
|
|
113
|
-
let saturation = -1;
|
|
114
|
-
if (chroma === 0) {
|
|
115
|
-
hue = saturation = 0;
|
|
116
|
-
} else {
|
|
117
|
-
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
118
|
-
switch (max) {
|
|
119
|
-
case red:
|
|
120
|
-
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
121
|
-
break;
|
|
122
|
-
case green:
|
|
123
|
-
hue = (blue - red) / chroma + 2;
|
|
124
|
-
break;
|
|
125
|
-
case blue:
|
|
126
|
-
hue = (red - green) / chroma + 4;
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
hue /= 6;
|
|
130
|
-
}
|
|
131
|
-
return new HSLColor(
|
|
132
|
-
toFixedNumber(hue * 360, 2),
|
|
133
|
-
toFixedNumber(saturation * 100, 2),
|
|
134
|
-
toFixedNumber(lightness * 100, 2),
|
|
135
|
-
this.alpha
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
clone() {
|
|
139
|
-
return new RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
140
|
-
}
|
|
141
|
-
getChannelRange(channel) {
|
|
142
|
-
switch (channel) {
|
|
143
|
-
case "red":
|
|
144
|
-
case "green":
|
|
145
|
-
case "blue":
|
|
146
|
-
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
147
|
-
case "alpha":
|
|
148
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
149
|
-
default:
|
|
150
|
-
throw new Error("Unknown color channel: " + channel);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
getColorSpace() {
|
|
154
|
-
return "rgb";
|
|
155
|
-
}
|
|
156
|
-
static colorChannels = ["red", "green", "blue"];
|
|
157
|
-
getColorChannels() {
|
|
158
|
-
return RGBColor.colorChannels;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export { RGBColor };
|
package/dist/utils.d.ts
DELETED
package/dist/utils.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
function mod(n, m) {
|
|
6
|
-
return (n % m + m) % m;
|
|
7
|
-
}
|
|
8
|
-
function toFixedNumber(num, digits) {
|
|
9
|
-
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
10
|
-
}
|
|
11
|
-
function clampValue(value, min, max) {
|
|
12
|
-
return Math.min(Math.max(value, min), max);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
exports.clampValue = clampValue;
|
|
16
|
-
exports.mod = mod;
|
|
17
|
-
exports.toFixedNumber = toFixedNumber;
|
package/dist/utils.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
function mod(n, m) {
|
|
2
|
-
return (n % m + m) % m;
|
|
3
|
-
}
|
|
4
|
-
function toFixedNumber(num, digits) {
|
|
5
|
-
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits);
|
|
6
|
-
}
|
|
7
|
-
function clampValue(value, min, max) {
|
|
8
|
-
return Math.min(Math.max(value, min), max);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export { clampValue, mod, toFixedNumber };
|