@zag-js/color-utils 1.47.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.
Files changed (54) hide show
  1. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.js +11 -0
  2. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/defineProperty.mjs +11 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.js +13 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPrimitive.mjs +13 -0
  5. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.js +8 -0
  6. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/toPropertyKey.mjs +8 -0
  7. package/dist/_virtual/_@oxc-project_runtime@0.159.0/helpers/esm/typeof.js +15 -0
  8. package/dist/_virtual/_@oxc-project_runtime@0.159.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 +53 -84
  12. package/dist/area-gradient.mjs +49 -71
  13. package/dist/color-format-gradient.d.mts +58 -60
  14. package/dist/color-format-gradient.d.ts +58 -60
  15. package/dist/color-format-gradient.js +93 -154
  16. package/dist/color-format-gradient.mjs +81 -123
  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/hsb-color.d.mts +32 -35
  22. package/dist/hsb-color.d.ts +32 -35
  23. package/dist/hsb-color.js +133 -163
  24. package/dist/hsb-color.mjs +127 -138
  25. package/dist/hsl-color.d.mts +32 -36
  26. package/dist/hsl-color.d.ts +32 -36
  27. package/dist/hsl-color.js +134 -165
  28. package/dist/hsl-color.mjs +128 -139
  29. package/dist/index.d.mts +5 -4
  30. package/dist/index.d.ts +5 -4
  31. package/dist/index.js +10 -37
  32. package/dist/index.mjs +1 -9
  33. package/dist/native-color.d.mts +1 -3
  34. package/dist/native-color.d.ts +1 -3
  35. package/dist/native-color.js +16 -39
  36. package/dist/native-color.mjs +12 -16
  37. package/dist/parse-color.d.mts +3 -6
  38. package/dist/parse-color.d.ts +3 -6
  39. package/dist/parse-color.js +21 -47
  40. package/dist/parse-color.mjs +13 -20
  41. package/dist/parse-utils.d.mts +12 -0
  42. package/dist/parse-utils.d.ts +12 -0
  43. package/dist/parse-utils.js +62 -0
  44. package/dist/parse-utils.mjs +56 -0
  45. package/dist/rgb-color.d.mts +33 -36
  46. package/dist/rgb-color.d.ts +33 -36
  47. package/dist/rgb-color.js +181 -211
  48. package/dist/rgb-color.mjs +174 -185
  49. package/dist/types.d.mts +82 -84
  50. package/dist/types.d.ts +82 -84
  51. package/dist/types.js +4 -18
  52. package/dist/types.mjs +1 -0
  53. package/package.json +4 -4
  54. package/dist/chunk-QZ7TP4HQ.mjs +0 -7
@@ -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 };
@@ -1,36 +1,33 @@
1
- import { Color } from './color.mjs';
2
- import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.mjs';
3
-
4
- declare class RGBColor extends Color {
5
- private red;
6
- private green;
7
- private blue;
8
- private alpha;
9
- constructor(red: number, green: number, blue: number, alpha: number);
10
- static parse(value: string): RGBColor | undefined;
11
- toString(format?: ColorStringFormat): string;
12
- toFormat(format: ColorFormat): ColorType;
13
- toHexInt(): number;
14
- /**
15
- * Converts an RGB color value to HSB.
16
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
17
- * @returns An HSBColor object.
18
- */
19
- private toHSB;
20
- /**
21
- * Converts an RGB color value to HSL.
22
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
23
- * @returns An HSLColor object.
24
- */
25
- private toHSL;
26
- clone(): ColorType;
27
- getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
28
- formatChannelValue(channel: ColorChannel, locale: string): string;
29
- getChannelRange(channel: ColorChannel): ColorChannelRange;
30
- toJSON(): Record<"r" | "g" | "b" | "a", number>;
31
- getFormat(): ColorFormat;
32
- private static colorChannels;
33
- getChannels(): [ColorChannel, ColorChannel, ColorChannel];
34
- }
35
-
36
- export { RGBColor };
1
+ import { Color } from "./color.mjs";
2
+ import { ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.mjs";
3
+ export declare class RGBColor extends Color {
4
+ private red;
5
+ private green;
6
+ private blue;
7
+ private alpha;
8
+ constructor(red: number, green: number, blue: number, alpha: number);
9
+ static parse(value: string): RGBColor | undefined;
10
+ toString(format?: ColorStringFormat): string;
11
+ toFormat(format: ColorFormat): ColorType;
12
+ toHexInt(): number;
13
+ /**
14
+ * Converts an RGB color value to HSB.
15
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
16
+ * @returns An HSBColor object.
17
+ */
18
+ private toHSB;
19
+ /**
20
+ * Converts an RGB color value to HSL.
21
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
22
+ * @returns An HSLColor object.
23
+ */
24
+ private toHSL;
25
+ clone(): ColorType;
26
+ getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
27
+ formatChannelValue(channel: ColorChannel, locale: string): string;
28
+ getChannelRange(channel: ColorChannel): ColorChannelRange;
29
+ toJSON(): Record<"r" | "g" | "b" | "a", number>;
30
+ getFormat(): ColorFormat;
31
+ private static colorChannels;
32
+ getChannels(): [ColorChannel, ColorChannel, ColorChannel];
33
+ }
@@ -1,36 +1,33 @@
1
- import { Color } from './color.js';
2
- import { ColorStringFormat, ColorFormat, ColorType, ColorChannel, ColorChannelRange } from './types.js';
3
-
4
- declare class RGBColor extends Color {
5
- private red;
6
- private green;
7
- private blue;
8
- private alpha;
9
- constructor(red: number, green: number, blue: number, alpha: number);
10
- static parse(value: string): RGBColor | undefined;
11
- toString(format?: ColorStringFormat): string;
12
- toFormat(format: ColorFormat): ColorType;
13
- toHexInt(): number;
14
- /**
15
- * Converts an RGB color value to HSB.
16
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
17
- * @returns An HSBColor object.
18
- */
19
- private toHSB;
20
- /**
21
- * Converts an RGB color value to HSL.
22
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
23
- * @returns An HSLColor object.
24
- */
25
- private toHSL;
26
- clone(): ColorType;
27
- getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
28
- formatChannelValue(channel: ColorChannel, locale: string): string;
29
- getChannelRange(channel: ColorChannel): ColorChannelRange;
30
- toJSON(): Record<"r" | "g" | "b" | "a", number>;
31
- getFormat(): ColorFormat;
32
- private static colorChannels;
33
- getChannels(): [ColorChannel, ColorChannel, ColorChannel];
34
- }
35
-
36
- export { RGBColor };
1
+ import { Color } from "./color.js";
2
+ import { ColorChannel, ColorChannelRange, ColorFormat, ColorStringFormat, ColorType } from "./types.js";
3
+ export declare class RGBColor extends Color {
4
+ private red;
5
+ private green;
6
+ private blue;
7
+ private alpha;
8
+ constructor(red: number, green: number, blue: number, alpha: number);
9
+ static parse(value: string): RGBColor | undefined;
10
+ toString(format?: ColorStringFormat): string;
11
+ toFormat(format: ColorFormat): ColorType;
12
+ toHexInt(): number;
13
+ /**
14
+ * Converts an RGB color value to HSB.
15
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
16
+ * @returns An HSBColor object.
17
+ */
18
+ private toHSB;
19
+ /**
20
+ * Converts an RGB color value to HSL.
21
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
22
+ * @returns An HSLColor object.
23
+ */
24
+ private toHSL;
25
+ clone(): ColorType;
26
+ getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
27
+ formatChannelValue(channel: ColorChannel, locale: string): string;
28
+ getChannelRange(channel: ColorChannel): ColorChannelRange;
29
+ toJSON(): Record<"r" | "g" | "b" | "a", number>;
30
+ getFormat(): ColorFormat;
31
+ private static colorChannels;
32
+ getChannels(): [ColorChannel, ColorChannel, ColorChannel];
33
+ }