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