@zag-js/color-utils 1.34.1 → 1.35.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/area-gradient.d.mts +15 -0
- package/dist/area-gradient.d.ts +15 -0
- package/dist/area-gradient.js +87 -0
- package/dist/area-gradient.mjs +74 -0
- package/dist/chunk-QZ7TP4HQ.mjs +7 -0
- package/dist/color-format-gradient.d.mts +68 -0
- package/dist/color-format-gradient.d.ts +68 -0
- package/dist/color-format-gradient.js +164 -0
- package/dist/color-format-gradient.mjs +133 -0
- package/dist/color.d.mts +23 -0
- package/dist/color.d.ts +23 -0
- package/dist/color.js +87 -0
- package/dist/color.mjs +64 -0
- package/dist/hsb-color.d.mts +35 -0
- package/dist/hsb-color.d.ts +35 -0
- package/dist/hsb-color.js +165 -0
- package/dist/hsb-color.mjs +142 -0
- package/dist/hsl-color.d.mts +36 -0
- package/dist/hsl-color.d.ts +36 -0
- package/dist/hsl-color.js +167 -0
- package/dist/hsl-color.mjs +143 -0
- package/dist/index.d.mts +4 -121
- package/dist/index.d.ts +4 -121
- package/dist/index.js +37 -721
- package/dist/index.mjs +11 -716
- package/dist/native-color.d.mts +3 -0
- package/dist/native-color.d.ts +3 -0
- package/dist/native-color.js +41 -0
- package/dist/native-color.mjs +18 -0
- package/dist/parse-color.d.mts +6 -0
- package/dist/parse-color.d.ts +6 -0
- package/dist/parse-color.js +50 -0
- package/dist/parse-color.mjs +26 -0
- package/dist/rgb-color.d.mts +36 -0
- package/dist/rgb-color.d.ts +36 -0
- package/dist/rgb-color.js +213 -0
- package/dist/rgb-color.mjs +190 -0
- package/dist/types.d.mts +87 -0
- package/dist/types.d.ts +87 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/package.json +2 -2
|
@@ -0,0 +1,41 @@
|
|
|
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/native-color.ts
|
|
21
|
+
var native_color_exports = {};
|
|
22
|
+
__export(native_color_exports, {
|
|
23
|
+
nativeColorMap: () => nativeColorMap
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(native_color_exports);
|
|
26
|
+
var nativeColors = "aliceblue:f0f8ff,antiquewhite:faebd7,aqua:00ffff,aquamarine:7fffd4,azure:f0ffff,beige:f5f5dc,bisque:ffe4c4,black:000000,blanchedalmond:ffebcd,blue:0000ff,blueviolet:8a2be2,brown:a52a2a,burlywood:deb887,cadetblue:5f9ea0,chartreuse:7fff00,chocolate:d2691e,coral:ff7f50,cornflowerblue:6495ed,cornsilk:fff8dc,crimson:dc143c,cyan:00ffff,darkblue:00008b,darkcyan:008b8b,darkgoldenrod:b8860b,darkgray:a9a9a9,darkgreen:006400,darkkhaki:bdb76b,darkmagenta:8b008b,darkolivegreen:556b2f,darkorange:ff8c00,darkorchid:9932cc,darkred:8b0000,darksalmon:e9967a,darkseagreen:8fbc8f,darkslateblue:483d8b,darkslategray:2f4f4f,darkturquoise:00ced1,darkviolet:9400d3,deeppink:ff1493,deepskyblue:00bfff,dimgray:696969,dodgerblue:1e90ff,firebrick:b22222,floralwhite:fffaf0,forestgreen:228b22,fuchsia:ff00ff,gainsboro:dcdcdc,ghostwhite:f8f8ff,gold:ffd700,goldenrod:daa520,gray:808080,green:008000,greenyellow:adff2f,honeydew:f0fff0,hotpink:ff69b4,indianred:cd5c5c,indigo:4b0082,ivory:fffff0,khaki:f0e68c,lavender:e6e6fa,lavenderblush:fff0f5,lawngreen:7cfc00,lemonchiffon:fffacd,lightblue:add8e6,lightcoral:f08080,lightcyan:e0ffff,lightgoldenrodyellow:fafad2,lightgrey:d3d3d3,lightgreen:90ee90,lightpink:ffb6c1,lightsalmon:ffa07a,lightseagreen:20b2aa,lightskyblue:87cefa,lightslategray:778899,lightsteelblue:b0c4de,lightyellow:ffffe0,lime:00ff00,limegreen:32cd32,linen:faf0e6,magenta:ff00ff,maroon:800000,mediumaquamarine:66cdaa,mediumblue:0000cd,mediumorchid:ba55d3,mediumpurple:9370d8,mediumseagreen:3cb371,mediumslateblue:7b68ee,mediumspringgreen:00fa9a,mediumturquoise:48d1cc,mediumvioletred:c71585,midnightblue:191970,mintcream:f5fffa,mistyrose:ffe4e1,moccasin:ffe4b5,navajowhite:ffdead,navy:000080,oldlace:fdf5e6,olive:808000,olivedrab:6b8e23,orange:ffa500,orangered:ff4500,orchid:da70d6,palegoldenrod:eee8aa,palegreen:98fb98,paleturquoise:afeeee,palevioletred:d87093,papayawhip:ffefd5,peachpuff:ffdab9,peru:cd853f,pink:ffc0cb,plum:dda0dd,powderblue:b0e0e6,purple:800080,rebeccapurple:663399,red:ff0000,rosybrown:bc8f8f,royalblue:4169e1,saddlebrown:8b4513,salmon:fa8072,sandybrown:f4a460,seagreen:2e8b57,seashell:fff5ee,sienna:a0522d,silver:c0c0c0,skyblue:87ceeb,slateblue:6a5acd,slategray:708090,snow:fffafa,springgreen:00ff7f,steelblue:4682b4,tan:d2b48c,teal:008080,thistle:d8bfd8,tomato:ff6347,turquoise:40e0d0,violet:ee82ee,wheat:f5deb3,white:ffffff,whitesmoke:f5f5f5,yellow:ffff00,yellowgreen:9acd32";
|
|
27
|
+
var makeMap = (str) => {
|
|
28
|
+
const map = /* @__PURE__ */ new Map();
|
|
29
|
+
const list = str.split(",");
|
|
30
|
+
for (let i = 0; i < list.length; i++) {
|
|
31
|
+
const [key, val] = list[i].split(":");
|
|
32
|
+
map.set(key, `#${val}`);
|
|
33
|
+
if (key.includes("gray")) map.set(key.replace("gray", "grey"), `#${val}`);
|
|
34
|
+
}
|
|
35
|
+
return map;
|
|
36
|
+
};
|
|
37
|
+
var nativeColorMap = makeMap(nativeColors);
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
nativeColorMap
|
|
41
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "./chunk-QZ7TP4HQ.mjs";
|
|
2
|
+
|
|
3
|
+
// src/native-color.ts
|
|
4
|
+
var nativeColors = "aliceblue:f0f8ff,antiquewhite:faebd7,aqua:00ffff,aquamarine:7fffd4,azure:f0ffff,beige:f5f5dc,bisque:ffe4c4,black:000000,blanchedalmond:ffebcd,blue:0000ff,blueviolet:8a2be2,brown:a52a2a,burlywood:deb887,cadetblue:5f9ea0,chartreuse:7fff00,chocolate:d2691e,coral:ff7f50,cornflowerblue:6495ed,cornsilk:fff8dc,crimson:dc143c,cyan:00ffff,darkblue:00008b,darkcyan:008b8b,darkgoldenrod:b8860b,darkgray:a9a9a9,darkgreen:006400,darkkhaki:bdb76b,darkmagenta:8b008b,darkolivegreen:556b2f,darkorange:ff8c00,darkorchid:9932cc,darkred:8b0000,darksalmon:e9967a,darkseagreen:8fbc8f,darkslateblue:483d8b,darkslategray:2f4f4f,darkturquoise:00ced1,darkviolet:9400d3,deeppink:ff1493,deepskyblue:00bfff,dimgray:696969,dodgerblue:1e90ff,firebrick:b22222,floralwhite:fffaf0,forestgreen:228b22,fuchsia:ff00ff,gainsboro:dcdcdc,ghostwhite:f8f8ff,gold:ffd700,goldenrod:daa520,gray:808080,green:008000,greenyellow:adff2f,honeydew:f0fff0,hotpink:ff69b4,indianred:cd5c5c,indigo:4b0082,ivory:fffff0,khaki:f0e68c,lavender:e6e6fa,lavenderblush:fff0f5,lawngreen:7cfc00,lemonchiffon:fffacd,lightblue:add8e6,lightcoral:f08080,lightcyan:e0ffff,lightgoldenrodyellow:fafad2,lightgrey:d3d3d3,lightgreen:90ee90,lightpink:ffb6c1,lightsalmon:ffa07a,lightseagreen:20b2aa,lightskyblue:87cefa,lightslategray:778899,lightsteelblue:b0c4de,lightyellow:ffffe0,lime:00ff00,limegreen:32cd32,linen:faf0e6,magenta:ff00ff,maroon:800000,mediumaquamarine:66cdaa,mediumblue:0000cd,mediumorchid:ba55d3,mediumpurple:9370d8,mediumseagreen:3cb371,mediumslateblue:7b68ee,mediumspringgreen:00fa9a,mediumturquoise:48d1cc,mediumvioletred:c71585,midnightblue:191970,mintcream:f5fffa,mistyrose:ffe4e1,moccasin:ffe4b5,navajowhite:ffdead,navy:000080,oldlace:fdf5e6,olive:808000,olivedrab:6b8e23,orange:ffa500,orangered:ff4500,orchid:da70d6,palegoldenrod:eee8aa,palegreen:98fb98,paleturquoise:afeeee,palevioletred:d87093,papayawhip:ffefd5,peachpuff:ffdab9,peru:cd853f,pink:ffc0cb,plum:dda0dd,powderblue:b0e0e6,purple:800080,rebeccapurple:663399,red:ff0000,rosybrown:bc8f8f,royalblue:4169e1,saddlebrown:8b4513,salmon:fa8072,sandybrown:f4a460,seagreen:2e8b57,seashell:fff5ee,sienna:a0522d,silver:c0c0c0,skyblue:87ceeb,slateblue:6a5acd,slategray:708090,snow:fffafa,springgreen:00ff7f,steelblue:4682b4,tan:d2b48c,teal:008080,thistle:d8bfd8,tomato:ff6347,turquoise:40e0d0,violet:ee82ee,wheat:f5deb3,white:ffffff,whitesmoke:f5f5f5,yellow:ffff00,yellowgreen:9acd32";
|
|
5
|
+
var makeMap = (str) => {
|
|
6
|
+
const map = /* @__PURE__ */ new Map();
|
|
7
|
+
const list = str.split(",");
|
|
8
|
+
for (let i = 0; i < list.length; i++) {
|
|
9
|
+
const [key, val] = list[i].split(":");
|
|
10
|
+
map.set(key, `#${val}`);
|
|
11
|
+
if (key.includes("gray")) map.set(key.replace("gray", "grey"), `#${val}`);
|
|
12
|
+
}
|
|
13
|
+
return map;
|
|
14
|
+
};
|
|
15
|
+
var nativeColorMap = makeMap(nativeColors);
|
|
16
|
+
export {
|
|
17
|
+
nativeColorMap
|
|
18
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
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/parse-color.ts
|
|
21
|
+
var parse_color_exports = {};
|
|
22
|
+
__export(parse_color_exports, {
|
|
23
|
+
normalizeColor: () => normalizeColor,
|
|
24
|
+
parseColor: () => parseColor
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(parse_color_exports);
|
|
27
|
+
var import_hsb_color = require("./hsb-color.cjs");
|
|
28
|
+
var import_hsl_color = require("./hsl-color.cjs");
|
|
29
|
+
var import_native_color = require("./native-color.cjs");
|
|
30
|
+
var import_rgb_color = require("./rgb-color.cjs");
|
|
31
|
+
var parseColor = (value) => {
|
|
32
|
+
if (import_native_color.nativeColorMap.has(value)) {
|
|
33
|
+
return parseColor(import_native_color.nativeColorMap.get(value));
|
|
34
|
+
}
|
|
35
|
+
const result = import_rgb_color.RGBColor.parse(value) || import_hsb_color.HSBColor.parse(value) || import_hsl_color.HSLColor.parse(value);
|
|
36
|
+
if (!result) {
|
|
37
|
+
const error = new Error("Invalid color value: " + value);
|
|
38
|
+
Error.captureStackTrace?.(error, parseColor);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
var normalizeColor = (v) => {
|
|
44
|
+
return typeof v === "string" ? parseColor(v) : v;
|
|
45
|
+
};
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
normalizeColor,
|
|
49
|
+
parseColor
|
|
50
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import "./chunk-QZ7TP4HQ.mjs";
|
|
2
|
+
|
|
3
|
+
// src/parse-color.ts
|
|
4
|
+
import { HSBColor } from "./hsb-color.mjs";
|
|
5
|
+
import { HSLColor } from "./hsl-color.mjs";
|
|
6
|
+
import { nativeColorMap } from "./native-color.mjs";
|
|
7
|
+
import { RGBColor } from "./rgb-color.mjs";
|
|
8
|
+
var parseColor = (value) => {
|
|
9
|
+
if (nativeColorMap.has(value)) {
|
|
10
|
+
return parseColor(nativeColorMap.get(value));
|
|
11
|
+
}
|
|
12
|
+
const result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
13
|
+
if (!result) {
|
|
14
|
+
const error = new Error("Invalid color value: " + value);
|
|
15
|
+
Error.captureStackTrace?.(error, parseColor);
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
};
|
|
20
|
+
var normalizeColor = (v) => {
|
|
21
|
+
return typeof v === "string" ? parseColor(v) : v;
|
|
22
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
normalizeColor,
|
|
25
|
+
parseColor
|
|
26
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Color } from './color.mjs';
|
|
2
|
+
import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
declare class RGBColor extends Color {
|
|
5
|
+
private red;
|
|
6
|
+
private green;
|
|
7
|
+
private blue;
|
|
8
|
+
private alpha;
|
|
9
|
+
constructor(red: number, green: number, blue: number, alpha: number);
|
|
10
|
+
static parse(value: string): RGBColor | undefined;
|
|
11
|
+
toString(format?: ColorStringFormat): string;
|
|
12
|
+
toFormat(format: ColorFormat): ColorType;
|
|
13
|
+
toHexInt(): number;
|
|
14
|
+
/**
|
|
15
|
+
* Converts an RGB color value to HSB.
|
|
16
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
17
|
+
* @returns An HSBColor object.
|
|
18
|
+
*/
|
|
19
|
+
private toHSB;
|
|
20
|
+
/**
|
|
21
|
+
* Converts an RGB color value to HSL.
|
|
22
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
23
|
+
* @returns An HSLColor object.
|
|
24
|
+
*/
|
|
25
|
+
private toHSL;
|
|
26
|
+
clone(): ColorType;
|
|
27
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
28
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
29
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
30
|
+
toJSON(): Record<"r" | "g" | "b" | "a", number>;
|
|
31
|
+
getFormat(): ColorFormat;
|
|
32
|
+
private static colorChannels;
|
|
33
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { RGBColor };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Color } from './color.js';
|
|
2
|
+
import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare class RGBColor extends Color {
|
|
5
|
+
private red;
|
|
6
|
+
private green;
|
|
7
|
+
private blue;
|
|
8
|
+
private alpha;
|
|
9
|
+
constructor(red: number, green: number, blue: number, alpha: number);
|
|
10
|
+
static parse(value: string): RGBColor | undefined;
|
|
11
|
+
toString(format?: ColorStringFormat): string;
|
|
12
|
+
toFormat(format: ColorFormat): ColorType;
|
|
13
|
+
toHexInt(): number;
|
|
14
|
+
/**
|
|
15
|
+
* Converts an RGB color value to HSB.
|
|
16
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
17
|
+
* @returns An HSBColor object.
|
|
18
|
+
*/
|
|
19
|
+
private toHSB;
|
|
20
|
+
/**
|
|
21
|
+
* Converts an RGB color value to HSL.
|
|
22
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
23
|
+
* @returns An HSLColor object.
|
|
24
|
+
*/
|
|
25
|
+
private toHSL;
|
|
26
|
+
clone(): ColorType;
|
|
27
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
28
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
29
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
30
|
+
toJSON(): Record<"r" | "g" | "b" | "a", number>;
|
|
31
|
+
getFormat(): ColorFormat;
|
|
32
|
+
private static colorChannels;
|
|
33
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { RGBColor };
|
|
@@ -0,0 +1,213 @@
|
|
|
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 = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
|
|
22
|
+
// src/rgb-color.ts
|
|
23
|
+
var rgb_color_exports = {};
|
|
24
|
+
__export(rgb_color_exports, {
|
|
25
|
+
RGBColor: () => RGBColor
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(rgb_color_exports);
|
|
28
|
+
var import_utils = require("@zag-js/utils");
|
|
29
|
+
var import_color = require("./color.cjs");
|
|
30
|
+
var import_hsb_color = require("./hsb-color.cjs");
|
|
31
|
+
var import_hsl_color = require("./hsl-color.cjs");
|
|
32
|
+
var HEX_COLOR_REGEX = /^#[\da-f]+$/i;
|
|
33
|
+
var RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
|
|
34
|
+
var HEX_STARTING_REGEX = /[^#]/gi;
|
|
35
|
+
var _RGBColor = class _RGBColor extends import_color.Color {
|
|
36
|
+
constructor(red, green, blue, alpha) {
|
|
37
|
+
super();
|
|
38
|
+
this.red = red;
|
|
39
|
+
this.green = green;
|
|
40
|
+
this.blue = blue;
|
|
41
|
+
this.alpha = alpha;
|
|
42
|
+
}
|
|
43
|
+
static parse(value) {
|
|
44
|
+
let colors = [];
|
|
45
|
+
if (HEX_COLOR_REGEX.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
46
|
+
const values = (value.length < 6 ? value.replace(HEX_STARTING_REGEX, "$&$&") : value).slice(1).split("");
|
|
47
|
+
while (values.length > 0) {
|
|
48
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
49
|
+
}
|
|
50
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
51
|
+
}
|
|
52
|
+
const match = value.match(RGB_COLOR_REGEX);
|
|
53
|
+
if (match?.[1]) {
|
|
54
|
+
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => (0, import_utils.clampValue)(num, 0, i < 3 ? 255 : 1));
|
|
55
|
+
}
|
|
56
|
+
return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
57
|
+
}
|
|
58
|
+
toString(format = "css") {
|
|
59
|
+
switch (format) {
|
|
60
|
+
case "hex":
|
|
61
|
+
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
62
|
+
case "hexa":
|
|
63
|
+
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();
|
|
64
|
+
case "rgb":
|
|
65
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
66
|
+
case "css":
|
|
67
|
+
case "rgba":
|
|
68
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
69
|
+
case "hsl":
|
|
70
|
+
return this.toHSL().toString("hsl");
|
|
71
|
+
case "hsb":
|
|
72
|
+
return this.toHSB().toString("hsb");
|
|
73
|
+
default:
|
|
74
|
+
return this.toFormat(format).toString(format);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
toFormat(format) {
|
|
78
|
+
switch (format) {
|
|
79
|
+
case "rgba":
|
|
80
|
+
return this;
|
|
81
|
+
case "hsba":
|
|
82
|
+
return this.toHSB();
|
|
83
|
+
case "hsla":
|
|
84
|
+
return this.toHSL();
|
|
85
|
+
default:
|
|
86
|
+
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
toHexInt() {
|
|
90
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Converts an RGB color value to HSB.
|
|
94
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
95
|
+
* @returns An HSBColor object.
|
|
96
|
+
*/
|
|
97
|
+
toHSB() {
|
|
98
|
+
const red = this.red / 255;
|
|
99
|
+
const green = this.green / 255;
|
|
100
|
+
const blue = this.blue / 255;
|
|
101
|
+
const min = Math.min(red, green, blue);
|
|
102
|
+
const brightness = Math.max(red, green, blue);
|
|
103
|
+
const chroma = brightness - min;
|
|
104
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
105
|
+
let hue = 0;
|
|
106
|
+
if (chroma !== 0) {
|
|
107
|
+
switch (brightness) {
|
|
108
|
+
case red:
|
|
109
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
110
|
+
break;
|
|
111
|
+
case green:
|
|
112
|
+
hue = (blue - red) / chroma + 2;
|
|
113
|
+
break;
|
|
114
|
+
case blue:
|
|
115
|
+
hue = (red - green) / chroma + 4;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
hue /= 6;
|
|
119
|
+
}
|
|
120
|
+
return new import_hsb_color.HSBColor(
|
|
121
|
+
(0, import_utils.toFixedNumber)(hue * 360, 2),
|
|
122
|
+
(0, import_utils.toFixedNumber)(saturation * 100, 2),
|
|
123
|
+
(0, import_utils.toFixedNumber)(brightness * 100, 2),
|
|
124
|
+
(0, import_utils.toFixedNumber)(this.alpha, 2)
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Converts an RGB color value to HSL.
|
|
129
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
130
|
+
* @returns An HSLColor object.
|
|
131
|
+
*/
|
|
132
|
+
toHSL() {
|
|
133
|
+
const red = this.red / 255;
|
|
134
|
+
const green = this.green / 255;
|
|
135
|
+
const blue = this.blue / 255;
|
|
136
|
+
const min = Math.min(red, green, blue);
|
|
137
|
+
const max = Math.max(red, green, blue);
|
|
138
|
+
const lightness = (max + min) / 2;
|
|
139
|
+
const chroma = max - min;
|
|
140
|
+
let hue = -1;
|
|
141
|
+
let saturation = -1;
|
|
142
|
+
if (chroma === 0) {
|
|
143
|
+
hue = saturation = 0;
|
|
144
|
+
} else {
|
|
145
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
146
|
+
switch (max) {
|
|
147
|
+
case red:
|
|
148
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
149
|
+
break;
|
|
150
|
+
case green:
|
|
151
|
+
hue = (blue - red) / chroma + 2;
|
|
152
|
+
break;
|
|
153
|
+
case blue:
|
|
154
|
+
hue = (red - green) / chroma + 4;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
hue /= 6;
|
|
158
|
+
}
|
|
159
|
+
return new import_hsl_color.HSLColor(
|
|
160
|
+
(0, import_utils.toFixedNumber)(hue * 360, 2),
|
|
161
|
+
(0, import_utils.toFixedNumber)(saturation * 100, 2),
|
|
162
|
+
(0, import_utils.toFixedNumber)(lightness * 100, 2),
|
|
163
|
+
(0, import_utils.toFixedNumber)(this.alpha, 2)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
clone() {
|
|
167
|
+
return new _RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
168
|
+
}
|
|
169
|
+
getChannelFormatOptions(channel) {
|
|
170
|
+
switch (channel) {
|
|
171
|
+
case "red":
|
|
172
|
+
case "green":
|
|
173
|
+
case "blue":
|
|
174
|
+
return { style: "decimal" };
|
|
175
|
+
case "alpha":
|
|
176
|
+
return { style: "percent" };
|
|
177
|
+
default:
|
|
178
|
+
throw new Error("Unknown color channel: " + channel);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
formatChannelValue(channel, locale) {
|
|
182
|
+
let options = this.getChannelFormatOptions(channel);
|
|
183
|
+
let value = this.getChannelValue(channel);
|
|
184
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
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
|
+
toJSON() {
|
|
199
|
+
return { r: this.red, g: this.green, b: this.blue, a: this.alpha };
|
|
200
|
+
}
|
|
201
|
+
getFormat() {
|
|
202
|
+
return "rgba";
|
|
203
|
+
}
|
|
204
|
+
getChannels() {
|
|
205
|
+
return _RGBColor.colorChannels;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
__publicField(_RGBColor, "colorChannels", ["red", "green", "blue"]);
|
|
209
|
+
var RGBColor = _RGBColor;
|
|
210
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
211
|
+
0 && (module.exports = {
|
|
212
|
+
RGBColor
|
|
213
|
+
});
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "./chunk-QZ7TP4HQ.mjs";
|
|
4
|
+
|
|
5
|
+
// src/rgb-color.ts
|
|
6
|
+
import { clampValue, toFixedNumber } from "@zag-js/utils";
|
|
7
|
+
import { Color } from "./color.mjs";
|
|
8
|
+
import { HSBColor } from "./hsb-color.mjs";
|
|
9
|
+
import { HSLColor } from "./hsl-color.mjs";
|
|
10
|
+
var HEX_COLOR_REGEX = /^#[\da-f]+$/i;
|
|
11
|
+
var RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
|
|
12
|
+
var HEX_STARTING_REGEX = /[^#]/gi;
|
|
13
|
+
var _RGBColor = class _RGBColor extends Color {
|
|
14
|
+
constructor(red, green, blue, alpha) {
|
|
15
|
+
super();
|
|
16
|
+
this.red = red;
|
|
17
|
+
this.green = green;
|
|
18
|
+
this.blue = blue;
|
|
19
|
+
this.alpha = alpha;
|
|
20
|
+
}
|
|
21
|
+
static parse(value) {
|
|
22
|
+
let colors = [];
|
|
23
|
+
if (HEX_COLOR_REGEX.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
24
|
+
const values = (value.length < 6 ? value.replace(HEX_STARTING_REGEX, "$&$&") : value).slice(1).split("");
|
|
25
|
+
while (values.length > 0) {
|
|
26
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16));
|
|
27
|
+
}
|
|
28
|
+
colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
|
|
29
|
+
}
|
|
30
|
+
const match = value.match(RGB_COLOR_REGEX);
|
|
31
|
+
if (match?.[1]) {
|
|
32
|
+
colors = match[1].split(",").map((value2) => Number(value2.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
|
|
33
|
+
}
|
|
34
|
+
return colors.length < 3 ? void 0 : new _RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
|
|
35
|
+
}
|
|
36
|
+
toString(format = "css") {
|
|
37
|
+
switch (format) {
|
|
38
|
+
case "hex":
|
|
39
|
+
return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
|
|
40
|
+
case "hexa":
|
|
41
|
+
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();
|
|
42
|
+
case "rgb":
|
|
43
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
44
|
+
case "css":
|
|
45
|
+
case "rgba":
|
|
46
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
47
|
+
case "hsl":
|
|
48
|
+
return this.toHSL().toString("hsl");
|
|
49
|
+
case "hsb":
|
|
50
|
+
return this.toHSB().toString("hsb");
|
|
51
|
+
default:
|
|
52
|
+
return this.toFormat(format).toString(format);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
toFormat(format) {
|
|
56
|
+
switch (format) {
|
|
57
|
+
case "rgba":
|
|
58
|
+
return this;
|
|
59
|
+
case "hsba":
|
|
60
|
+
return this.toHSB();
|
|
61
|
+
case "hsla":
|
|
62
|
+
return this.toHSL();
|
|
63
|
+
default:
|
|
64
|
+
throw new Error("Unsupported color conversion: rgb -> " + format);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
toHexInt() {
|
|
68
|
+
return this.red << 16 | this.green << 8 | this.blue;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts an RGB color value to HSB.
|
|
72
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
73
|
+
* @returns An HSBColor object.
|
|
74
|
+
*/
|
|
75
|
+
toHSB() {
|
|
76
|
+
const red = this.red / 255;
|
|
77
|
+
const green = this.green / 255;
|
|
78
|
+
const blue = this.blue / 255;
|
|
79
|
+
const min = Math.min(red, green, blue);
|
|
80
|
+
const brightness = Math.max(red, green, blue);
|
|
81
|
+
const chroma = brightness - min;
|
|
82
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
83
|
+
let hue = 0;
|
|
84
|
+
if (chroma !== 0) {
|
|
85
|
+
switch (brightness) {
|
|
86
|
+
case red:
|
|
87
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
88
|
+
break;
|
|
89
|
+
case green:
|
|
90
|
+
hue = (blue - red) / chroma + 2;
|
|
91
|
+
break;
|
|
92
|
+
case blue:
|
|
93
|
+
hue = (red - green) / chroma + 4;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
hue /= 6;
|
|
97
|
+
}
|
|
98
|
+
return new HSBColor(
|
|
99
|
+
toFixedNumber(hue * 360, 2),
|
|
100
|
+
toFixedNumber(saturation * 100, 2),
|
|
101
|
+
toFixedNumber(brightness * 100, 2),
|
|
102
|
+
toFixedNumber(this.alpha, 2)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Converts an RGB color value to HSL.
|
|
107
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
108
|
+
* @returns An HSLColor object.
|
|
109
|
+
*/
|
|
110
|
+
toHSL() {
|
|
111
|
+
const red = this.red / 255;
|
|
112
|
+
const green = this.green / 255;
|
|
113
|
+
const blue = this.blue / 255;
|
|
114
|
+
const min = Math.min(red, green, blue);
|
|
115
|
+
const max = Math.max(red, green, blue);
|
|
116
|
+
const lightness = (max + min) / 2;
|
|
117
|
+
const chroma = max - min;
|
|
118
|
+
let hue = -1;
|
|
119
|
+
let saturation = -1;
|
|
120
|
+
if (chroma === 0) {
|
|
121
|
+
hue = saturation = 0;
|
|
122
|
+
} else {
|
|
123
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
124
|
+
switch (max) {
|
|
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 HSLColor(
|
|
138
|
+
toFixedNumber(hue * 360, 2),
|
|
139
|
+
toFixedNumber(saturation * 100, 2),
|
|
140
|
+
toFixedNumber(lightness * 100, 2),
|
|
141
|
+
toFixedNumber(this.alpha, 2)
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
clone() {
|
|
145
|
+
return new _RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
146
|
+
}
|
|
147
|
+
getChannelFormatOptions(channel) {
|
|
148
|
+
switch (channel) {
|
|
149
|
+
case "red":
|
|
150
|
+
case "green":
|
|
151
|
+
case "blue":
|
|
152
|
+
return { style: "decimal" };
|
|
153
|
+
case "alpha":
|
|
154
|
+
return { style: "percent" };
|
|
155
|
+
default:
|
|
156
|
+
throw new Error("Unknown color channel: " + channel);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
formatChannelValue(channel, locale) {
|
|
160
|
+
let options = this.getChannelFormatOptions(channel);
|
|
161
|
+
let value = this.getChannelValue(channel);
|
|
162
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
163
|
+
}
|
|
164
|
+
getChannelRange(channel) {
|
|
165
|
+
switch (channel) {
|
|
166
|
+
case "red":
|
|
167
|
+
case "green":
|
|
168
|
+
case "blue":
|
|
169
|
+
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
170
|
+
case "alpha":
|
|
171
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
172
|
+
default:
|
|
173
|
+
throw new Error("Unknown color channel: " + channel);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
toJSON() {
|
|
177
|
+
return { r: this.red, g: this.green, b: this.blue, a: this.alpha };
|
|
178
|
+
}
|
|
179
|
+
getFormat() {
|
|
180
|
+
return "rgba";
|
|
181
|
+
}
|
|
182
|
+
getChannels() {
|
|
183
|
+
return _RGBColor.colorChannels;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
__publicField(_RGBColor, "colorChannels", ["red", "green", "blue"]);
|
|
187
|
+
var RGBColor = _RGBColor;
|
|
188
|
+
export {
|
|
189
|
+
RGBColor
|
|
190
|
+
};
|