@zag-js/color-utils 1.48.0 → 1.49.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.
@@ -1,5 +1,5 @@
1
1
  const require_toPropertyKey = require("./toPropertyKey.js");
2
- // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
2
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/defineProperty.js
3
3
  function _defineProperty(e, r, t) {
4
4
  return (r = require_toPropertyKey.default(r)) in e ? Object.defineProperty(e, r, {
5
5
  value: t,
@@ -1,5 +1,5 @@
1
1
  import toPropertyKey from "./toPropertyKey.mjs";
2
- // \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
2
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/defineProperty.js
3
3
  function _defineProperty(e, r, t) {
4
4
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
5
5
  value: t,
@@ -1,5 +1,5 @@
1
1
  const require_typeof = require("./typeof.js");
2
- // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
2
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/toPrimitive.js
3
3
  function toPrimitive(t, e) {
4
4
  if ("object" != require_typeof.default(t) || !t) return t;
5
5
  var r;
@@ -1,5 +1,5 @@
1
1
  import _typeof from "./typeof.mjs";
2
- // \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
2
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/toPrimitive.js
3
3
  function toPrimitive(t, e) {
4
4
  if ("object" != _typeof(t) || !t) return t;
5
5
  var r;
@@ -1,6 +1,6 @@
1
1
  const require_typeof = require("./typeof.js");
2
2
  const require_toPrimitive = require("./toPrimitive.js");
3
- // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
3
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/toPropertyKey.js
4
4
  function toPropertyKey(t) {
5
5
  var i = require_toPrimitive.default(t, "string");
6
6
  return "symbol" == require_typeof.default(i) ? i : i + "";
@@ -1,6 +1,6 @@
1
1
  import _typeof from "./typeof.mjs";
2
2
  import toPrimitive from "./toPrimitive.mjs";
3
- // \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
3
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/toPropertyKey.js
4
4
  function toPropertyKey(t) {
5
5
  var i = toPrimitive(t, "string");
6
6
  return "symbol" == _typeof(i) ? i : i + "";
@@ -1,4 +1,4 @@
1
- // \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
1
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/typeof.js
2
2
  function _typeof(o) {
3
3
  "@babel/helpers - typeof";
4
4
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -1,4 +1,4 @@
1
- // \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
1
+ // \0@oxc-project+runtime@0.159.0/helpers/esm/typeof.js
2
2
  function _typeof(o) {
3
3
  "@babel/helpers - typeof";
4
4
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
package/dist/hsb-color.js CHANGED
@@ -2,13 +2,14 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
+ let _zag_js_utils = require("@zag-js/utils");
5
6
  const require_color = require("./color.js");
6
7
  const require_hsl_color = require("./hsl-color.js");
8
+ const require_parse_utils = require("./parse-utils.js");
7
9
  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
+ const require_defineProperty = require("./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js");
10
11
  // src/hsb-color.ts
11
- const 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+)?)\)/;
12
+ const NAMES = ["hsb", "hsba"];
12
13
  var HSBColor = class HSBColor extends require_color.Color {
13
14
  constructor(hue, saturation, brightness, alpha) {
14
15
  super();
@@ -18,11 +19,8 @@ var HSBColor = class HSBColor extends require_color.Color {
18
19
  this.alpha = alpha;
19
20
  }
20
21
  static parse(value) {
21
- let m;
22
- if (m = value.match(HSB_REGEX)) {
23
- const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
24
- return new HSBColor((0, _zag_js_utils.mod)(h, 360), (0, _zag_js_utils.clampValue)(s, 0, 100), (0, _zag_js_utils.clampValue)(b, 0, 100), (0, _zag_js_utils.clampValue)(a ?? 1, 0, 1));
25
- }
22
+ const args = require_parse_utils.parseHueColorArgs(value, NAMES);
23
+ if (args) return new HSBColor(...args);
26
24
  }
27
25
  toString(format = "css") {
28
26
  switch (format) {
@@ -1,10 +1,11 @@
1
+ import { toFixedNumber } from "@zag-js/utils";
1
2
  import { Color } from "./color.mjs";
2
3
  import { HSLColor } from "./hsl-color.mjs";
4
+ import { parseHueColorArgs } from "./parse-utils.mjs";
3
5
  import { RGBColor } from "./rgb-color.mjs";
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
+ import _defineProperty from "./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs";
6
7
  // src/hsb-color.ts
7
- const 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+)?)\)/;
8
+ const NAMES = ["hsb", "hsba"];
8
9
  var HSBColor = class HSBColor extends Color {
9
10
  constructor(hue, saturation, brightness, alpha) {
10
11
  super();
@@ -14,11 +15,8 @@ var HSBColor = class HSBColor extends Color {
14
15
  this.alpha = alpha;
15
16
  }
16
17
  static parse(value) {
17
- let m;
18
- if (m = value.match(HSB_REGEX)) {
19
- const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
20
- return new HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
21
- }
18
+ const args = parseHueColorArgs(value, NAMES);
19
+ if (args) return new HSBColor(...args);
22
20
  }
23
21
  toString(format = "css") {
24
22
  switch (format) {
@@ -1,6 +1,5 @@
1
1
  import { Color } from "./color.mjs";
2
2
  import { ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.mjs";
3
- export declare const HSL_REGEX: RegExp;
4
3
  export declare class HSLColor extends Color {
5
4
  private hue;
6
5
  private saturation;
@@ -1,6 +1,5 @@
1
1
  import { Color } from "./color.js";
2
2
  import { ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.js";
3
- export declare const HSL_REGEX: RegExp;
4
3
  export declare class HSLColor extends Color {
5
4
  private hue;
6
5
  private saturation;
package/dist/hsl-color.js CHANGED
@@ -2,13 +2,14 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
+ let _zag_js_utils = require("@zag-js/utils");
5
6
  const require_color = require("./color.js");
6
7
  const require_hsb_color = require("./hsb-color.js");
8
+ const require_parse_utils = require("./parse-utils.js");
7
9
  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
+ const require_defineProperty = require("./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js");
10
11
  // 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
+ const NAMES = ["hsl", "hsla"];
12
13
  var HSLColor = class HSLColor extends require_color.Color {
13
14
  constructor(hue, saturation, lightness, alpha) {
14
15
  super();
@@ -18,11 +19,8 @@ var HSLColor = class HSLColor extends require_color.Color {
18
19
  this.alpha = alpha;
19
20
  }
20
21
  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
- }
22
+ const args = require_parse_utils.parseHueColorArgs(value, NAMES);
23
+ if (args) return new HSLColor(...args);
26
24
  }
27
25
  toString(format = "css") {
28
26
  switch (format) {
@@ -136,4 +134,3 @@ require_defineProperty.default(HSLColor, "colorChannels", [
136
134
  "lightness"
137
135
  ]);
138
136
  exports.HSLColor = HSLColor;
139
- exports.HSL_REGEX = HSL_REGEX;
@@ -1,10 +1,11 @@
1
+ import { toFixedNumber } from "@zag-js/utils";
1
2
  import { Color } from "./color.mjs";
2
3
  import { HSBColor } from "./hsb-color.mjs";
4
+ import { parseHueColorArgs } from "./parse-utils.mjs";
3
5
  import { RGBColor } from "./rgb-color.mjs";
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
+ import _defineProperty from "./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs";
6
7
  // 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
+ const NAMES = ["hsl", "hsla"];
8
9
  var HSLColor = class HSLColor extends Color {
9
10
  constructor(hue, saturation, lightness, alpha) {
10
11
  super();
@@ -14,11 +15,8 @@ var HSLColor = class HSLColor extends Color {
14
15
  this.alpha = alpha;
15
16
  }
16
17
  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
- }
18
+ const args = parseHueColorArgs(value, NAMES);
19
+ if (args) return new HSLColor(...args);
22
20
  }
23
21
  toString(format = "css") {
24
22
  switch (format) {
@@ -131,4 +129,4 @@ _defineProperty(HSLColor, "colorChannels", [
131
129
  "saturation",
132
130
  "lightness"
133
131
  ]);
134
- export { HSLColor, HSL_REGEX };
132
+ export { HSLColor };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns the channel tokens of `name(a, b, c[, d])` or `name(a b c[ / d])`,
3
+ * where `names` lists the accepted function names (e.g. `rgb`, `rgba`).
4
+ */
5
+ export declare function getColorArgs(value: string, names: string[]): string[] | undefined;
6
+ /**
7
+ * Parses a number, or a percentage where 100% equals `percentScale`.
8
+ * Percentages are rejected when `percentScale` is omitted.
9
+ */
10
+ export declare function parseNumber(token: string, percentScale?: number): number | undefined;
11
+ /** Parses the args of `hsl`/`hsb`-style colors into `[hue, percent, percent, alpha]` */
12
+ export declare function parseHueColorArgs(value: string, names: string[]): [number, number, number, number] | undefined;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns the channel tokens of `name(a, b, c[, d])` or `name(a b c[ / d])`,
3
+ * where `names` lists the accepted function names (e.g. `rgb`, `rgba`).
4
+ */
5
+ export declare function getColorArgs(value: string, names: string[]): string[] | undefined;
6
+ /**
7
+ * Parses a number, or a percentage where 100% equals `percentScale`.
8
+ * Percentages are rejected when `percentScale` is omitted.
9
+ */
10
+ export declare function parseNumber(token: string, percentScale?: number): number | undefined;
11
+ /** Parses the args of `hsl`/`hsb`-style colors into `[hue, percent, percent, alpha]` */
12
+ export declare function parseHueColorArgs(value: string, names: string[]): [number, number, number, number] | undefined;
@@ -0,0 +1,62 @@
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ let _zag_js_utils = require("@zag-js/utils");
6
+ // src/parse-utils.ts
7
+ const NUMBER_REGEX = /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[-+]?\d+)?$/i;
8
+ /**
9
+ * Returns the channel tokens of `name(a, b, c[, d])` or `name(a b c[ / d])`,
10
+ * where `names` lists the accepted function names (e.g. `rgb`, `rgba`).
11
+ */
12
+ function getColorArgs(value, names) {
13
+ const start = value.indexOf("(");
14
+ if (start < 0 || value[value.length - 1] !== ")") return;
15
+ if (!names.includes(value.slice(0, start))) return;
16
+ const body = value.slice(start + 1, -1);
17
+ let args;
18
+ if (body.includes(",")) args = body.split(",");
19
+ else {
20
+ const [main, alpha, ...rest] = body.split("/");
21
+ if (rest.length) return;
22
+ args = main.trim().split(/\s+/);
23
+ if (alpha !== void 0) args.push(alpha);
24
+ }
25
+ if (args.length < 3 || args.length > 4) return;
26
+ return args;
27
+ }
28
+ /**
29
+ * Parses a number, or a percentage where 100% equals `percentScale`.
30
+ * Percentages are rejected when `percentScale` is omitted.
31
+ */
32
+ function parseNumber(token, percentScale) {
33
+ token = token.trim();
34
+ const isPercent = token[token.length - 1] === "%";
35
+ if (isPercent) {
36
+ if (percentScale === void 0) return;
37
+ token = token.slice(0, -1);
38
+ }
39
+ if (!NUMBER_REGEX.test(token)) return;
40
+ const num = Number(token);
41
+ return isPercent ? num / 100 * percentScale : num;
42
+ }
43
+ /** Parses the args of `hsl`/`hsb`-style colors into `[hue, percent, percent, alpha]` */
44
+ function parseHueColorArgs(value, names) {
45
+ const args = getColorArgs(value, names);
46
+ if (!args) return;
47
+ const hueToken = args[0].trim();
48
+ const hue = parseNumber(hueToken.endsWith("deg") ? hueToken.slice(0, -3) : hueToken);
49
+ const x = parseNumber(args[1], 100);
50
+ const y = parseNumber(args[2], 100);
51
+ const alpha = args[3] === void 0 ? 1 : parseNumber(args[3], 1);
52
+ if (hue === void 0 || x === void 0 || y === void 0 || alpha === void 0) return;
53
+ return [
54
+ (0, _zag_js_utils.mod)(hue, 360),
55
+ (0, _zag_js_utils.clampValue)(x, 0, 100),
56
+ (0, _zag_js_utils.clampValue)(y, 0, 100),
57
+ (0, _zag_js_utils.clampValue)(alpha, 0, 1)
58
+ ];
59
+ }
60
+ exports.getColorArgs = getColorArgs;
61
+ exports.parseHueColorArgs = parseHueColorArgs;
62
+ exports.parseNumber = parseNumber;
@@ -0,0 +1,56 @@
1
+ import { clampValue, mod } from "@zag-js/utils";
2
+ // src/parse-utils.ts
3
+ const NUMBER_REGEX = /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[-+]?\d+)?$/i;
4
+ /**
5
+ * Returns the channel tokens of `name(a, b, c[, d])` or `name(a b c[ / d])`,
6
+ * where `names` lists the accepted function names (e.g. `rgb`, `rgba`).
7
+ */
8
+ function getColorArgs(value, names) {
9
+ const start = value.indexOf("(");
10
+ if (start < 0 || value[value.length - 1] !== ")") return;
11
+ if (!names.includes(value.slice(0, start))) return;
12
+ const body = value.slice(start + 1, -1);
13
+ let args;
14
+ if (body.includes(",")) args = body.split(",");
15
+ else {
16
+ const [main, alpha, ...rest] = body.split("/");
17
+ if (rest.length) return;
18
+ args = main.trim().split(/\s+/);
19
+ if (alpha !== void 0) args.push(alpha);
20
+ }
21
+ if (args.length < 3 || args.length > 4) return;
22
+ return args;
23
+ }
24
+ /**
25
+ * Parses a number, or a percentage where 100% equals `percentScale`.
26
+ * Percentages are rejected when `percentScale` is omitted.
27
+ */
28
+ function parseNumber(token, percentScale) {
29
+ token = token.trim();
30
+ const isPercent = token[token.length - 1] === "%";
31
+ if (isPercent) {
32
+ if (percentScale === void 0) return;
33
+ token = token.slice(0, -1);
34
+ }
35
+ if (!NUMBER_REGEX.test(token)) return;
36
+ const num = Number(token);
37
+ return isPercent ? num / 100 * percentScale : num;
38
+ }
39
+ /** Parses the args of `hsl`/`hsb`-style colors into `[hue, percent, percent, alpha]` */
40
+ function parseHueColorArgs(value, names) {
41
+ const args = getColorArgs(value, names);
42
+ if (!args) return;
43
+ const hueToken = args[0].trim();
44
+ const hue = parseNumber(hueToken.endsWith("deg") ? hueToken.slice(0, -3) : hueToken);
45
+ const x = parseNumber(args[1], 100);
46
+ const y = parseNumber(args[2], 100);
47
+ const alpha = args[3] === void 0 ? 1 : parseNumber(args[3], 1);
48
+ if (hue === void 0 || x === void 0 || y === void 0 || alpha === void 0) return;
49
+ return [
50
+ mod(hue, 360),
51
+ clampValue(x, 0, 100),
52
+ clampValue(y, 0, 100),
53
+ clampValue(alpha, 0, 1)
54
+ ];
55
+ }
56
+ export { getColorArgs, parseHueColorArgs, parseNumber };
package/dist/rgb-color.js CHANGED
@@ -2,15 +2,17 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
+ let _zag_js_utils = require("@zag-js/utils");
5
6
  const require_color = require("./color.js");
6
7
  const require_hsb_color = require("./hsb-color.js");
7
8
  const require_hsl_color = require("./hsl-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");
9
+ const require_parse_utils = require("./parse-utils.js");
10
+ const require_defineProperty = require("./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js");
10
11
  // src/rgb-color.ts
11
12
  const HEX_COLOR_REGEX = /^#[\da-f]+$/i;
12
- const RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
13
13
  const HEX_STARTING_REGEX = /[^#]/gi;
14
+ const RGB_NAMES = ["rgb", "rgba"];
15
+ const toHex = (value) => Math.round(value).toString(16).padStart(2, "0");
14
16
  var RGBColor = class RGBColor extends require_color.Color {
15
17
  constructor(red, green, blue, alpha) {
16
18
  super();
@@ -31,14 +33,19 @@ var RGBColor = class RGBColor extends require_color.Color {
31
33
  while (values.length > 0) colors.push(parseInt(values.splice(0, 2).join(""), 16));
32
34
  colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
33
35
  }
34
- const match = value.match(RGB_COLOR_REGEX);
35
- if (match?.[1]) colors = match[1].split(",").map((value) => Number(value.trim())).map((num, i) => (0, _zag_js_utils.clampValue)(num, 0, i < 3 ? 255 : 1));
36
+ const args = require_parse_utils.getColorArgs(value, RGB_NAMES);
37
+ if (args) for (let i = 0; i < args.length; i++) {
38
+ const max = i < 3 ? 255 : 1;
39
+ const num = require_parse_utils.parseNumber(args[i], max);
40
+ if (num === void 0) return;
41
+ colors[i] = (0, _zag_js_utils.clampValue)(num, 0, max);
42
+ }
36
43
  return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
37
44
  }
38
45
  toString(format = "css") {
39
46
  switch (format) {
40
- case "hex": return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
41
- case "hexa": 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();
47
+ case "hex": return "#" + (toHex(this.red) + toHex(this.green) + toHex(this.blue)).toUpperCase();
48
+ case "hexa": return "#" + (toHex(this.red) + toHex(this.green) + toHex(this.blue) + toHex(this.alpha * 255)).toUpperCase();
42
49
  case "rgb": return `rgb(${this.red}, ${this.green}, ${this.blue})`;
43
50
  case "css":
44
51
  case "rgba": return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
@@ -56,7 +63,7 @@ var RGBColor = class RGBColor extends require_color.Color {
56
63
  }
57
64
  }
58
65
  toHexInt() {
59
- return this.red << 16 | this.green << 8 | this.blue;
66
+ return Math.round(this.red) << 16 | Math.round(this.green) << 8 | Math.round(this.blue);
60
67
  }
61
68
  /**
62
69
  * Converts an RGB color value to HSB.
@@ -1,12 +1,14 @@
1
+ import { clampValue, toFixedNumber } from "@zag-js/utils";
1
2
  import { Color } from "./color.mjs";
2
3
  import { HSBColor } from "./hsb-color.mjs";
3
4
  import { HSLColor } from "./hsl-color.mjs";
4
- import _defineProperty from "./_virtual/_@oxc-project_runtime@0.158.0/helpers/esm/defineProperty.mjs";
5
- import { clampValue, toFixedNumber } from "@zag-js/utils";
5
+ import { getColorArgs, parseNumber } from "./parse-utils.mjs";
6
+ import _defineProperty from "./_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs";
6
7
  // src/rgb-color.ts
7
8
  const HEX_COLOR_REGEX = /^#[\da-f]+$/i;
8
- const RGB_COLOR_REGEX = /^rgba?\((.*)\)$/;
9
9
  const HEX_STARTING_REGEX = /[^#]/gi;
10
+ const RGB_NAMES = ["rgb", "rgba"];
11
+ const toHex = (value) => Math.round(value).toString(16).padStart(2, "0");
10
12
  var RGBColor = class RGBColor extends Color {
11
13
  constructor(red, green, blue, alpha) {
12
14
  super();
@@ -27,14 +29,19 @@ var RGBColor = class RGBColor extends Color {
27
29
  while (values.length > 0) colors.push(parseInt(values.splice(0, 2).join(""), 16));
28
30
  colors[3] = colors[3] !== void 0 ? colors[3] / 255 : void 0;
29
31
  }
30
- const match = value.match(RGB_COLOR_REGEX);
31
- if (match?.[1]) colors = match[1].split(",").map((value) => Number(value.trim())).map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1));
32
+ const args = getColorArgs(value, RGB_NAMES);
33
+ if (args) for (let i = 0; i < args.length; i++) {
34
+ const max = i < 3 ? 255 : 1;
35
+ const num = parseNumber(args[i], max);
36
+ if (num === void 0) return;
37
+ colors[i] = clampValue(num, 0, max);
38
+ }
32
39
  return colors.length < 3 ? void 0 : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
33
40
  }
34
41
  toString(format = "css") {
35
42
  switch (format) {
36
- case "hex": return "#" + (this.red.toString(16).padStart(2, "0") + this.green.toString(16).padStart(2, "0") + this.blue.toString(16).padStart(2, "0")).toUpperCase();
37
- case "hexa": 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();
43
+ case "hex": return "#" + (toHex(this.red) + toHex(this.green) + toHex(this.blue)).toUpperCase();
44
+ case "hexa": return "#" + (toHex(this.red) + toHex(this.green) + toHex(this.blue) + toHex(this.alpha * 255)).toUpperCase();
38
45
  case "rgb": return `rgb(${this.red}, ${this.green}, ${this.blue})`;
39
46
  case "css":
40
47
  case "rgba": return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
@@ -52,7 +59,7 @@ var RGBColor = class RGBColor extends Color {
52
59
  }
53
60
  }
54
61
  toHexInt() {
55
- return this.red << 16 | this.green << 8 | this.blue;
62
+ return Math.round(this.red) << 16 | Math.round(this.green) << 8 | Math.round(this.blue);
56
63
  }
57
64
  /**
58
65
  * Converts an RGB color value to HSB.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/color-utils",
3
- "version": "1.48.0",
3
+ "version": "1.49.0",
4
4
  "description": "Color utilities for zag.js",
5
5
  "keywords": [
6
6
  "js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "clean-package": "../../../clean-package.config.json",
29
29
  "dependencies": {
30
- "@zag-js/utils": "1.48.0"
30
+ "@zag-js/utils": "1.49.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "clean-package": "2.2.0"