@oscarpalmer/atoms 0.188.0 → 0.189.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 (130) hide show
  1. package/dist/array/difference.d.mts +0 -1
  2. package/dist/array/exists.d.mts +0 -1
  3. package/dist/array/filter.d.mts +0 -1
  4. package/dist/array/find.d.mts +0 -1
  5. package/dist/array/first.d.mts +0 -1
  6. package/dist/array/flatten.d.mts +0 -1
  7. package/dist/array/get.d.mts +0 -1
  8. package/dist/array/group-by.d.mts +0 -1
  9. package/dist/array/last.d.mts +0 -1
  10. package/dist/array/match.d.mts +0 -1
  11. package/dist/array/match.mjs +3 -3
  12. package/dist/array/move.d.mts +0 -1
  13. package/dist/array/partition.d.mts +0 -1
  14. package/dist/array/select.d.mts +0 -1
  15. package/dist/array/select.mjs +2 -1
  16. package/dist/array/single.d.mts +0 -1
  17. package/dist/array/slice.d.mts +0 -1
  18. package/dist/array/sort.d.mts +1 -2
  19. package/dist/array/sort.mjs +0 -1
  20. package/dist/array/swap.d.mts +0 -1
  21. package/dist/array/to-map.d.mts +0 -1
  22. package/dist/array/to-record.d.mts +0 -1
  23. package/dist/array/to-set.d.mts +0 -1
  24. package/dist/array/toggle.d.mts +0 -1
  25. package/dist/array/unique.d.mts +0 -1
  26. package/dist/array/update.d.mts +0 -1
  27. package/dist/color/constants.d.mts +9 -10
  28. package/dist/color/constants.mjs +15 -7
  29. package/dist/color/index.d.mts +9 -9
  30. package/dist/color/index.mjs +8 -7
  31. package/dist/color/instance.d.mts +35 -8
  32. package/dist/color/instance.mjs +53 -13
  33. package/dist/color/misc/alpha.d.mts +0 -1
  34. package/dist/color/misc/alpha.mjs +5 -5
  35. package/dist/color/misc/get.d.mts +42 -13
  36. package/dist/color/misc/get.mjs +45 -24
  37. package/dist/color/misc/index.d.mts +4 -4
  38. package/dist/color/misc/index.mjs +12 -12
  39. package/dist/color/misc/is.d.mts +56 -3
  40. package/dist/color/misc/is.mjs +72 -4
  41. package/dist/color/misc/state.d.mts +2 -2
  42. package/dist/color/misc/state.mjs +79 -43
  43. package/dist/color/models.d.mts +18 -2
  44. package/dist/color/space/hex.d.mts +38 -5
  45. package/dist/color/space/hex.mjs +57 -9
  46. package/dist/color/space/hsl.d.mts +33 -7
  47. package/dist/color/space/hsl.mjs +65 -14
  48. package/dist/color/space/hwb.d.mts +52 -0
  49. package/dist/color/space/hwb.mjs +99 -0
  50. package/dist/color/space/rgb.d.mts +26 -5
  51. package/dist/color/space/rgb.mjs +82 -28
  52. package/dist/function/assert.d.mts +1 -2
  53. package/dist/function/limit.d.mts +0 -1
  54. package/dist/function/memoize.d.mts +0 -1
  55. package/dist/function/once.d.mts +0 -1
  56. package/dist/function/retry.d.mts +0 -1
  57. package/dist/function/work.d.mts +0 -1
  58. package/dist/herald.d.mts +0 -1
  59. package/dist/index.d.mts +296 -51
  60. package/dist/index.mjs +664 -265
  61. package/dist/internal/array/callbacks.d.mts +0 -1
  62. package/dist/internal/array/callbacks.mjs +0 -1
  63. package/dist/internal/array/group.d.mts +0 -1
  64. package/dist/internal/array/index-of.d.mts +0 -1
  65. package/dist/internal/function/timer.d.mts +0 -1
  66. package/dist/internal/is.d.mts +0 -1
  67. package/dist/internal/is.mjs +1 -1
  68. package/dist/internal/math/aggregate.d.mts +0 -1
  69. package/dist/internal/math/misc.d.mts +12 -0
  70. package/dist/internal/math/misc.mjs +19 -0
  71. package/dist/internal/result.d.mts +0 -1
  72. package/dist/internal/value/compare.d.mts +0 -1
  73. package/dist/internal/value/equal.d.mts +0 -1
  74. package/dist/internal/value/get.d.mts +0 -1
  75. package/dist/internal/value/handlers.d.mts +0 -1
  76. package/dist/internal/value/has.d.mts +0 -1
  77. package/dist/internal/value/misc.d.mts +0 -1
  78. package/dist/internal/value/set.d.mts +0 -1
  79. package/dist/is.d.mts +0 -1
  80. package/dist/math.d.mts +1 -9
  81. package/dist/math.mjs +1 -16
  82. package/dist/models.d.mts +6 -6
  83. package/dist/promise/delay.d.mts +0 -1
  84. package/dist/promise/helpers.d.mts +0 -1
  85. package/dist/promise/index.d.mts +0 -1
  86. package/dist/promise/misc.d.mts +0 -1
  87. package/dist/promise/models.d.mts +4 -5
  88. package/dist/promise/models.mjs +1 -1
  89. package/dist/promise/timed.d.mts +0 -1
  90. package/dist/query.d.mts +0 -1
  91. package/dist/query.mjs +1 -1
  92. package/dist/queue.d.mts +0 -1
  93. package/dist/random.d.mts +0 -1
  94. package/dist/result/index.d.mts +0 -1
  95. package/dist/result/index.mjs +2 -1
  96. package/dist/result/match.d.mts +0 -1
  97. package/dist/result/misc.d.mts +0 -1
  98. package/dist/result/models.d.mts +0 -1
  99. package/dist/result/work/flow.d.mts +0 -1
  100. package/dist/result/work/pipe.d.mts +0 -1
  101. package/dist/string/fuzzy.d.mts +0 -1
  102. package/dist/string/index.d.mts +0 -1
  103. package/dist/string/index.mjs +8 -7
  104. package/dist/string/template.d.mts +0 -1
  105. package/dist/value/clone.d.mts +0 -1
  106. package/dist/value/freeze.d.mts +1 -2
  107. package/dist/value/merge.d.mts +0 -1
  108. package/dist/value/omit.d.mts +0 -1
  109. package/dist/value/pick.d.mts +0 -1
  110. package/dist/value/shake.d.mts +1 -2
  111. package/dist/value/smush.d.mts +1 -2
  112. package/dist/value/transform.d.mts +1 -2
  113. package/dist/value/unsmush.d.mts +1 -2
  114. package/package.json +5 -5
  115. package/src/color/constants.ts +17 -13
  116. package/src/color/index.ts +21 -6
  117. package/src/color/instance.ts +67 -14
  118. package/src/color/misc/alpha.ts +6 -10
  119. package/src/color/misc/get.ts +70 -16
  120. package/src/color/misc/index.ts +18 -17
  121. package/src/color/misc/is.ts +86 -3
  122. package/src/color/misc/state.ts +126 -40
  123. package/src/color/models.ts +19 -1
  124. package/src/color/space/hex.ts +55 -8
  125. package/src/color/space/hsl.ts +80 -14
  126. package/src/color/space/hwb.ts +123 -0
  127. package/src/color/space/rgb.ts +108 -27
  128. package/src/internal/math/misc.ts +28 -0
  129. package/src/math.ts +2 -29
  130. package/src/string/index.ts +13 -11
@@ -1,11 +1,20 @@
1
- import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "../models.mjs";
1
+ import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
2
2
  import { Color } from "../instance.mjs";
3
-
4
3
  //#region src/color/misc/get.d.ts
4
+ /**
5
+ * Get a foreground color _(usually text)_ based on a background color's luminance as an unprefixed hex color string
6
+ *
7
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
8
+ * - If the value cannot be parsed, a white foreground color will be returned
9
+ *
10
+ * @param value Original value
11
+ * @returns Foreground color
12
+ */
13
+ declare function getForegroundColor(value: unknown, hex: true): string;
5
14
  /**
6
15
  * Get a foreground color _(usually text)_ based on a background color's luminance
7
16
  *
8
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
17
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
9
18
  * - If the value cannot be parsed, a white foreground color will be returned
10
19
  *
11
20
  * @param value Original value
@@ -15,8 +24,8 @@ declare function getForegroundColor(value: unknown): Color;
15
24
  /**
16
25
  * Get the hex color _(with alpha channel, i.e., opacity)_ from any kind of value
17
26
  *
18
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
19
- * - If the value cannot be parsed, a black hex color with an alpha channel of `0` will be returned
27
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
28
+ * - If the value cannot be parsed, a black hex color will be returned
20
29
  *
21
30
  * @param value Original value
22
31
  * @returns Hex color string
@@ -25,7 +34,7 @@ declare function getHexaColor(value: unknown): string;
25
34
  /**
26
35
  * Get the hex color from any kind of value
27
36
  *
28
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
37
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
29
38
  * - If the value cannot be parsed, a black hex color will be returned
30
39
  *
31
40
  * @param value Original value
@@ -37,17 +46,37 @@ declare function getDegrees(value: unknown): number;
37
46
  /**
38
47
  * Get the _HSLA_ color from any kind of value
39
48
  *
40
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
41
- * - If the value cannot be parsed, a black _HSLA_ color with an alpha channel _(opacity)_ of `0` will be returned
49
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
50
+ * - If the value cannot be parsed, a black _HSLA_ color will be returned
42
51
  *
43
52
  * @param value Original value
44
53
  * @returns _HSLA_ color
45
54
  */
46
55
  declare function getHslaColor(value: unknown): HSLAColor;
56
+ /**
57
+ * Get the _HWB_ color from any kind of value
58
+ *
59
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
60
+ * - If the value cannot be parsed, a black _HWB_ color will be returned
61
+ *
62
+ * @param value Original value
63
+ * @returns _HWB_ color
64
+ */
65
+ declare function getHwbColor(value: unknown): HWBColor;
66
+ /**
67
+ * Get the _HWBA_ color from any kind of value
68
+ *
69
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
70
+ * - If the value cannot be parsed, a black _HWBA_ color will be returned
71
+ *
72
+ * @param value Original value
73
+ * @returns _HWBA_ color
74
+ */
75
+ declare function getHwbaColor(value: unknown): HWBAColor;
47
76
  /**
48
77
  * Get the _HSL_ color from any kind of value
49
78
  *
50
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
79
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
51
80
  * - If the value cannot be parsed, a black _HSL_ color will be returned
52
81
  *
53
82
  * @param value Original value
@@ -58,8 +87,8 @@ declare function getPercentage(value: unknown): number;
58
87
  /**
59
88
  * Get the _RGBA_ color from any kind of value
60
89
  *
61
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
62
- * - If the value cannot be parsed, a black _RGBA_ color with an alpha channel _(opacity)_ of `0` will be returned
90
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
91
+ * - If the value cannot be parsed, a black _RGBA_ color will be returned
63
92
  *
64
93
  * @param value Original value
65
94
  * @returns _RGBA_ color
@@ -68,7 +97,7 @@ declare function getRgbaColor(value: unknown): RGBAColor;
68
97
  /**
69
98
  * Get the _RGB_ color from any kind of value
70
99
  *
71
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
100
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
72
101
  * - If the value cannot be parsed, a black _RGB_ color will be returned
73
102
  *
74
103
  * @param value Original value
@@ -76,4 +105,4 @@ declare function getRgbaColor(value: unknown): RGBAColor;
76
105
  */
77
106
  declare function getRgbColor(value: unknown): RGBColor;
78
107
  //#endregion
79
- export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getPercentage, getRgbColor, getRgbaColor };
108
+ export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getPercentage, getRgbColor, getRgbaColor };
@@ -1,21 +1,13 @@
1
1
  import { clamp } from "../../internal/number.mjs";
2
+ import { round } from "../../internal/math/misc.mjs";
2
3
  import { HEX_BLACK, HEX_WHITE, SRGB_LUMINANCE_EXPONENT, SRGB_LUMINANCE_MODIFIER, SRGB_LUMINANCE_MULTIPLIER, SRGB_LUMINANCE_OFFSET } from "../constants.mjs";
3
4
  import { getColorState } from "./state.mjs";
4
5
  import { Color } from "../instance.mjs";
5
6
  //#region src/color/misc/get.ts
6
- function getClampedValue(value, minimum, maximum) {
7
- return typeof value === "number" ? clamp(value, minimum, maximum) : minimum;
7
+ function getClampedValue(value, minimum, maximum, rounding) {
8
+ return typeof value === "number" && !Number.isNaN(value) ? clamp(rounding ?? false ? round(value) : value, minimum, maximum) : minimum;
8
9
  }
9
- /**
10
- * Get a foreground color _(usually text)_ based on a background color's luminance
11
- *
12
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
13
- * - If the value cannot be parsed, a white foreground color will be returned
14
- *
15
- * @param value Original value
16
- * @returns Foreground color
17
- */
18
- function getForegroundColor(value) {
10
+ function getForegroundColor(value, hex) {
19
11
  const { blue, green, red } = getColorState(value).rgb;
20
12
  const values = [
21
13
  blue / 255,
@@ -28,13 +20,14 @@ function getForegroundColor(value) {
28
20
  if (color <= .03928) values[index] /= SRGB_LUMINANCE_MULTIPLIER;
29
21
  else values[index] = ((color + SRGB_LUMINANCE_OFFSET) / SRGB_LUMINANCE_MODIFIER) ** SRGB_LUMINANCE_EXPONENT;
30
22
  }
31
- return new Color(.2126 * values[2] + .7152 * values[1] + .0722 * values[0] > .625 ? HEX_BLACK : HEX_WHITE);
23
+ const color = .2126 * values[2] + .7152 * values[1] + .0722 * values[0] > .625 ? HEX_BLACK : HEX_WHITE;
24
+ return hex === true ? color : new Color(color);
32
25
  }
33
26
  /**
34
27
  * Get the hex color _(with alpha channel, i.e., opacity)_ from any kind of value
35
28
  *
36
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
37
- * - If the value cannot be parsed, a black hex color with an alpha channel of `0` will be returned
29
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
30
+ * - If the value cannot be parsed, a black hex color will be returned
38
31
  *
39
32
  * @param value Original value
40
33
  * @returns Hex color string
@@ -46,7 +39,7 @@ function getHexaColor(value) {
46
39
  /**
47
40
  * Get the hex color from any kind of value
48
41
  *
49
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
42
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
50
43
  * - If the value cannot be parsed, a black hex color will be returned
51
44
  *
52
45
  * @param value Original value
@@ -56,7 +49,7 @@ function getHexColor(value) {
56
49
  return getColorState(value).hex;
57
50
  }
58
51
  function getHexValue(value) {
59
- return getClampedValue(value, 0, 255);
52
+ return getClampedValue(value, 0, 255, true);
60
53
  }
61
54
  function getDegrees(value) {
62
55
  return getClampedValue(value, 0, 360);
@@ -64,8 +57,8 @@ function getDegrees(value) {
64
57
  /**
65
58
  * Get the _HSLA_ color from any kind of value
66
59
  *
67
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
68
- * - If the value cannot be parsed, a black _HSLA_ color with an alpha channel _(opacity)_ of `0` will be returned
60
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
61
+ * - If the value cannot be parsed, a black _HSLA_ color will be returned
69
62
  *
70
63
  * @param value Original value
71
64
  * @returns _HSLA_ color
@@ -78,9 +71,37 @@ function getHslaColor(value) {
78
71
  };
79
72
  }
80
73
  /**
74
+ * Get the _HWB_ color from any kind of value
75
+ *
76
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
77
+ * - If the value cannot be parsed, a black _HWB_ color will be returned
78
+ *
79
+ * @param value Original value
80
+ * @returns _HWB_ color
81
+ */
82
+ function getHwbColor(value) {
83
+ return getColorState(value).hwb;
84
+ }
85
+ /**
86
+ * Get the _HWBA_ color from any kind of value
87
+ *
88
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
89
+ * - If the value cannot be parsed, a black _HWBA_ color will be returned
90
+ *
91
+ * @param value Original value
92
+ * @returns _HWBA_ color
93
+ */
94
+ function getHwbaColor(value) {
95
+ const { alpha, hwb } = getColorState(value);
96
+ return {
97
+ ...hwb,
98
+ alpha: alpha.value
99
+ };
100
+ }
101
+ /**
81
102
  * Get the _HSL_ color from any kind of value
82
103
  *
83
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
104
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
84
105
  * - If the value cannot be parsed, a black _HSL_ color will be returned
85
106
  *
86
107
  * @param value Original value
@@ -95,8 +116,8 @@ function getPercentage(value) {
95
116
  /**
96
117
  * Get the _RGBA_ color from any kind of value
97
118
  *
98
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
99
- * - If the value cannot be parsed, a black _RGBA_ color with an alpha channel _(opacity)_ of `0` will be returned
119
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
120
+ * - If the value cannot be parsed, a black _RGBA_ color will be returned
100
121
  *
101
122
  * @param value Original value
102
123
  * @returns _RGBA_ color
@@ -111,7 +132,7 @@ function getRgbaColor(value) {
111
132
  /**
112
133
  * Get the _RGB_ color from any kind of value
113
134
  *
114
- * - Values that can be parsed are: hex(a) color strings, _HSL(A)_ color objects, and _RGB(A)_ color objects
135
+ * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
115
136
  * - If the value cannot be parsed, a black _RGB_ color will be returned
116
137
  *
117
138
  * @param value Original value
@@ -121,4 +142,4 @@ function getRgbColor(value) {
121
142
  return getColorState(value).rgb;
122
143
  }
123
144
  //#endregion
124
- export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getPercentage, getRgbColor, getRgbaColor };
145
+ export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getPercentage, getRgbColor, getRgbaColor };
@@ -1,7 +1,7 @@
1
- import { ColorSpace } from "../models.mjs";
2
1
  import { Color } from "../instance.mjs";
3
-
4
2
  //#region src/color/misc/index.d.ts
5
- declare function formatColor(space: ColorSpace, color: Color, alpha: boolean): string;
3
+ declare function formatHslColor(color: Color, alpha: boolean): string;
4
+ declare function formatHwbColor(color: Color, alpha: boolean): string;
5
+ declare function formatRgbColor(color: Color, alpha: boolean): string;
6
6
  //#endregion
7
- export { formatColor };
7
+ export { formatHslColor, formatHwbColor, formatRgbColor };
@@ -1,15 +1,15 @@
1
- import { join } from "../../internal/string.mjs";
2
- import { KEYS_HSL, KEYS_RGB } from "../constants.mjs";
3
1
  //#region src/color/misc/index.ts
4
- function formatColor(space, color, alpha) {
5
- const suffix = alpha ? ` / ${color.alpha}` : "";
6
- const value = color[space];
7
- return `${space}(${join(keys[space].map((key) => value[key]), SPACE)}${suffix})`;
2
+ function formatHslColor(color, alpha) {
3
+ const { hue, lightness, saturation } = color.hsl;
4
+ return `hsl(${hue}deg ${saturation}% ${lightness}%${alpha ? ` / ${color.alpha}%` : ""})`;
5
+ }
6
+ function formatHwbColor(color, alpha) {
7
+ const { blackness, hue, whiteness } = color.hwb;
8
+ return `hwb(${hue}deg ${whiteness}% ${blackness}%${alpha ? ` / ${color.alpha}%` : ""})`;
9
+ }
10
+ function formatRgbColor(color, alpha) {
11
+ const { blue, green, red } = color.rgb;
12
+ return `rgb(${red} ${green} ${blue}${alpha ? ` / ${color.alpha}%` : ""})`;
8
13
  }
9
- const SPACE = " ";
10
- const keys = {
11
- hsl: KEYS_HSL,
12
- rgb: KEYS_RGB
13
- };
14
14
  //#endregion
15
- export { formatColor };
15
+ export { formatHslColor, formatHwbColor, formatRgbColor };
@@ -1,6 +1,5 @@
1
- import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "../models.mjs";
1
+ import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
2
2
  import { Color } from "../instance.mjs";
3
-
4
3
  //#region src/color/misc/is.d.ts
5
4
  /**
6
5
  * Is the value a _Color_?
@@ -33,6 +32,13 @@ declare function isHexColor(value: unknown, alpha?: boolean): value is string;
33
32
  * @returns `true` if the value is an _HSLA_ color, otherwise `false`
34
33
  */
35
34
  declare function isHslaColor(value: unknown): value is HSLAColor;
35
+ /**
36
+ * Is the value like an _HSLA_ color?
37
+ *
38
+ * @param value Value to check
39
+ * @returns `true` if the value is like an _HSLA_ color, otherwise `false`
40
+ */
41
+ declare function isHslaLike(value: unknown): value is Record<keyof HSLAColor, unknown>;
36
42
  /**
37
43
  * Is the value an _HSL_ color?
38
44
  *
@@ -40,7 +46,41 @@ declare function isHslaColor(value: unknown): value is HSLAColor;
40
46
  * @returns `true` if the value is an _HSL_ color, otherwise `false`
41
47
  */
42
48
  declare function isHslColor(value: unknown): value is HSLColor;
49
+ /**
50
+ * Is the value like an _HSL_ color?
51
+ *
52
+ * @param value Value to check
53
+ * @returns `true` if the value is like an _HSL_ color, otherwise `false`
54
+ */
43
55
  declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unknown>;
56
+ /**
57
+ * Is the value an _HWBA_ color?
58
+ *
59
+ * @param value Value to check
60
+ * @returns `true` if the value is an _HWBA_ color, otherwise `false`
61
+ */
62
+ declare function isHwbaColor(value: unknown): value is HWBAColor;
63
+ /**
64
+ * Is the value like an _HWBA_ color?
65
+ *
66
+ * @param value Value to check
67
+ * @returns `true` if the value is like an _HWBA_ color, otherwise `false`
68
+ */
69
+ declare function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, unknown>;
70
+ /**
71
+ * Is the value an _HWB_ color?
72
+ *
73
+ * @param value Value to check
74
+ * @returns `true` if the value is an _HWB_ color, otherwise `false`
75
+ */
76
+ declare function isHwbColor(value: unknown): value is HWBColor;
77
+ /**
78
+ * Is the value like an _HWB_ color?
79
+ *
80
+ * @param value Value to check
81
+ * @returns `true` if the value is like an _HWB_ color, otherwise `false`
82
+ */
83
+ declare function isHwbLike(value: unknown): value is Record<keyof HWBColor, unknown>;
44
84
  /**
45
85
  * Is the value an _RGBA_ color?
46
86
  *
@@ -48,6 +88,13 @@ declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unkn
48
88
  * @returns `true` if the value is an _RGBA_ color, otherwise `false`
49
89
  */
50
90
  declare function isRgbaColor(value: unknown): value is RGBAColor;
91
+ /**
92
+ * Is the value like an _RGBA_ color?
93
+ *
94
+ * @param value Value to check
95
+ * @returns `true` if the value is like an _RGBA_ color, otherwise `false`
96
+ */
97
+ declare function isRgbaLike(value: unknown): value is Record<keyof RGBAColor, unknown>;
51
98
  /**
52
99
  * Is the value an _RGB_ color?
53
100
  *
@@ -55,6 +102,12 @@ declare function isRgbaColor(value: unknown): value is RGBAColor;
55
102
  * @returns `true` if the value is an _RGB_ color, otherwise `false`
56
103
  */
57
104
  declare function isRgbColor(value: unknown): value is RGBColor;
105
+ /**
106
+ * Is the value like an _RGB_ color?
107
+ *
108
+ * @param value Value to check
109
+ * @returns `true` if the value is like an _RGB_ color, otherwise `false`
110
+ */
58
111
  declare function isRgbLike(value: unknown): value is Record<keyof RGBColor, unknown>;
59
112
  //#endregion
60
- export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isRgbColor, isRgbLike, isRgbaColor };
113
+ export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
@@ -1,11 +1,11 @@
1
1
  import { between } from "../../internal/number.mjs";
2
- import { EXPRESSION_HEX_LONG, EXPRESSION_HEX_SHORT, KEYS_HSL, KEYS_HSLA, KEYS_RGB, KEYS_RGBA } from "../constants.mjs";
2
+ import { EXPRESSION_HEX_LONG, EXPRESSION_HEX_SHORT, KEYS_HSL, KEYS_HSLA, KEYS_HWB, KEYS_HWBA, KEYS_RGB, KEYS_RGBA } from "../constants.mjs";
3
3
  //#region src/color/misc/is.ts
4
4
  function hasKeys(value, keys) {
5
5
  return typeof value === "object" && value !== null && keys.every((key) => key in value);
6
6
  }
7
7
  function isAlpha(value) {
8
- return typeof value === "number" && between(value, 0, 1);
8
+ return typeof value === "number" && between(value, 0, 100);
9
9
  }
10
10
  function isBytey(value) {
11
11
  return typeof value === "number" && between(value, 0, 255);
@@ -65,6 +65,15 @@ function isHslaColor(value) {
65
65
  return isColorValue(value, KEYS_HSLA);
66
66
  }
67
67
  /**
68
+ * Is the value like an _HSLA_ color?
69
+ *
70
+ * @param value Value to check
71
+ * @returns `true` if the value is like an _HSLA_ color, otherwise `false`
72
+ */
73
+ function isHslaLike(value) {
74
+ return hasKeys(value, KEYS_HSLA);
75
+ }
76
+ /**
68
77
  * Is the value an _HSL_ color?
69
78
  *
70
79
  * @param value Value to check
@@ -73,10 +82,52 @@ function isHslaColor(value) {
73
82
  function isHslColor(value) {
74
83
  return isColorValue(value, KEYS_HSLA) || isColorValue(value, KEYS_HSL);
75
84
  }
85
+ /**
86
+ * Is the value like an _HSL_ color?
87
+ *
88
+ * @param value Value to check
89
+ * @returns `true` if the value is like an _HSL_ color, otherwise `false`
90
+ */
76
91
  function isHslLike(value) {
77
92
  return hasKeys(value, KEYS_HSL);
78
93
  }
79
94
  /**
95
+ * Is the value an _HWBA_ color?
96
+ *
97
+ * @param value Value to check
98
+ * @returns `true` if the value is an _HWBA_ color, otherwise `false`
99
+ */
100
+ function isHwbaColor(value) {
101
+ return isColorValue(value, KEYS_HWBA);
102
+ }
103
+ /**
104
+ * Is the value like an _HWBA_ color?
105
+ *
106
+ * @param value Value to check
107
+ * @returns `true` if the value is like an _HWBA_ color, otherwise `false`
108
+ */
109
+ function isHwbaLike(value) {
110
+ return hasKeys(value, KEYS_HWBA);
111
+ }
112
+ /**
113
+ * Is the value an _HWB_ color?
114
+ *
115
+ * @param value Value to check
116
+ * @returns `true` if the value is an _HWB_ color, otherwise `false`
117
+ */
118
+ function isHwbColor(value) {
119
+ return isColorValue(value, KEYS_HWB) || isColorValue(value, KEYS_HWBA);
120
+ }
121
+ /**
122
+ * Is the value like an _HWB_ color?
123
+ *
124
+ * @param value Value to check
125
+ * @returns `true` if the value is like an _HWB_ color, otherwise `false`
126
+ */
127
+ function isHwbLike(value) {
128
+ return hasKeys(value, KEYS_HWB);
129
+ }
130
+ /**
80
131
  * Is the value an _RGBA_ color?
81
132
  *
82
133
  * @param value Value to check
@@ -86,6 +137,15 @@ function isRgbaColor(value) {
86
137
  return isColorValue(value, KEYS_RGBA);
87
138
  }
88
139
  /**
140
+ * Is the value like an _RGBA_ color?
141
+ *
142
+ * @param value Value to check
143
+ * @returns `true` if the value is like an _RGBA_ color, otherwise `false`
144
+ */
145
+ function isRgbaLike(value) {
146
+ return hasKeys(value, KEYS_RGBA);
147
+ }
148
+ /**
89
149
  * Is the value an _RGB_ color?
90
150
  *
91
151
  * @param value Value to check
@@ -94,6 +154,12 @@ function isRgbaColor(value) {
94
154
  function isRgbColor(value) {
95
155
  return isColorValue(value, KEYS_RGBA) || isColorValue(value, KEYS_RGB);
96
156
  }
157
+ /**
158
+ * Is the value like an _RGB_ color?
159
+ *
160
+ * @param value Value to check
161
+ * @returns `true` if the value is like an _RGB_ color, otherwise `false`
162
+ */
97
163
  function isRgbLike(value) {
98
164
  return hasKeys(value, KEYS_RGB);
99
165
  }
@@ -102,12 +168,14 @@ function isPercentage(value) {
102
168
  }
103
169
  const validators = {
104
170
  alpha: isAlpha,
171
+ blackness: isPercentage,
105
172
  blue: isBytey,
106
173
  green: isBytey,
107
174
  hue: isDegree,
108
175
  lightness: isPercentage,
109
176
  saturation: isPercentage,
110
- red: isBytey
177
+ red: isBytey,
178
+ whiteness: isPercentage
111
179
  };
112
180
  //#endregion
113
- export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isRgbColor, isRgbLike, isRgbaColor };
181
+ export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
@@ -1,9 +1,9 @@
1
1
  import { ColorState } from "../models.mjs";
2
-
3
2
  //#region src/color/misc/state.d.ts
4
3
  declare function getColorState(value: unknown): ColorState;
5
4
  declare function setHexColor(state: ColorState, value: string, alpha: boolean): void;
6
5
  declare function setHSLColor(state: ColorState, value: unknown, alpha: boolean): void;
6
+ declare function setHWBColor(state: ColorState, value: unknown, alpha: boolean): void;
7
7
  declare function setRGBColor(state: ColorState, value: unknown, alpha: boolean): void;
8
8
  //#endregion
9
- export { getColorState, setHSLColor, setHexColor, setRGBColor };
9
+ export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
@@ -1,72 +1,106 @@
1
- import { DEFAULT_HSL, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_RGB } from "../constants.mjs";
1
+ import { DEFAULT_HSL, DEFAULT_HWB, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_HWB, KEYS_RGB } from "../constants.mjs";
2
2
  import { getAlpha } from "./alpha.mjs";
3
- import { isColor, isHexColor, isHslLike, isRgbLike } from "./is.mjs";
4
3
  import { getDegrees, getHexValue, getPercentage } from "./get.mjs";
5
- import { getRgbValue, rgbToHex, rgbToHsl } from "../space/rgb.mjs";
6
- import { getNormalizedHex, hexToRgb } from "../space/hex.mjs";
7
- import { getHslValue, hslToRgb } from "../space/hsl.mjs";
4
+ import { isColor, isHexColor, isHslLike, isHwbLike, isRgbLike } from "./is.mjs";
5
+ import { getRgbValue, rgbToHex, rgbToHsl, rgbToHwb } from "../space/rgb.mjs";
6
+ import { getNormalizedHex, hexToHsl } from "../space/hex.mjs";
7
+ import { getHslValue, hslToHex, hslToHwb, hslToRgb } from "../space/hsl.mjs";
8
+ import { getHwbValue, hwbToHex, hwbToHsl, hwbToRgb } from "../space/hwb.mjs";
8
9
  //#region src/color/misc/state.ts
9
10
  function getColorState(value) {
10
- if (typeof value === "string") {
11
- const normalized = getNormalizedHex(value, true);
12
- const hex = normalized.slice(0, 6);
13
- const rgb = hexToRgb(hex);
14
- return {
15
- hex,
16
- rgb,
17
- alpha: getAlpha(normalized.slice(6)),
18
- hsl: rgbToHsl(rgb)
19
- };
20
- }
11
+ if (typeof value === "string") return getColorStateForHex(value);
21
12
  if (isColor(value)) return {
22
13
  hex: value.hex,
23
14
  hsl: value.hsl,
15
+ hwb: value.hwb,
24
16
  rgb: value.rgb,
25
17
  alpha: getAlpha(value.alpha)
26
18
  };
27
19
  const state = {};
28
20
  if (typeof value === "object" && value !== null) {
29
21
  state.alpha = getAlpha(value.alpha);
30
- if (KEYS_HSL.every((key) => key in value)) {
31
- state.hsl = getHslValue(value);
32
- state.rgb = hslToRgb(state.hsl);
33
- state.hex = rgbToHex(state.rgb);
34
- return state;
35
- }
36
- if (KEYS_RGB.every((key) => key in value)) {
37
- state.rgb = getRgbValue(value);
38
- state.hex = rgbToHex(state.rgb);
39
- state.hsl = rgbToHsl(state.rgb);
40
- return state;
41
- }
22
+ if (KEYS_HSL.every((key) => key in value)) return getColorStateForHsl(state, value);
23
+ if (KEYS_HWB.every((key) => key in value)) return getColorStateForHwb(state, value);
24
+ if (KEYS_RGB.every((key) => key in value)) return getColorStateForRgb(state, value);
42
25
  }
43
- state.alpha ??= getAlpha(1);
44
- state.hex ??= HEX_BLACK;
45
- state.hsl ??= { ...DEFAULT_HSL };
46
- state.rgb ??= { ...DEFAULT_RGB };
26
+ return getDefaultColorState(state);
27
+ }
28
+ function getColorStateForHex(value) {
29
+ const normalized = getNormalizedHex(value, true);
30
+ const hex = normalized.slice(0, 6);
31
+ const hsl = hexToHsl(hex);
32
+ return {
33
+ hex,
34
+ hsl,
35
+ alpha: getAlpha(normalized.slice(6)),
36
+ hwb: hslToHwb(hsl),
37
+ rgb: hslToRgb(hsl)
38
+ };
39
+ }
40
+ function getColorStateForHsl(state, value) {
41
+ state.hsl = getHslValue(value);
42
+ state.rgb = hslToRgb(state.hsl);
43
+ state.hex = hslToHex(state.hsl);
44
+ state.hwb = hslToHwb(state.hsl);
45
+ return state;
46
+ }
47
+ function getColorStateForHwb(state, value) {
48
+ state.hwb = getHwbValue(value);
49
+ state.hsl = hwbToHsl(state.hwb);
50
+ state.rgb = hwbToRgb(state.hwb);
51
+ state.hex = hwbToHex(state.hwb);
52
+ return state;
53
+ }
54
+ function getColorStateForRgb(state, value) {
55
+ state.rgb = getRgbValue(value);
56
+ state.hex = rgbToHex(state.rgb);
57
+ state.hsl = rgbToHsl(state.rgb);
58
+ state.hwb = rgbToHwb(state.rgb);
59
+ return state;
60
+ }
61
+ function getDefaultColorState(state) {
62
+ state.alpha = getAlpha(100);
63
+ state.hex = HEX_BLACK;
64
+ state.hsl = { ...DEFAULT_HSL };
65
+ state.hwb = { ...DEFAULT_HWB };
66
+ state.rgb = { ...DEFAULT_RGB };
47
67
  return state;
48
68
  }
49
69
  function setHexColor(state, value, alpha) {
50
70
  if (!isHexColor(value) || !alpha && value === state.hex) return;
51
71
  const normalized = getNormalizedHex(value, true);
52
72
  const hex = normalized.slice(0, 6);
53
- const rgb = hexToRgb(hex);
73
+ const hsl = hexToHsl(hex);
54
74
  state.hex = hex;
55
- state.hsl = rgbToHsl(rgb);
56
- state.rgb = rgb;
75
+ state.hsl = hsl;
76
+ state.rgb = hslToRgb(hsl);
77
+ state.hwb = hslToHwb(hsl);
57
78
  if (alpha) state.alpha = getAlpha(normalized.slice(6));
58
79
  }
59
80
  function setHSLColor(state, value, alpha) {
60
81
  if (!isHslLike(value)) return;
61
82
  const hsl = {
62
83
  hue: getDegrees(value.hue),
63
- lightness: getPercentage(value.lightness),
64
- saturation: getPercentage(value.saturation)
84
+ saturation: getPercentage(value.saturation),
85
+ lightness: getPercentage(value.lightness)
65
86
  };
66
- const rgb = hslToRgb(hsl);
67
- state.hex = rgbToHex(rgb);
68
87
  state.hsl = hsl;
69
- state.rgb = rgb;
88
+ state.hex = hslToHex(hsl);
89
+ state.hwb = hslToHwb(hsl);
90
+ state.rgb = hslToRgb(hsl);
91
+ if (alpha) state.alpha = getAlpha(value.alpha);
92
+ }
93
+ function setHWBColor(state, value, alpha) {
94
+ if (!isHwbLike(value)) return;
95
+ const hwb = {
96
+ hue: getDegrees(value.hue),
97
+ whiteness: getPercentage(value.whiteness),
98
+ blackness: getPercentage(value.blackness)
99
+ };
100
+ state.hwb = hwb;
101
+ state.hex = hwbToHex(hwb);
102
+ state.hsl = hwbToHsl(hwb);
103
+ state.rgb = hwbToRgb(hwb);
70
104
  if (alpha) state.alpha = getAlpha(value.alpha);
71
105
  }
72
106
  function setRGBColor(state, value, alpha) {
@@ -76,10 +110,12 @@ function setRGBColor(state, value, alpha) {
76
110
  green: getHexValue(value.green),
77
111
  red: getHexValue(value.red)
78
112
  };
79
- state.hex = rgbToHex(rgb);
80
- state.hsl = rgbToHsl(rgb);
113
+ const hsl = rgbToHsl(rgb);
114
+ state.hsl = hsl;
81
115
  state.rgb = rgb;
116
+ state.hex = hslToHex(hsl);
117
+ state.hwb = hslToHwb(hsl);
82
118
  if (alpha) state.alpha = getAlpha(value.alpha);
83
119
  }
84
120
  //#endregion
85
- export { getColorState, setHSLColor, setHexColor, setRGBColor };
121
+ export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };