@zag-js/color-utils 1.34.1 → 1.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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
package/dist/color.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
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 import_utils = require("@zag-js/utils");
|
|
27
|
+
var isEqualObject = (a, b) => {
|
|
28
|
+
if (Object.keys(a).length !== Object.keys(b).length) return false;
|
|
29
|
+
for (let key in a) if (a[key] !== b[key]) return false;
|
|
30
|
+
return true;
|
|
31
|
+
};
|
|
32
|
+
var Color = class {
|
|
33
|
+
toHexInt() {
|
|
34
|
+
return this.toFormat("rgba").toHexInt();
|
|
35
|
+
}
|
|
36
|
+
getChannelValue(channel) {
|
|
37
|
+
if (channel in this) return this[channel];
|
|
38
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
39
|
+
}
|
|
40
|
+
getChannelValuePercent(channel, valueToCheck) {
|
|
41
|
+
const value = valueToCheck ?? this.getChannelValue(channel);
|
|
42
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
43
|
+
return (0, import_utils.getValuePercent)(value, minValue, maxValue);
|
|
44
|
+
}
|
|
45
|
+
getChannelPercentValue(channel, percentToCheck) {
|
|
46
|
+
const { minValue, maxValue, step } = this.getChannelRange(channel);
|
|
47
|
+
const percentValue = (0, import_utils.getPercentValue)(percentToCheck, minValue, maxValue, step);
|
|
48
|
+
return (0, import_utils.snapValueToStep)(percentValue, minValue, maxValue, step);
|
|
49
|
+
}
|
|
50
|
+
withChannelValue(channel, value) {
|
|
51
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
52
|
+
if (channel in this) {
|
|
53
|
+
let clone = this.clone();
|
|
54
|
+
clone[channel] = (0, import_utils.clampValue)(value, minValue, maxValue);
|
|
55
|
+
return clone;
|
|
56
|
+
}
|
|
57
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
58
|
+
}
|
|
59
|
+
getColorAxes(xyChannels) {
|
|
60
|
+
let { xChannel, yChannel } = xyChannels;
|
|
61
|
+
let xCh = xChannel || this.getChannels().find((c) => c !== yChannel);
|
|
62
|
+
let yCh = yChannel || this.getChannels().find((c) => c !== xCh);
|
|
63
|
+
let zCh = this.getChannels().find((c) => c !== xCh && c !== yCh);
|
|
64
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
65
|
+
}
|
|
66
|
+
incrementChannel(channel, stepSize) {
|
|
67
|
+
const { minValue, maxValue, step } = this.getChannelRange(channel);
|
|
68
|
+
const value = (0, import_utils.snapValueToStep)(
|
|
69
|
+
(0, import_utils.clampValue)(this.getChannelValue(channel) + stepSize, minValue, maxValue),
|
|
70
|
+
minValue,
|
|
71
|
+
maxValue,
|
|
72
|
+
step
|
|
73
|
+
);
|
|
74
|
+
return this.withChannelValue(channel, value);
|
|
75
|
+
}
|
|
76
|
+
decrementChannel(channel, stepSize) {
|
|
77
|
+
return this.incrementChannel(channel, -stepSize);
|
|
78
|
+
}
|
|
79
|
+
isEqual(color) {
|
|
80
|
+
const isSame = isEqualObject(this.toJSON(), color.toJSON());
|
|
81
|
+
return isSame && this.getChannelValue("alpha") === color.getChannelValue("alpha");
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
85
|
+
0 && (module.exports = {
|
|
86
|
+
Color
|
|
87
|
+
});
|
package/dist/color.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import "./chunk-QZ7TP4HQ.mjs";
|
|
2
|
+
|
|
3
|
+
// src/color.ts
|
|
4
|
+
import { clampValue, getPercentValue, getValuePercent, snapValueToStep } from "@zag-js/utils";
|
|
5
|
+
var isEqualObject = (a, b) => {
|
|
6
|
+
if (Object.keys(a).length !== Object.keys(b).length) return false;
|
|
7
|
+
for (let key in a) if (a[key] !== b[key]) return false;
|
|
8
|
+
return true;
|
|
9
|
+
};
|
|
10
|
+
var Color = class {
|
|
11
|
+
toHexInt() {
|
|
12
|
+
return this.toFormat("rgba").toHexInt();
|
|
13
|
+
}
|
|
14
|
+
getChannelValue(channel) {
|
|
15
|
+
if (channel in this) return this[channel];
|
|
16
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
17
|
+
}
|
|
18
|
+
getChannelValuePercent(channel, valueToCheck) {
|
|
19
|
+
const value = valueToCheck ?? this.getChannelValue(channel);
|
|
20
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
21
|
+
return getValuePercent(value, minValue, maxValue);
|
|
22
|
+
}
|
|
23
|
+
getChannelPercentValue(channel, percentToCheck) {
|
|
24
|
+
const { minValue, maxValue, step } = this.getChannelRange(channel);
|
|
25
|
+
const percentValue = getPercentValue(percentToCheck, minValue, maxValue, step);
|
|
26
|
+
return snapValueToStep(percentValue, minValue, maxValue, step);
|
|
27
|
+
}
|
|
28
|
+
withChannelValue(channel, value) {
|
|
29
|
+
const { minValue, maxValue } = this.getChannelRange(channel);
|
|
30
|
+
if (channel in this) {
|
|
31
|
+
let clone = this.clone();
|
|
32
|
+
clone[channel] = clampValue(value, minValue, maxValue);
|
|
33
|
+
return clone;
|
|
34
|
+
}
|
|
35
|
+
throw new Error("Unsupported color channel: " + channel);
|
|
36
|
+
}
|
|
37
|
+
getColorAxes(xyChannels) {
|
|
38
|
+
let { xChannel, yChannel } = xyChannels;
|
|
39
|
+
let xCh = xChannel || this.getChannels().find((c) => c !== yChannel);
|
|
40
|
+
let yCh = yChannel || this.getChannels().find((c) => c !== xCh);
|
|
41
|
+
let zCh = this.getChannels().find((c) => c !== xCh && c !== yCh);
|
|
42
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh };
|
|
43
|
+
}
|
|
44
|
+
incrementChannel(channel, stepSize) {
|
|
45
|
+
const { minValue, maxValue, step } = this.getChannelRange(channel);
|
|
46
|
+
const value = snapValueToStep(
|
|
47
|
+
clampValue(this.getChannelValue(channel) + stepSize, minValue, maxValue),
|
|
48
|
+
minValue,
|
|
49
|
+
maxValue,
|
|
50
|
+
step
|
|
51
|
+
);
|
|
52
|
+
return this.withChannelValue(channel, value);
|
|
53
|
+
}
|
|
54
|
+
decrementChannel(channel, stepSize) {
|
|
55
|
+
return this.incrementChannel(channel, -stepSize);
|
|
56
|
+
}
|
|
57
|
+
isEqual(color) {
|
|
58
|
+
const isSame = isEqualObject(this.toJSON(), color.toJSON());
|
|
59
|
+
return isSame && this.getChannelValue("alpha") === color.getChannelValue("alpha");
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
Color
|
|
64
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Color } from './color.mjs';
|
|
2
|
+
import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.mjs';
|
|
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?: ColorStringFormat): 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
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
27
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
28
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
29
|
+
toJSON(): Record<"h" | "s" | "b" | "a", number>;
|
|
30
|
+
getFormat(): ColorFormat;
|
|
31
|
+
private static colorChannels;
|
|
32
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { HSBColor };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Color } from './color.js';
|
|
2
|
+
import { ColorStringFormat, 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?: ColorStringFormat): 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
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
27
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
28
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
29
|
+
toJSON(): Record<"h" | "s" | "b" | "a", number>;
|
|
30
|
+
getFormat(): ColorFormat;
|
|
31
|
+
private static colorChannels;
|
|
32
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { HSBColor };
|
|
@@ -0,0 +1,165 @@
|
|
|
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) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
|
|
22
|
+
// src/hsb-color.ts
|
|
23
|
+
var hsb_color_exports = {};
|
|
24
|
+
__export(hsb_color_exports, {
|
|
25
|
+
HSBColor: () => HSBColor
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(hsb_color_exports);
|
|
28
|
+
var import_utils = require("@zag-js/utils");
|
|
29
|
+
var import_color = require("./color.cjs");
|
|
30
|
+
var import_hsl_color = require("./hsl-color.cjs");
|
|
31
|
+
var import_rgb_color = require("./rgb-color.cjs");
|
|
32
|
+
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+)?)\)/;
|
|
33
|
+
var _HSBColor = class _HSBColor extends import_color.Color {
|
|
34
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
35
|
+
super();
|
|
36
|
+
this.hue = hue;
|
|
37
|
+
this.saturation = saturation;
|
|
38
|
+
this.brightness = brightness;
|
|
39
|
+
this.alpha = alpha;
|
|
40
|
+
}
|
|
41
|
+
static parse(value) {
|
|
42
|
+
let m;
|
|
43
|
+
if (m = value.match(HSB_REGEX)) {
|
|
44
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
45
|
+
return new _HSBColor((0, import_utils.mod)(h, 360), (0, import_utils.clampValue)(s, 0, 100), (0, import_utils.clampValue)(b, 0, 100), (0, import_utils.clampValue)(a ?? 1, 0, 1));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
toString(format = "css") {
|
|
49
|
+
switch (format) {
|
|
50
|
+
case "css":
|
|
51
|
+
return this.toHSL().toString("css");
|
|
52
|
+
case "hex":
|
|
53
|
+
return this.toRGB().toString("hex");
|
|
54
|
+
case "hexa":
|
|
55
|
+
return this.toRGB().toString("hexa");
|
|
56
|
+
case "hsb":
|
|
57
|
+
return `hsb(${this.hue}, ${(0, import_utils.toFixedNumber)(this.saturation, 2)}%, ${(0, import_utils.toFixedNumber)(this.brightness, 2)}%)`;
|
|
58
|
+
case "hsba":
|
|
59
|
+
return `hsba(${this.hue}, ${(0, import_utils.toFixedNumber)(this.saturation, 2)}%, ${(0, import_utils.toFixedNumber)(this.brightness, 2)}%, ${this.alpha})`;
|
|
60
|
+
case "hsl":
|
|
61
|
+
return this.toHSL().toString("hsl");
|
|
62
|
+
case "rgb":
|
|
63
|
+
return this.toRGB().toString("rgb");
|
|
64
|
+
default:
|
|
65
|
+
return this.toFormat(format).toString(format);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
toFormat(format) {
|
|
69
|
+
switch (format) {
|
|
70
|
+
case "hsba":
|
|
71
|
+
return this;
|
|
72
|
+
case "hsla":
|
|
73
|
+
return this.toHSL();
|
|
74
|
+
case "rgba":
|
|
75
|
+
return this.toRGB();
|
|
76
|
+
default:
|
|
77
|
+
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Converts a HSB color to HSL.
|
|
82
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
83
|
+
* @returns An HSLColor object.
|
|
84
|
+
*/
|
|
85
|
+
toHSL() {
|
|
86
|
+
let saturation = this.saturation / 100;
|
|
87
|
+
let brightness = this.brightness / 100;
|
|
88
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
89
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
90
|
+
return new import_hsl_color.HSLColor(
|
|
91
|
+
(0, import_utils.toFixedNumber)(this.hue, 2),
|
|
92
|
+
(0, import_utils.toFixedNumber)(saturation * 100, 2),
|
|
93
|
+
(0, import_utils.toFixedNumber)(lightness * 100, 2),
|
|
94
|
+
(0, import_utils.toFixedNumber)(this.alpha, 2)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Converts a HSV color value to RGB.
|
|
99
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
100
|
+
* @returns An RGBColor object.
|
|
101
|
+
*/
|
|
102
|
+
toRGB() {
|
|
103
|
+
let hue = this.hue;
|
|
104
|
+
let saturation = this.saturation / 100;
|
|
105
|
+
let brightness = this.brightness / 100;
|
|
106
|
+
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
107
|
+
return new import_rgb_color.RGBColor(
|
|
108
|
+
Math.round(fn(5) * 255),
|
|
109
|
+
Math.round(fn(3) * 255),
|
|
110
|
+
Math.round(fn(1) * 255),
|
|
111
|
+
(0, import_utils.toFixedNumber)(this.alpha, 2)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
clone() {
|
|
115
|
+
return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
116
|
+
}
|
|
117
|
+
getChannelFormatOptions(channel) {
|
|
118
|
+
switch (channel) {
|
|
119
|
+
case "hue":
|
|
120
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow" };
|
|
121
|
+
case "saturation":
|
|
122
|
+
case "brightness":
|
|
123
|
+
case "alpha":
|
|
124
|
+
return { style: "percent" };
|
|
125
|
+
default:
|
|
126
|
+
throw new Error("Unknown color channel: " + channel);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
formatChannelValue(channel, locale) {
|
|
130
|
+
let options = this.getChannelFormatOptions(channel);
|
|
131
|
+
let value = this.getChannelValue(channel);
|
|
132
|
+
if (channel === "saturation" || channel === "brightness") {
|
|
133
|
+
value /= 100;
|
|
134
|
+
}
|
|
135
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
136
|
+
}
|
|
137
|
+
getChannelRange(channel) {
|
|
138
|
+
switch (channel) {
|
|
139
|
+
case "hue":
|
|
140
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
141
|
+
case "saturation":
|
|
142
|
+
case "brightness":
|
|
143
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
144
|
+
case "alpha":
|
|
145
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
146
|
+
default:
|
|
147
|
+
throw new Error("Unknown color channel: " + channel);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
toJSON() {
|
|
151
|
+
return { h: this.hue, s: this.saturation, b: this.brightness, a: this.alpha };
|
|
152
|
+
}
|
|
153
|
+
getFormat() {
|
|
154
|
+
return "hsba";
|
|
155
|
+
}
|
|
156
|
+
getChannels() {
|
|
157
|
+
return _HSBColor.colorChannels;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
__publicField(_HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
|
|
161
|
+
var HSBColor = _HSBColor;
|
|
162
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
163
|
+
0 && (module.exports = {
|
|
164
|
+
HSBColor
|
|
165
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__publicField
|
|
3
|
+
} from "./chunk-QZ7TP4HQ.mjs";
|
|
4
|
+
|
|
5
|
+
// src/hsb-color.ts
|
|
6
|
+
import { clampValue, mod, toFixedNumber } from "@zag-js/utils";
|
|
7
|
+
import { Color } from "./color.mjs";
|
|
8
|
+
import { HSLColor } from "./hsl-color.mjs";
|
|
9
|
+
import { RGBColor } from "./rgb-color.mjs";
|
|
10
|
+
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+)?)\)/;
|
|
11
|
+
var _HSBColor = class _HSBColor extends Color {
|
|
12
|
+
constructor(hue, saturation, brightness, alpha) {
|
|
13
|
+
super();
|
|
14
|
+
this.hue = hue;
|
|
15
|
+
this.saturation = saturation;
|
|
16
|
+
this.brightness = brightness;
|
|
17
|
+
this.alpha = alpha;
|
|
18
|
+
}
|
|
19
|
+
static parse(value) {
|
|
20
|
+
let m;
|
|
21
|
+
if (m = value.match(HSB_REGEX)) {
|
|
22
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
23
|
+
return new _HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
toString(format = "css") {
|
|
27
|
+
switch (format) {
|
|
28
|
+
case "css":
|
|
29
|
+
return this.toHSL().toString("css");
|
|
30
|
+
case "hex":
|
|
31
|
+
return this.toRGB().toString("hex");
|
|
32
|
+
case "hexa":
|
|
33
|
+
return this.toRGB().toString("hexa");
|
|
34
|
+
case "hsb":
|
|
35
|
+
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
|
|
36
|
+
case "hsba":
|
|
37
|
+
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
38
|
+
case "hsl":
|
|
39
|
+
return this.toHSL().toString("hsl");
|
|
40
|
+
case "rgb":
|
|
41
|
+
return this.toRGB().toString("rgb");
|
|
42
|
+
default:
|
|
43
|
+
return this.toFormat(format).toString(format);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
toFormat(format) {
|
|
47
|
+
switch (format) {
|
|
48
|
+
case "hsba":
|
|
49
|
+
return this;
|
|
50
|
+
case "hsla":
|
|
51
|
+
return this.toHSL();
|
|
52
|
+
case "rgba":
|
|
53
|
+
return this.toRGB();
|
|
54
|
+
default:
|
|
55
|
+
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Converts a HSB color to HSL.
|
|
60
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
61
|
+
* @returns An HSLColor object.
|
|
62
|
+
*/
|
|
63
|
+
toHSL() {
|
|
64
|
+
let saturation = this.saturation / 100;
|
|
65
|
+
let brightness = this.brightness / 100;
|
|
66
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
67
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
68
|
+
return new HSLColor(
|
|
69
|
+
toFixedNumber(this.hue, 2),
|
|
70
|
+
toFixedNumber(saturation * 100, 2),
|
|
71
|
+
toFixedNumber(lightness * 100, 2),
|
|
72
|
+
toFixedNumber(this.alpha, 2)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Converts a HSV color value to RGB.
|
|
77
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
78
|
+
* @returns An RGBColor object.
|
|
79
|
+
*/
|
|
80
|
+
toRGB() {
|
|
81
|
+
let hue = this.hue;
|
|
82
|
+
let saturation = this.saturation / 100;
|
|
83
|
+
let brightness = this.brightness / 100;
|
|
84
|
+
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
85
|
+
return new RGBColor(
|
|
86
|
+
Math.round(fn(5) * 255),
|
|
87
|
+
Math.round(fn(3) * 255),
|
|
88
|
+
Math.round(fn(1) * 255),
|
|
89
|
+
toFixedNumber(this.alpha, 2)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
clone() {
|
|
93
|
+
return new _HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
94
|
+
}
|
|
95
|
+
getChannelFormatOptions(channel) {
|
|
96
|
+
switch (channel) {
|
|
97
|
+
case "hue":
|
|
98
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow" };
|
|
99
|
+
case "saturation":
|
|
100
|
+
case "brightness":
|
|
101
|
+
case "alpha":
|
|
102
|
+
return { style: "percent" };
|
|
103
|
+
default:
|
|
104
|
+
throw new Error("Unknown color channel: " + channel);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
formatChannelValue(channel, locale) {
|
|
108
|
+
let options = this.getChannelFormatOptions(channel);
|
|
109
|
+
let value = this.getChannelValue(channel);
|
|
110
|
+
if (channel === "saturation" || channel === "brightness") {
|
|
111
|
+
value /= 100;
|
|
112
|
+
}
|
|
113
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
114
|
+
}
|
|
115
|
+
getChannelRange(channel) {
|
|
116
|
+
switch (channel) {
|
|
117
|
+
case "hue":
|
|
118
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
119
|
+
case "saturation":
|
|
120
|
+
case "brightness":
|
|
121
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
122
|
+
case "alpha":
|
|
123
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
124
|
+
default:
|
|
125
|
+
throw new Error("Unknown color channel: " + channel);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
toJSON() {
|
|
129
|
+
return { h: this.hue, s: this.saturation, b: this.brightness, a: this.alpha };
|
|
130
|
+
}
|
|
131
|
+
getFormat() {
|
|
132
|
+
return "hsba";
|
|
133
|
+
}
|
|
134
|
+
getChannels() {
|
|
135
|
+
return _HSBColor.colorChannels;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
__publicField(_HSBColor, "colorChannels", ["hue", "saturation", "brightness"]);
|
|
139
|
+
var HSBColor = _HSBColor;
|
|
140
|
+
export {
|
|
141
|
+
HSBColor
|
|
142
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Color } from './color.mjs';
|
|
2
|
+
import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.mjs';
|
|
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?: ColorStringFormat): 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
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
28
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
29
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
30
|
+
toJSON(): Record<"h" | "s" | "l" | "a", number>;
|
|
31
|
+
getFormat(): ColorFormat;
|
|
32
|
+
private static colorChannels;
|
|
33
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { HSLColor, HSL_REGEX };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Color } from './color.js';
|
|
2
|
+
import { ColorStringFormat, 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?: ColorStringFormat): 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
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
28
|
+
formatChannelValue(channel: ColorChannel, locale: string): string;
|
|
29
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
30
|
+
toJSON(): Record<"h" | "s" | "l" | "a", number>;
|
|
31
|
+
getFormat(): ColorFormat;
|
|
32
|
+
private static colorChannels;
|
|
33
|
+
getChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { HSLColor, HSL_REGEX };
|