@zag-js/color-utils 2.0.0-next.2 → 2.0.0-next.4
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/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.js +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPrimitive.js +13 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPrimitive.mjs +13 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPropertyKey.js +8 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/toPropertyKey.mjs +8 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/typeof.js +15 -0
- package/dist/_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/typeof.mjs +10 -0
- package/dist/area-gradient.d.mts +9 -11
- package/dist/area-gradient.d.ts +9 -11
- package/dist/area-gradient.js +58 -89
- package/dist/area-gradient.mjs +54 -77
- package/dist/color-format-gradient.d.mts +76 -78
- package/dist/color-format-gradient.d.ts +76 -78
- package/dist/color-format-gradient.js +118 -185
- package/dist/color-format-gradient.mjs +104 -152
- package/dist/color.d.mts +20 -23
- package/dist/color.d.ts +20 -23
- package/dist/color.js +57 -83
- package/dist/color.mjs +53 -60
- package/dist/gamut.d.mts +2 -5
- package/dist/gamut.d.ts +2 -5
- package/dist/gamut.js +12 -34
- package/dist/gamut.mjs +8 -11
- package/dist/hsb-color.d.mts +37 -40
- package/dist/hsb-color.d.ts +37 -40
- package/dist/hsb-color.js +164 -200
- package/dist/hsb-color.mjs +158 -175
- package/dist/hsl-color.d.mts +33 -36
- package/dist/hsl-color.d.ts +33 -36
- package/dist/hsl-color.js +139 -168
- package/dist/hsl-color.mjs +132 -142
- package/dist/index.d.mts +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.js +23 -60
- package/dist/index.mjs +2 -28
- package/dist/native-color.d.mts +1 -3
- package/dist/native-color.d.ts +1 -3
- package/dist/native-color.js +16 -39
- package/dist/native-color.mjs +12 -16
- package/dist/oklab-color.d.mts +57 -60
- package/dist/oklab-color.d.ts +57 -60
- package/dist/oklab-color.js +363 -379
- package/dist/oklab-color.mjs +353 -350
- package/dist/oklab-math.d.mts +26 -28
- package/dist/oklab-math.d.ts +26 -28
- package/dist/oklab-math.js +123 -142
- package/dist/oklab-math.mjs +108 -108
- package/dist/parse-color.d.mts +3 -6
- package/dist/parse-color.d.ts +3 -6
- package/dist/parse-color.js +22 -48
- package/dist/parse-color.mjs +13 -20
- package/dist/rgb-color.d.mts +33 -36
- package/dist/rgb-color.d.ts +33 -36
- package/dist/rgb-color.js +182 -220
- package/dist/rgb-color.mjs +174 -193
- package/dist/types.d.mts +82 -84
- package/dist/types.d.ts +82 -84
- package/dist/types.js +4 -18
- package/dist/types.mjs +1 -0
- package/package.json +3 -3
- package/dist/chunk-QZ7TP4HQ.mjs +0 -7
package/dist/hsl-color.js
CHANGED
|
@@ -1,170 +1,141 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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/hsl-color.ts
|
|
23
|
-
var hsl_color_exports = {};
|
|
24
|
-
__export(hsl_color_exports, {
|
|
25
|
-
HSLColor: () => HSLColor,
|
|
26
|
-
HSL_REGEX: () => HSL_REGEX
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
27
4
|
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
getFormat() {
|
|
158
|
-
return "hsla";
|
|
159
|
-
}
|
|
160
|
-
getChannels() {
|
|
161
|
-
return _HSLColor.colorChannels;
|
|
162
|
-
}
|
|
5
|
+
const require_color = require("./color.js");
|
|
6
|
+
const require_hsb_color = require("./hsb-color.js");
|
|
7
|
+
const require_rgb_color = require("./rgb-color.js");
|
|
8
|
+
const require_defineProperty = require("./_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.js");
|
|
9
|
+
let _zag_js_utils = require("@zag-js/utils");
|
|
10
|
+
// src/hsl-color.ts
|
|
11
|
+
const 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+)?)\)/;
|
|
12
|
+
var HSLColor = class HSLColor extends require_color.Color {
|
|
13
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
14
|
+
super();
|
|
15
|
+
this.hue = hue;
|
|
16
|
+
this.saturation = saturation;
|
|
17
|
+
this.lightness = lightness;
|
|
18
|
+
this.alpha = alpha;
|
|
19
|
+
}
|
|
20
|
+
static parse(value) {
|
|
21
|
+
let m;
|
|
22
|
+
if (m = value.match(HSL_REGEX)) {
|
|
23
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
24
|
+
return new HSLColor((0, _zag_js_utils.mod)(h, 360), (0, _zag_js_utils.clampValue)(s, 0, 100), (0, _zag_js_utils.clampValue)(l, 0, 100), (0, _zag_js_utils.clampValue)(a ?? 1, 0, 1));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
toString(format = "css") {
|
|
28
|
+
switch (format) {
|
|
29
|
+
case "hex": return this.toRGB().toString("hex");
|
|
30
|
+
case "hexa": return this.toRGB().toString("hexa");
|
|
31
|
+
case "hsl": return `hsl(${this.hue}, ${(0, _zag_js_utils.toFixedNumber)(this.saturation, 2)}%, ${(0, _zag_js_utils.toFixedNumber)(this.lightness, 2)}%)`;
|
|
32
|
+
case "css":
|
|
33
|
+
case "hsla": return `hsla(${this.hue}, ${(0, _zag_js_utils.toFixedNumber)(this.saturation, 2)}%, ${(0, _zag_js_utils.toFixedNumber)(this.lightness, 2)}%, ${this.alpha})`;
|
|
34
|
+
case "hsb": return this.toHSB().toString("hsb");
|
|
35
|
+
case "rgb": return this.toRGB().toString("rgb");
|
|
36
|
+
default: return this.toFormat(format).toString(format);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
toFormat(format) {
|
|
40
|
+
switch (format) {
|
|
41
|
+
case "hsla": return this;
|
|
42
|
+
case "hsba": return this.toHSB();
|
|
43
|
+
case "rgba": return this.toRGB();
|
|
44
|
+
case "oklab":
|
|
45
|
+
case "oklch": return this.toRGB().toFormat(format);
|
|
46
|
+
default: throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Converts a HSL color to HSB.
|
|
51
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
52
|
+
* @returns An HSBColor object.
|
|
53
|
+
*/
|
|
54
|
+
toHSB() {
|
|
55
|
+
let saturation = this.saturation / 100;
|
|
56
|
+
let lightness = this.lightness / 100;
|
|
57
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
58
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
59
|
+
return new require_hsb_color.HSBColor((0, _zag_js_utils.toFixedNumber)(this.hue, 2), (0, _zag_js_utils.toFixedNumber)(saturation * 100, 2), (0, _zag_js_utils.toFixedNumber)(brightness * 100, 2), (0, _zag_js_utils.toFixedNumber)(this.alpha, 2));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Converts a HSL color to RGB.
|
|
63
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
64
|
+
* @returns An RGBColor object.
|
|
65
|
+
*/
|
|
66
|
+
toRGB() {
|
|
67
|
+
let hue = this.hue;
|
|
68
|
+
let saturation = this.saturation / 100;
|
|
69
|
+
let lightness = this.lightness / 100;
|
|
70
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
71
|
+
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
72
|
+
return new require_rgb_color.RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), (0, _zag_js_utils.toFixedNumber)(this.alpha, 2));
|
|
73
|
+
}
|
|
74
|
+
clone() {
|
|
75
|
+
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
76
|
+
}
|
|
77
|
+
getChannelFormatOptions(channel) {
|
|
78
|
+
switch (channel) {
|
|
79
|
+
case "hue": return {
|
|
80
|
+
style: "unit",
|
|
81
|
+
unit: "degree",
|
|
82
|
+
unitDisplay: "narrow"
|
|
83
|
+
};
|
|
84
|
+
case "saturation":
|
|
85
|
+
case "lightness":
|
|
86
|
+
case "alpha": return { style: "percent" };
|
|
87
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
formatChannelValue(channel, locale) {
|
|
91
|
+
let options = this.getChannelFormatOptions(channel);
|
|
92
|
+
let value = this.getChannelValue(channel);
|
|
93
|
+
if (channel === "saturation" || channel === "lightness") value /= 100;
|
|
94
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
95
|
+
}
|
|
96
|
+
getChannelRange(channel) {
|
|
97
|
+
switch (channel) {
|
|
98
|
+
case "hue": return {
|
|
99
|
+
minValue: 0,
|
|
100
|
+
maxValue: 360,
|
|
101
|
+
step: 1,
|
|
102
|
+
pageSize: 15
|
|
103
|
+
};
|
|
104
|
+
case "saturation":
|
|
105
|
+
case "lightness": return {
|
|
106
|
+
minValue: 0,
|
|
107
|
+
maxValue: 100,
|
|
108
|
+
step: 1,
|
|
109
|
+
pageSize: 10
|
|
110
|
+
};
|
|
111
|
+
case "alpha": return {
|
|
112
|
+
minValue: 0,
|
|
113
|
+
maxValue: 1,
|
|
114
|
+
step: .01,
|
|
115
|
+
pageSize: .1
|
|
116
|
+
};
|
|
117
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
toJSON() {
|
|
121
|
+
return {
|
|
122
|
+
h: this.hue,
|
|
123
|
+
s: this.saturation,
|
|
124
|
+
l: this.lightness,
|
|
125
|
+
a: this.alpha
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
getFormat() {
|
|
129
|
+
return "hsla";
|
|
130
|
+
}
|
|
131
|
+
getChannels() {
|
|
132
|
+
return HSLColor.colorChannels;
|
|
133
|
+
}
|
|
163
134
|
};
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
135
|
+
require_defineProperty.default(HSLColor, "colorChannels", [
|
|
136
|
+
"hue",
|
|
137
|
+
"saturation",
|
|
138
|
+
"lightness"
|
|
139
|
+
]);
|
|
140
|
+
exports.HSLColor = HSLColor;
|
|
141
|
+
exports.HSL_REGEX = HSL_REGEX;
|
package/dist/hsl-color.mjs
CHANGED
|
@@ -1,146 +1,136 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "./chunk-QZ7TP4HQ.mjs";
|
|
4
|
-
|
|
5
|
-
// src/hsl-color.ts
|
|
6
|
-
import { clampValue, mod, toFixedNumber } from "@zag-js/utils";
|
|
7
1
|
import { Color } from "./color.mjs";
|
|
8
2
|
import { HSBColor } from "./hsb-color.mjs";
|
|
9
3
|
import { RGBColor } from "./rgb-color.mjs";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
getChannels() {
|
|
138
|
-
return _HSLColor.colorChannels;
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
__publicField(_HSLColor, "colorChannels", ["hue", "saturation", "lightness"]);
|
|
142
|
-
var HSLColor = _HSLColor;
|
|
143
|
-
export {
|
|
144
|
-
HSLColor,
|
|
145
|
-
HSL_REGEX
|
|
4
|
+
import _defineProperty from "./_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.mjs";
|
|
5
|
+
import { clampValue, mod, toFixedNumber } from "@zag-js/utils";
|
|
6
|
+
// src/hsl-color.ts
|
|
7
|
+
const 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+)?)\)/;
|
|
8
|
+
var HSLColor = class HSLColor extends Color {
|
|
9
|
+
constructor(hue, saturation, lightness, alpha) {
|
|
10
|
+
super();
|
|
11
|
+
this.hue = hue;
|
|
12
|
+
this.saturation = saturation;
|
|
13
|
+
this.lightness = lightness;
|
|
14
|
+
this.alpha = alpha;
|
|
15
|
+
}
|
|
16
|
+
static parse(value) {
|
|
17
|
+
let m;
|
|
18
|
+
if (m = value.match(HSL_REGEX)) {
|
|
19
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
20
|
+
return new HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
toString(format = "css") {
|
|
24
|
+
switch (format) {
|
|
25
|
+
case "hex": return this.toRGB().toString("hex");
|
|
26
|
+
case "hexa": return this.toRGB().toString("hexa");
|
|
27
|
+
case "hsl": return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
|
|
28
|
+
case "css":
|
|
29
|
+
case "hsla": return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
30
|
+
case "hsb": return this.toHSB().toString("hsb");
|
|
31
|
+
case "rgb": return this.toRGB().toString("rgb");
|
|
32
|
+
default: return this.toFormat(format).toString(format);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
toFormat(format) {
|
|
36
|
+
switch (format) {
|
|
37
|
+
case "hsla": return this;
|
|
38
|
+
case "hsba": return this.toHSB();
|
|
39
|
+
case "rgba": return this.toRGB();
|
|
40
|
+
case "oklab":
|
|
41
|
+
case "oklch": return this.toRGB().toFormat(format);
|
|
42
|
+
default: throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Converts a HSL color to HSB.
|
|
47
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
48
|
+
* @returns An HSBColor object.
|
|
49
|
+
*/
|
|
50
|
+
toHSB() {
|
|
51
|
+
let saturation = this.saturation / 100;
|
|
52
|
+
let lightness = this.lightness / 100;
|
|
53
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
54
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
55
|
+
return new HSBColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), toFixedNumber(this.alpha, 2));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts a HSL color to RGB.
|
|
59
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
60
|
+
* @returns An RGBColor object.
|
|
61
|
+
*/
|
|
62
|
+
toRGB() {
|
|
63
|
+
let hue = this.hue;
|
|
64
|
+
let saturation = this.saturation / 100;
|
|
65
|
+
let lightness = this.lightness / 100;
|
|
66
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
67
|
+
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
68
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), toFixedNumber(this.alpha, 2));
|
|
69
|
+
}
|
|
70
|
+
clone() {
|
|
71
|
+
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
72
|
+
}
|
|
73
|
+
getChannelFormatOptions(channel) {
|
|
74
|
+
switch (channel) {
|
|
75
|
+
case "hue": return {
|
|
76
|
+
style: "unit",
|
|
77
|
+
unit: "degree",
|
|
78
|
+
unitDisplay: "narrow"
|
|
79
|
+
};
|
|
80
|
+
case "saturation":
|
|
81
|
+
case "lightness":
|
|
82
|
+
case "alpha": return { style: "percent" };
|
|
83
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
formatChannelValue(channel, locale) {
|
|
87
|
+
let options = this.getChannelFormatOptions(channel);
|
|
88
|
+
let value = this.getChannelValue(channel);
|
|
89
|
+
if (channel === "saturation" || channel === "lightness") value /= 100;
|
|
90
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
91
|
+
}
|
|
92
|
+
getChannelRange(channel) {
|
|
93
|
+
switch (channel) {
|
|
94
|
+
case "hue": return {
|
|
95
|
+
minValue: 0,
|
|
96
|
+
maxValue: 360,
|
|
97
|
+
step: 1,
|
|
98
|
+
pageSize: 15
|
|
99
|
+
};
|
|
100
|
+
case "saturation":
|
|
101
|
+
case "lightness": return {
|
|
102
|
+
minValue: 0,
|
|
103
|
+
maxValue: 100,
|
|
104
|
+
step: 1,
|
|
105
|
+
pageSize: 10
|
|
106
|
+
};
|
|
107
|
+
case "alpha": return {
|
|
108
|
+
minValue: 0,
|
|
109
|
+
maxValue: 1,
|
|
110
|
+
step: .01,
|
|
111
|
+
pageSize: .1
|
|
112
|
+
};
|
|
113
|
+
default: throw new Error("Unknown color channel: " + channel);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
toJSON() {
|
|
117
|
+
return {
|
|
118
|
+
h: this.hue,
|
|
119
|
+
s: this.saturation,
|
|
120
|
+
l: this.lightness,
|
|
121
|
+
a: this.alpha
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
getFormat() {
|
|
125
|
+
return "hsla";
|
|
126
|
+
}
|
|
127
|
+
getChannels() {
|
|
128
|
+
return HSLColor.colorChannels;
|
|
129
|
+
}
|
|
146
130
|
};
|
|
131
|
+
_defineProperty(HSLColor, "colorChannels", [
|
|
132
|
+
"hue",
|
|
133
|
+
"saturation",
|
|
134
|
+
"lightness"
|
|
135
|
+
]);
|
|
136
|
+
export { HSLColor, HSL_REGEX };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { getColorAreaGradient } from "./area-gradient.mjs";
|
|
2
|
+
import { Color } from "./color.mjs";
|
|
3
|
+
import { isDisplayP3HsvInSrgbGamut, isLinearRgbInSrgbGamut, linearRgbToOklab, oklabToLinearRgb, oklabToOklch, oklchToOklab, srgbToLinearRgb } from "./oklab-math.mjs";
|
|
4
|
+
import { isInSrgbGamut } from "./gamut.mjs";
|
|
5
|
+
import { OklabColor, OklchColor } from "./oklab-color.mjs";
|
|
6
|
+
import { normalizeColor, parseColor } from "./parse-color.mjs";
|
|
7
|
+
import { ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.mjs";
|
|
8
|
+
export { Color, type ColorAxes, type ColorChannel, type ColorChannelRange, type ColorFormat, type ColorStringFormat, type ColorType, OklabColor, OklchColor, getColorAreaGradient, isDisplayP3HsvInSrgbGamut, isInSrgbGamut, isLinearRgbInSrgbGamut, linearRgbToOklab, normalizeColor, oklabToLinearRgb, oklabToOklch, oklchToOklab, parseColor, srgbToLinearRgb };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { getColorAreaGradient } from "./area-gradient.js";
|
|
2
|
+
import { Color } from "./color.js";
|
|
3
|
+
import { isDisplayP3HsvInSrgbGamut, isLinearRgbInSrgbGamut, linearRgbToOklab, oklabToLinearRgb, oklabToOklch, oklchToOklab, srgbToLinearRgb } from "./oklab-math.js";
|
|
4
|
+
import { isInSrgbGamut } from "./gamut.js";
|
|
5
|
+
import { OklabColor, OklchColor } from "./oklab-color.js";
|
|
6
|
+
import { normalizeColor, parseColor } from "./parse-color.js";
|
|
7
|
+
import { ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.js";
|
|
8
|
+
export { Color, type ColorAxes, type ColorChannel, type ColorChannelRange, type ColorFormat, type ColorStringFormat, type ColorType, OklabColor, OklchColor, getColorAreaGradient, isDisplayP3HsvInSrgbGamut, isInSrgbGamut, isLinearRgbInSrgbGamut, linearRgbToOklab, normalizeColor, oklabToLinearRgb, oklabToOklch, oklchToOklab, parseColor, srgbToLinearRgb };
|