@oscarpalmer/atoms 0.190.0 → 0.192.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 (57) hide show
  1. package/dist/beacon.d.mts +6 -44
  2. package/dist/beacon.mjs +20 -67
  3. package/dist/color/constants.d.mts +82 -35
  4. package/dist/color/constants.mjs +106 -61
  5. package/dist/color/index.d.mts +3 -3
  6. package/dist/color/index.mjs +2 -2
  7. package/dist/color/instance.mjs +60 -35
  8. package/dist/color/misc/alpha.d.mts +1 -1
  9. package/dist/color/misc/alpha.mjs +12 -10
  10. package/dist/color/misc/format.d.mts +7 -0
  11. package/dist/color/misc/format.mjs +20 -0
  12. package/dist/color/misc/get.mjs +25 -25
  13. package/dist/color/misc/is.d.mts +0 -36
  14. package/dist/color/misc/is.mjs +22 -57
  15. package/dist/color/misc/state.d.mts +8 -6
  16. package/dist/color/misc/state.mjs +79 -88
  17. package/dist/color/models.d.mts +106 -6
  18. package/dist/color/space/hex.d.mts +3 -2
  19. package/dist/color/space/hex.mjs +22 -10
  20. package/dist/color/space/hsl.d.mts +3 -2
  21. package/dist/color/space/hsl.mjs +26 -14
  22. package/dist/color/space/hwb.d.mts +3 -2
  23. package/dist/color/space/hwb.mjs +20 -8
  24. package/dist/color/space/rgb.d.mts +3 -2
  25. package/dist/color/space/rgb.mjs +27 -15
  26. package/dist/herald.d.mts +2 -3
  27. package/dist/index.d.mts +139 -73
  28. package/dist/index.mjs +653 -477
  29. package/dist/internal/subscription.d.mts +52 -0
  30. package/dist/internal/subscription.mjs +127 -0
  31. package/dist/models.d.mts +5 -1
  32. package/package.json +4 -4
  33. package/src/beacon.ts +32 -117
  34. package/src/color/constants.ts +115 -66
  35. package/src/color/index.ts +26 -8
  36. package/src/color/instance.ts +113 -45
  37. package/src/color/misc/alpha.ts +18 -20
  38. package/src/color/misc/format.ts +29 -0
  39. package/src/color/misc/get.ts +31 -41
  40. package/src/color/misc/is.ts +31 -71
  41. package/src/color/misc/state.ts +165 -141
  42. package/src/color/models.ts +126 -5
  43. package/src/color/space/hex.ts +46 -18
  44. package/src/color/space/hsl.ts +44 -14
  45. package/src/color/space/hwb.ts +38 -8
  46. package/src/color/space/rgb.ts +48 -15
  47. package/src/herald.ts +1 -3
  48. package/src/index.ts +1 -0
  49. package/src/internal/subscription.ts +264 -0
  50. package/src/models.ts +5 -0
  51. package/src/string/fuzzy.ts +1 -1
  52. package/src/value/freeze.ts +2 -2
  53. package/src/value/shake.ts +1 -1
  54. package/src/value/transform.ts +1 -1
  55. package/dist/color/misc/index.d.mts +0 -7
  56. package/dist/color/misc/index.mjs +0 -15
  57. package/src/color/misc/index.ts +0 -29
@@ -1,59 +1,84 @@
1
- import { formatHslColor, formatHwbColor, formatRgbColor } from "./misc/index.mjs";
1
+ import { clearSubscriptions, getSubscription, getSubscriptions } from "../internal/subscription.mjs";
2
+ import { COLOR_PROPERTY, COLOR_TYPE } from "./constants.mjs";
2
3
  import { getAlpha } from "./misc/alpha.mjs";
3
- import { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
4
+ import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
5
+ import { formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
4
6
  //#region src/color/instance.ts
5
7
  function color(value) {
6
8
  const state = getColorState(value);
9
+ const subscriptions = getSubscriptions(COLOR_TYPE.all);
7
10
  const instance = {
11
+ subscribe: (first, second) => subscribeToColor(subscriptions, first, second),
8
12
  toHexString: (alpha) => `#${alpha === true ? instance.hexa : instance.hex}`,
9
- toHslString: (alpha) => formatHslColor(state, alpha === true),
10
- toHwbString: (alpha) => formatHwbColor(state, alpha === true),
11
- toRgbString: (alpha) => formatRgbColor(state, alpha === true),
12
- toString: () => instance.toHexString()
13
+ toHslString: (alpha) => formatHslColor(state, alpha),
14
+ toHwbString: (alpha) => formatHwbColor(state, alpha),
15
+ toRgbString: (alpha) => formatRgbColor(state, alpha),
16
+ toString: () => instance.toHexString(),
17
+ unsubscribe: () => clearSubscriptions(subscriptions)
13
18
  };
14
19
  Object.defineProperties(instance, {
15
- $color: {
20
+ [COLOR_PROPERTY.name]: {
16
21
  enumerable: false,
17
- get: () => true
22
+ value: true
18
23
  },
19
24
  alpha: {
20
25
  enumerable: true,
21
26
  get: () => state.alpha.value,
22
- set: (value) => {
23
- if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value);
24
- }
27
+ set: (value) => setAlphaValue(state, value)
25
28
  },
26
- hex: getProperty("hex", state, false),
27
- hexa: getProperty("hex", state, true),
28
- hsl: getProperty("hsl", state, false),
29
- hsla: getProperty("hsl", state, true),
30
- hwb: getProperty("hwb", state, false),
31
- hwba: getProperty("hwb", state, true),
32
- rgb: getProperty("rgb", state, false),
33
- rgba: getProperty("rgb", state, true)
29
+ hex: getProperty(COLOR_TYPE.hex, instance, state, subscriptions, false),
30
+ hexa: getProperty(COLOR_TYPE.hex, instance, state, subscriptions, true),
31
+ hsl: getProperty(COLOR_TYPE.hsl, instance, state, subscriptions, false),
32
+ hsla: getProperty(COLOR_TYPE.hsl, instance, state, subscriptions, true),
33
+ hwb: getProperty(COLOR_TYPE.hwb, instance, state, subscriptions, false),
34
+ hwba: getProperty(COLOR_TYPE.hwb, instance, state, subscriptions, true),
35
+ origin: {
36
+ enumerable: true,
37
+ get: () => state.origin
38
+ },
39
+ rgb: getProperty(COLOR_TYPE.rgb, instance, state, subscriptions, false),
40
+ rgba: getProperty(COLOR_TYPE.rgb, instance, state, subscriptions, true)
34
41
  });
35
42
  return Object.freeze(instance);
36
43
  }
37
- function getHex(state, alpha) {
38
- return alpha ? `${state.hex}${state.alpha.hex}` : state.hex;
39
- }
40
- function getProperty(space, state, alpha) {
41
- const setter = setters[space];
44
+ function getProperty(space, color, state, subscriptions, alpha) {
45
+ const setValue = setters[space];
42
46
  return {
43
47
  enumerable: true,
44
- get: () => {
45
- const value = state[space];
46
- if (typeof value === "string") return getHex(state, alpha);
47
- return alpha ? {
48
- ...value,
49
- alpha: state.alpha.value
50
- } : { ...value };
51
- },
52
- set: (value) => {
53
- setter(state, value, alpha);
54
- }
48
+ get: () => getValue(space, state, alpha),
49
+ set: (value) => setValue(color, state, subscriptions, value, alpha)
55
50
  };
56
51
  }
52
+ function getValue(space, state, alpha) {
53
+ const value = getColorFromState(state, space);
54
+ if (typeof value === "string") return alpha ? `${value}${state.alpha.hex}` : value;
55
+ return alpha ? {
56
+ ...value,
57
+ alpha: state.alpha.value
58
+ } : { ...value };
59
+ }
60
+ function setAlphaValue(state, value) {
61
+ if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
62
+ }
63
+ function subscribeToColor(subscriptions, first, second) {
64
+ let key;
65
+ let value;
66
+ if (typeof first === "string") {
67
+ key = first;
68
+ value = second;
69
+ } else if (typeof first === "function") value = first;
70
+ if (typeof value !== "function") throw new Error();
71
+ return getSubscription({
72
+ key,
73
+ subscriptions,
74
+ value,
75
+ property: colorSubscription
76
+ });
77
+ }
78
+ const colorSubscription = {
79
+ key: COLOR_PROPERTY.name,
80
+ value: COLOR_PROPERTY.subscription
81
+ };
57
82
  const setters = {
58
83
  hex: setHexColor,
59
84
  hsl: setHSLColor,
@@ -1,6 +1,6 @@
1
1
  import { Alpha } from "../models.mjs";
2
2
  //#region src/color/misc/alpha.d.ts
3
- declare function getAlpha(value: unknown): Alpha;
3
+ declare function getAlpha(value: unknown, hex: boolean): Alpha;
4
4
  declare function getAlphaHexadecimal(value: number): string;
5
5
  declare function getAlphaValue(original: number): number;
6
6
  //#endregion
@@ -1,18 +1,20 @@
1
+ import { clamp } from "../../internal/number.mjs";
2
+ import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX } from "../constants.mjs";
1
3
  import { round } from "../../internal/math/misc.mjs";
2
- import { ALPHA_FULL_HEX_LONG, DEFAULT_ALPHA } from "../constants.mjs";
3
4
  //#region src/color/misc/alpha.ts
4
- function getAlpha(value) {
5
+ function getAlpha(value, hex) {
5
6
  if (typeof value === "number") return getAlphaFromValue(value);
6
- if (typeof value === "string" && value !== ALPHA_FULL_HEX_LONG) return {
7
+ if (typeof value !== "string" || value.toLowerCase() === COLOR_ALPHA.fullHexLong) return { ...COLOR_DEFAULTS.alpha };
8
+ if (hex && COLOR_EXPRESSION.alphaHex.test(value)) return {
7
9
  hex: value,
8
- value: Number.parseInt(value, 16) / 255
10
+ value: Number.parseInt(value, 16) / COLOR_MAX.hex
9
11
  };
10
- return { ...DEFAULT_ALPHA };
12
+ return getAlphaFromValue(Number.parseFloat(value));
11
13
  }
12
14
  function getAlphaHexadecimal(value) {
13
- if (value === 0) return "00";
14
- if (value === 100) return ALPHA_FULL_HEX_LONG;
15
- return round(value * 255).toString(16);
15
+ if (value === COLOR_ALPHA.noneValue) return COLOR_ALPHA.noneHex;
16
+ if (value === COLOR_MAX.percent) return COLOR_ALPHA.fullHexLong;
17
+ return round(value / COLOR_MAX.percent * COLOR_MAX.hex).toString(16);
16
18
  }
17
19
  function getAlphaFromValue(value) {
18
20
  const alpha = getAlphaValue(value);
@@ -22,8 +24,8 @@ function getAlphaFromValue(value) {
22
24
  };
23
25
  }
24
26
  function getAlphaValue(original) {
25
- if (Number.isNaN(original) || original >= 100) return 100;
26
- return original <= 0 ? 0 : original;
27
+ if (Number.isNaN(original)) return COLOR_MAX.percent;
28
+ return clamp(original, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
27
29
  }
28
30
  //#endregion
29
31
  export { getAlpha, getAlphaHexadecimal, getAlphaValue };
@@ -0,0 +1,7 @@
1
+ import { ColorState } from "../models.mjs";
2
+ //#region src/color/misc/format.d.ts
3
+ declare function formatHslColor(state: ColorState, alpha?: boolean): string;
4
+ declare function formatHwbColor(state: ColorState, alpha?: boolean): string;
5
+ declare function formatRgbColor(state: ColorState, alpha?: boolean): string;
6
+ //#endregion
7
+ export { formatHslColor, formatHwbColor, formatRgbColor };
@@ -0,0 +1,20 @@
1
+ import { COLOR_TYPE } from "../constants.mjs";
2
+ import { getColorFromState } from "./state.mjs";
3
+ //#region src/color/misc/format.ts
4
+ function formatHslColor(state, alpha) {
5
+ const { hue, lightness, saturation } = getColorFromState(state, COLOR_TYPE.hsl);
6
+ return `hsl(${hue}deg ${saturation}% ${lightness}%${getSuffix(state, alpha)})`;
7
+ }
8
+ function formatHwbColor(state, alpha) {
9
+ const { blackness, hue, whiteness } = getColorFromState(state, COLOR_TYPE.hwb);
10
+ return `hwb(${hue}deg ${whiteness}% ${blackness}%${getSuffix(state, alpha)})`;
11
+ }
12
+ function formatRgbColor(state, alpha) {
13
+ const { blue, green, red } = getColorFromState(state, COLOR_TYPE.rgb);
14
+ return `rgb(${red} ${green} ${blue}${getSuffix(state, alpha)})`;
15
+ }
16
+ function getSuffix(state, alpha) {
17
+ return alpha === true ? ` / ${state.alpha.value}%` : "";
18
+ }
19
+ //#endregion
20
+ export { formatHslColor, formatHwbColor, formatRgbColor };
@@ -1,26 +1,26 @@
1
1
  import { clamp } from "../../internal/number.mjs";
2
+ import { COLOR_DEFAULTS, COLOR_MAX, COLOR_SRGB, COLOR_TYPE } from "../constants.mjs";
2
3
  import { round } from "../../internal/math/misc.mjs";
3
- import { HEX_BLACK, HEX_WHITE, SRGB_LUMINANCE_EXPONENT, SRGB_LUMINANCE_MODIFIER, SRGB_LUMINANCE_MULTIPLIER, SRGB_LUMINANCE_OFFSET } from "../constants.mjs";
4
- import { getColorState } from "./state.mjs";
4
+ import { getColorFromState, getColorState } from "./state.mjs";
5
5
  import { color } from "../instance.mjs";
6
6
  //#region src/color/misc/get.ts
7
7
  function getClampedValue(value, minimum, maximum, rounding) {
8
8
  return typeof value === "number" && !Number.isNaN(value) ? clamp(rounding ?? false ? round(value) : value, minimum, maximum) : minimum;
9
9
  }
10
10
  function getForegroundColor(value, hex) {
11
- const { blue, green, red } = getColorState(value).rgb;
11
+ const { blue, green, red } = getColorFromState(getColorState(value), COLOR_TYPE.rgb);
12
12
  const values = [
13
- blue / 255,
14
- green / 255,
15
- red / 255
13
+ blue / COLOR_MAX.hex,
14
+ green / COLOR_MAX.hex,
15
+ red / COLOR_MAX.hex
16
16
  ];
17
17
  const { length } = values;
18
18
  for (let index = 0; index < length; index += 1) {
19
19
  const color = values[index];
20
- if (color <= .03928) values[index] /= SRGB_LUMINANCE_MULTIPLIER;
21
- else values[index] = ((color + SRGB_LUMINANCE_OFFSET) / SRGB_LUMINANCE_MODIFIER) ** SRGB_LUMINANCE_EXPONENT;
20
+ if (color <= COLOR_SRGB.luminanceMinimum) values[index] /= COLOR_SRGB.luminanceMultiplier;
21
+ else values[index] = ((color + COLOR_SRGB.luminanceOffset) / COLOR_SRGB.luminanceModifier) ** COLOR_SRGB.luminanceExponent;
22
22
  }
23
- const foreground = .2126 * values[2] + .7152 * values[1] + .0722 * values[0] > .625 ? HEX_BLACK : HEX_WHITE;
23
+ const foreground = COLOR_SRGB.luminanceRed * values[2] + COLOR_SRGB.luminanceGreen * values[1] + COLOR_SRGB.luminanceBlue * values[0] > COLOR_SRGB.luminanceThreshold ? COLOR_DEFAULTS.hexBlack : COLOR_DEFAULTS.hexWhite;
24
24
  return hex === true ? foreground : color(foreground);
25
25
  }
26
26
  /**
@@ -46,13 +46,13 @@ function getHexaColor(value) {
46
46
  * @returns Hex color string
47
47
  */
48
48
  function getHexColor(value) {
49
- return getColorState(value).hex;
49
+ return getColorFromState(getColorState(value), COLOR_TYPE.hex);
50
50
  }
51
51
  function getHexValue(value) {
52
- return getClampedValue(value, 0, 255, true);
52
+ return getClampedValue(value, 0, COLOR_MAX.hex, true);
53
53
  }
54
54
  function getDegrees(value) {
55
- return getClampedValue(value, 0, 360);
55
+ return getClampedValue(value, 0, COLOR_MAX.degree);
56
56
  }
57
57
  /**
58
58
  * Get the _HSLA_ color from any kind of value
@@ -64,10 +64,10 @@ function getDegrees(value) {
64
64
  * @returns _HSLA_ color
65
65
  */
66
66
  function getHslaColor(value) {
67
- const { alpha, hsl } = getColorState(value);
67
+ const state = getColorState(value);
68
68
  return {
69
- ...hsl,
70
- alpha: alpha.value
69
+ ...getColorFromState(state, COLOR_TYPE.hsl),
70
+ alpha: state.alpha.value
71
71
  };
72
72
  }
73
73
  /**
@@ -80,7 +80,7 @@ function getHslaColor(value) {
80
80
  * @returns _HWB_ color
81
81
  */
82
82
  function getHwbColor(value) {
83
- return getColorState(value).hwb;
83
+ return getColorFromState(getColorState(value), COLOR_TYPE.hwb);
84
84
  }
85
85
  /**
86
86
  * Get the _HWBA_ color from any kind of value
@@ -92,10 +92,10 @@ function getHwbColor(value) {
92
92
  * @returns _HWBA_ color
93
93
  */
94
94
  function getHwbaColor(value) {
95
- const { alpha, hwb } = getColorState(value);
95
+ const state = getColorState(value);
96
96
  return {
97
- ...hwb,
98
- alpha: alpha.value
97
+ ...getColorFromState(state, COLOR_TYPE.hwb),
98
+ alpha: state.alpha.value
99
99
  };
100
100
  }
101
101
  /**
@@ -108,10 +108,10 @@ function getHwbaColor(value) {
108
108
  * @returns _HSL_ color
109
109
  */
110
110
  function getHslColor(value) {
111
- return getColorState(value).hsl;
111
+ return getColorFromState(getColorState(value), COLOR_TYPE.hsl);
112
112
  }
113
113
  function getPercentage(value) {
114
- return getClampedValue(value, 0, 100);
114
+ return getClampedValue(value, 0, COLOR_MAX.percent);
115
115
  }
116
116
  /**
117
117
  * Get the _RGBA_ color from any kind of value
@@ -123,10 +123,10 @@ function getPercentage(value) {
123
123
  * @returns _RGBA_ color
124
124
  */
125
125
  function getRgbaColor(value) {
126
- const { alpha, rgb } = getColorState(value);
126
+ const state = getColorState(value);
127
127
  return {
128
- ...rgb,
129
- alpha: alpha.value
128
+ ...getColorFromState(state, COLOR_TYPE.rgb),
129
+ alpha: state.alpha.value
130
130
  };
131
131
  }
132
132
  /**
@@ -139,7 +139,7 @@ function getRgbaColor(value) {
139
139
  * @returns _RGB_ color
140
140
  */
141
141
  function getRgbColor(value) {
142
- return getColorState(value).rgb;
142
+ return getColorFromState(getColorState(value), COLOR_TYPE.rgb);
143
143
  }
144
144
  //#endregion
145
145
  export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getPercentage, getRgbColor, getRgbaColor };
@@ -31,12 +31,6 @@ declare function isHexColor(value: unknown, alpha?: boolean): value is string;
31
31
  * @returns `true` if the value is an _HSLA_ color, otherwise `false`
32
32
  */
33
33
  declare function isHslaColor(value: unknown): value is HSLAColor;
34
- /**
35
- * Is the value like an _HSLA_ color?
36
- *
37
- * @param value Value to check
38
- * @returns `true` if the value is like an _HSLA_ color, otherwise `false`
39
- */
40
34
  declare function isHslaLike(value: unknown): value is Record<keyof HSLAColor, unknown>;
41
35
  /**
42
36
  * Is the value an _HSL_ color?
@@ -45,12 +39,6 @@ declare function isHslaLike(value: unknown): value is Record<keyof HSLAColor, un
45
39
  * @returns `true` if the value is an _HSL_ color, otherwise `false`
46
40
  */
47
41
  declare function isHslColor(value: unknown): value is HSLColor;
48
- /**
49
- * Is the value like an _HSL_ color?
50
- *
51
- * @param value Value to check
52
- * @returns `true` if the value is like an _HSL_ color, otherwise `false`
53
- */
54
42
  declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unknown>;
55
43
  /**
56
44
  * Is the value an _HWBA_ color?
@@ -59,12 +47,6 @@ declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unkn
59
47
  * @returns `true` if the value is an _HWBA_ color, otherwise `false`
60
48
  */
61
49
  declare function isHwbaColor(value: unknown): value is HWBAColor;
62
- /**
63
- * Is the value like an _HWBA_ color?
64
- *
65
- * @param value Value to check
66
- * @returns `true` if the value is like an _HWBA_ color, otherwise `false`
67
- */
68
50
  declare function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, unknown>;
69
51
  /**
70
52
  * Is the value an _HWB_ color?
@@ -73,12 +55,6 @@ declare function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, un
73
55
  * @returns `true` if the value is an _HWB_ color, otherwise `false`
74
56
  */
75
57
  declare function isHwbColor(value: unknown): value is HWBColor;
76
- /**
77
- * Is the value like an _HWB_ color?
78
- *
79
- * @param value Value to check
80
- * @returns `true` if the value is like an _HWB_ color, otherwise `false`
81
- */
82
58
  declare function isHwbLike(value: unknown): value is Record<keyof HWBColor, unknown>;
83
59
  /**
84
60
  * Is the value an _RGBA_ color?
@@ -87,12 +63,6 @@ declare function isHwbLike(value: unknown): value is Record<keyof HWBColor, unkn
87
63
  * @returns `true` if the value is an _RGBA_ color, otherwise `false`
88
64
  */
89
65
  declare function isRgbaColor(value: unknown): value is RGBAColor;
90
- /**
91
- * Is the value like an _RGBA_ color?
92
- *
93
- * @param value Value to check
94
- * @returns `true` if the value is like an _RGBA_ color, otherwise `false`
95
- */
96
66
  declare function isRgbaLike(value: unknown): value is Record<keyof RGBAColor, unknown>;
97
67
  /**
98
68
  * Is the value an _RGB_ color?
@@ -101,12 +71,6 @@ declare function isRgbaLike(value: unknown): value is Record<keyof RGBAColor, un
101
71
  * @returns `true` if the value is an _RGB_ color, otherwise `false`
102
72
  */
103
73
  declare function isRgbColor(value: unknown): value is RGBColor;
104
- /**
105
- * Is the value like an _RGB_ color?
106
- *
107
- * @param value Value to check
108
- * @returns `true` if the value is like an _RGB_ color, otherwise `false`
109
- */
110
74
  declare function isRgbLike(value: unknown): value is Record<keyof RGBColor, unknown>;
111
75
  //#endregion
112
76
  export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
@@ -1,14 +1,15 @@
1
+ import { isPlainObject } from "../../internal/is.mjs";
1
2
  import { between } from "../../internal/number.mjs";
2
- import { EXPRESSION_HEX_LONG, EXPRESSION_HEX_SHORT, EXPRESSION_PREFIX, KEYS_HSL, KEYS_HSLA, KEYS_HWB, KEYS_HWBA, KEYS_RGB, KEYS_RGBA } from "../constants.mjs";
3
+ import { COLOR_ALPHA, COLOR_EXPRESSION, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_PROPERTY } from "../constants.mjs";
3
4
  //#region src/color/misc/is.ts
4
5
  function hasKeys(value, keys) {
5
6
  return typeof value === "object" && value !== null && keys.every((key) => key in value);
6
7
  }
7
8
  function isAlpha(value) {
8
- return typeof value === "number" && between(value, 0, 100);
9
+ return typeof value === "number" && between(value, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
9
10
  }
10
11
  function isBytey(value) {
11
- return typeof value === "number" && between(value, 0, 255);
12
+ return typeof value === "number" && between(value, 0, COLOR_MAX.hex);
12
13
  }
13
14
  /**
14
15
  * Is the value a _Color_?
@@ -17,7 +18,7 @@ function isBytey(value) {
17
18
  * @returns `true` if the value is a _Color_, otherwise `false`
18
19
  */
19
20
  function isColor(value) {
20
- return typeof value === "object" && value !== null && "$color" in value && value.$color === true;
21
+ return isPlainObject(value) && COLOR_PROPERTY.name in value && value[COLOR_PROPERTY.name] === true;
21
22
  }
22
23
  function isColorValue(obj, properties) {
23
24
  if (typeof obj !== "object" || obj === null) return false;
@@ -31,7 +32,7 @@ function isColorValue(obj, properties) {
31
32
  return true;
32
33
  }
33
34
  function isDegree(value) {
34
- return typeof value === "number" && between(value, 0, 360);
35
+ return typeof value === "number" && between(value, 0, COLOR_MAX.degree);
35
36
  }
36
37
  /**
37
38
  * Is the value a hex color?
@@ -51,10 +52,10 @@ function isDegree(value) {
51
52
  */
52
53
  function isHexColor(value, alpha) {
53
54
  if (typeof value !== "string") return false;
54
- if (!(EXPRESSION_HEX_SHORT.test(value) || EXPRESSION_HEX_LONG.test(value))) return false;
55
+ if (!(COLOR_EXPRESSION.hexShort.test(value) || COLOR_EXPRESSION.hexLong.test(value))) return false;
55
56
  if (alpha === false) {
56
- const unprefixed = value.replace(EXPRESSION_PREFIX, "");
57
- return unprefixed.length === 3 || unprefixed.length === 6;
57
+ const unprefixed = value.replace(COLOR_EXPRESSION.prefix, "");
58
+ return unprefixed.length === COLOR_LENGTHS.hexShort || unprefixed.length === COLOR_LENGTHS.hexLong;
58
59
  }
59
60
  return true;
60
61
  }
@@ -65,16 +66,10 @@ function isHexColor(value, alpha) {
65
66
  * @returns `true` if the value is an _HSLA_ color, otherwise `false`
66
67
  */
67
68
  function isHslaColor(value) {
68
- return isColorValue(value, KEYS_HSLA);
69
+ return isColorValue(value, COLOR_KEYS.hsla);
69
70
  }
70
- /**
71
- * Is the value like an _HSLA_ color?
72
- *
73
- * @param value Value to check
74
- * @returns `true` if the value is like an _HSLA_ color, otherwise `false`
75
- */
76
71
  function isHslaLike(value) {
77
- return hasKeys(value, KEYS_HSLA);
72
+ return hasKeys(value, COLOR_KEYS.hsla);
78
73
  }
79
74
  /**
80
75
  * Is the value an _HSL_ color?
@@ -83,16 +78,10 @@ function isHslaLike(value) {
83
78
  * @returns `true` if the value is an _HSL_ color, otherwise `false`
84
79
  */
85
80
  function isHslColor(value) {
86
- return isColorValue(value, KEYS_HSLA) || isColorValue(value, KEYS_HSL);
81
+ return isColorValue(value, COLOR_KEYS.hsla) || isColorValue(value, COLOR_KEYS.hsl);
87
82
  }
88
- /**
89
- * Is the value like an _HSL_ color?
90
- *
91
- * @param value Value to check
92
- * @returns `true` if the value is like an _HSL_ color, otherwise `false`
93
- */
94
83
  function isHslLike(value) {
95
- return hasKeys(value, KEYS_HSL);
84
+ return hasKeys(value, COLOR_KEYS.hsl);
96
85
  }
97
86
  /**
98
87
  * Is the value an _HWBA_ color?
@@ -101,16 +90,10 @@ function isHslLike(value) {
101
90
  * @returns `true` if the value is an _HWBA_ color, otherwise `false`
102
91
  */
103
92
  function isHwbaColor(value) {
104
- return isColorValue(value, KEYS_HWBA);
93
+ return isColorValue(value, COLOR_KEYS.hwba);
105
94
  }
106
- /**
107
- * Is the value like an _HWBA_ color?
108
- *
109
- * @param value Value to check
110
- * @returns `true` if the value is like an _HWBA_ color, otherwise `false`
111
- */
112
95
  function isHwbaLike(value) {
113
- return hasKeys(value, KEYS_HWBA);
96
+ return hasKeys(value, COLOR_KEYS.hwba);
114
97
  }
115
98
  /**
116
99
  * Is the value an _HWB_ color?
@@ -119,16 +102,10 @@ function isHwbaLike(value) {
119
102
  * @returns `true` if the value is an _HWB_ color, otherwise `false`
120
103
  */
121
104
  function isHwbColor(value) {
122
- return isColorValue(value, KEYS_HWB) || isColorValue(value, KEYS_HWBA);
105
+ return isColorValue(value, COLOR_KEYS.hwb) || isColorValue(value, COLOR_KEYS.hwba);
123
106
  }
124
- /**
125
- * Is the value like an _HWB_ color?
126
- *
127
- * @param value Value to check
128
- * @returns `true` if the value is like an _HWB_ color, otherwise `false`
129
- */
130
107
  function isHwbLike(value) {
131
- return hasKeys(value, KEYS_HWB);
108
+ return hasKeys(value, COLOR_KEYS.hwb);
132
109
  }
133
110
  /**
134
111
  * Is the value an _RGBA_ color?
@@ -137,16 +114,10 @@ function isHwbLike(value) {
137
114
  * @returns `true` if the value is an _RGBA_ color, otherwise `false`
138
115
  */
139
116
  function isRgbaColor(value) {
140
- return isColorValue(value, KEYS_RGBA);
117
+ return isColorValue(value, COLOR_KEYS.rgba);
141
118
  }
142
- /**
143
- * Is the value like an _RGBA_ color?
144
- *
145
- * @param value Value to check
146
- * @returns `true` if the value is like an _RGBA_ color, otherwise `false`
147
- */
148
119
  function isRgbaLike(value) {
149
- return hasKeys(value, KEYS_RGBA);
120
+ return hasKeys(value, COLOR_KEYS.rgba);
150
121
  }
151
122
  /**
152
123
  * Is the value an _RGB_ color?
@@ -155,19 +126,13 @@ function isRgbaLike(value) {
155
126
  * @returns `true` if the value is an _RGB_ color, otherwise `false`
156
127
  */
157
128
  function isRgbColor(value) {
158
- return isColorValue(value, KEYS_RGBA) || isColorValue(value, KEYS_RGB);
129
+ return isColorValue(value, COLOR_KEYS.rgba) || isColorValue(value, COLOR_KEYS.rgb);
159
130
  }
160
- /**
161
- * Is the value like an _RGB_ color?
162
- *
163
- * @param value Value to check
164
- * @returns `true` if the value is like an _RGB_ color, otherwise `false`
165
- */
166
131
  function isRgbLike(value) {
167
- return hasKeys(value, KEYS_RGB);
132
+ return hasKeys(value, COLOR_KEYS.rgb);
168
133
  }
169
134
  function isPercentage(value) {
170
- return typeof value === "number" && between(value, 0, 100);
135
+ return typeof value === "number" && between(value, 0, COLOR_MAX.percent);
171
136
  }
172
137
  const validators = {
173
138
  alpha: isAlpha,
@@ -1,9 +1,11 @@
1
- import { ColorState } from "../models.mjs";
1
+ import { Subscriptions } from "../../internal/subscription.mjs";
2
+ import { Color, ColorState, ColorType } from "../models.mjs";
2
3
  //#region src/color/misc/state.d.ts
4
+ declare function getColorFromState<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
3
5
  declare function getColorState(value: unknown): ColorState;
4
- declare function setHexColor(state: ColorState, value: string, alpha: boolean): void;
5
- declare function setHSLColor(state: ColorState, value: unknown, alpha: boolean): void;
6
- declare function setHWBColor(state: ColorState, value: unknown, alpha: boolean): void;
7
- declare function setRGBColor(state: ColorState, value: unknown, alpha: boolean): void;
6
+ declare function setHexColor(color: Color, state: ColorState, subscriptions: Subscriptions<Function>, value: string, alpha: boolean): void;
7
+ declare function setHSLColor(color: Color, state: ColorState, subscriptions: Subscriptions<Function>, value: unknown, alpha: boolean): void;
8
+ declare function setHWBColor(color: Color, state: ColorState, subscriptions: Subscriptions<Function>, value: unknown, alpha: boolean): void;
9
+ declare function setRGBColor(color: Color, state: ColorState, subscriptions: Subscriptions<Function>, value: unknown, alpha: boolean): void;
8
10
  //#endregion
9
- export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
11
+ export { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };