@oscarpalmer/atoms 0.193.0 → 0.194.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 (204) hide show
  1. package/dist/array/difference.mjs +2 -2
  2. package/dist/array/intersection.mjs +2 -2
  3. package/dist/array/match.mjs +13 -13
  4. package/dist/array/slice.mjs +5 -5
  5. package/dist/array/sort.d.mts +5 -1
  6. package/dist/array/sort.mjs +4 -2
  7. package/dist/array/union.mjs +2 -2
  8. package/dist/beacon.d.mts +8 -1
  9. package/dist/beacon.mjs +52 -60
  10. package/dist/color/constants.d.mts +1 -1
  11. package/dist/color/constants.mjs +3 -5
  12. package/dist/color/instance.mjs +34 -41
  13. package/dist/color/misc/format.d.mts +3 -2
  14. package/dist/color/misc/format.mjs +4 -1
  15. package/dist/color/misc/is.d.mts +9 -1
  16. package/dist/color/misc/is.mjs +11 -1
  17. package/dist/color/misc/state.d.mts +6 -6
  18. package/dist/color/misc/state.mjs +11 -14
  19. package/dist/color/models.d.mts +23 -68
  20. package/dist/function/assert.mjs +2 -2
  21. package/dist/function/limit.d.mts +0 -6
  22. package/dist/function/limit.mjs +2 -2
  23. package/dist/function/memoize.mjs +19 -32
  24. package/dist/function/once.d.mts +0 -3
  25. package/dist/function/once.mjs +16 -13
  26. package/dist/function/retry.d.mts +1 -24
  27. package/dist/function/retry.mjs +1 -1
  28. package/dist/function/work.mjs +13 -11
  29. package/dist/herald.d.mts +30 -31
  30. package/dist/herald.mjs +74 -56
  31. package/dist/index.d.mts +100 -185
  32. package/dist/index.mjs +680 -702
  33. package/dist/internal/abort.d.mts +10 -0
  34. package/dist/internal/abort.mjs +14 -0
  35. package/dist/internal/array/chunk.mjs +2 -2
  36. package/dist/internal/array/find.mjs +2 -2
  37. package/dist/internal/array/sets.d.mts +4 -4
  38. package/dist/internal/array/sets.mjs +7 -7
  39. package/dist/internal/function/timer.mjs +19 -15
  40. package/dist/internal/number.mjs +7 -7
  41. package/dist/internal/result.mjs +3 -3
  42. package/dist/internal/sized.mjs +4 -4
  43. package/dist/internal/string.mjs +4 -4
  44. package/dist/internal/subscription.d.mts +24 -8
  45. package/dist/internal/subscription.mjs +49 -36
  46. package/dist/internal/value/equal.mjs +6 -6
  47. package/dist/internal/value/misc.mjs +14 -14
  48. package/dist/internal/value/set.mjs +2 -2
  49. package/dist/logger.mjs +15 -39
  50. package/dist/promise/constants.d.mts +17 -0
  51. package/dist/promise/constants.mjs +14 -0
  52. package/dist/promise/delay.mjs +7 -10
  53. package/dist/promise/helpers.mjs +1 -1
  54. package/dist/promise/index.mjs +14 -17
  55. package/dist/promise/misc.d.mts +2 -1
  56. package/dist/promise/misc.mjs +8 -7
  57. package/dist/promise/models.d.mts +4 -16
  58. package/dist/promise/models.mjs +4 -13
  59. package/dist/promise/timed.mjs +9 -10
  60. package/dist/query.mjs +15 -15
  61. package/dist/queue.mjs +103 -116
  62. package/dist/random.mjs +7 -7
  63. package/dist/result/index.d.mts +0 -3
  64. package/dist/result/index.mjs +1 -1
  65. package/dist/result/match.d.mts +0 -3
  66. package/dist/result/match.mjs +4 -4
  67. package/dist/result/misc.mjs +2 -2
  68. package/dist/result/work/flow.d.mts +0 -3
  69. package/dist/result/work/flow.mjs +5 -5
  70. package/dist/result/work/pipe.d.mts +0 -3
  71. package/dist/result/work/pipe.mjs +3 -3
  72. package/dist/string/case.mjs +20 -20
  73. package/dist/string/fuzzy.mjs +23 -18
  74. package/dist/string/index.mjs +7 -7
  75. package/dist/string/normalize.mjs +14 -14
  76. package/dist/string/template.mjs +2 -2
  77. package/dist/value/clone.mjs +10 -10
  78. package/dist/value/smush.mjs +2 -2
  79. package/package.json +13 -5
  80. package/src/array/difference.ts +0 -73
  81. package/src/array/exists.ts +0 -93
  82. package/src/array/filter.ts +0 -182
  83. package/src/array/find.ts +0 -183
  84. package/src/array/first.ts +0 -196
  85. package/src/array/flatten.ts +0 -20
  86. package/src/array/from.ts +0 -129
  87. package/src/array/get.ts +0 -131
  88. package/src/array/group-by.ts +0 -369
  89. package/src/array/index.ts +0 -26
  90. package/src/array/insert.ts +0 -52
  91. package/src/array/intersection.ts +0 -72
  92. package/src/array/last.ts +0 -194
  93. package/src/array/match.ts +0 -436
  94. package/src/array/move.ts +0 -315
  95. package/src/array/partition.ts +0 -91
  96. package/src/array/push.ts +0 -23
  97. package/src/array/reverse.ts +0 -33
  98. package/src/array/select.ts +0 -260
  99. package/src/array/single.ts +0 -93
  100. package/src/array/slice.ts +0 -335
  101. package/src/array/sort.ts +0 -607
  102. package/src/array/splice.ts +0 -106
  103. package/src/array/swap.ts +0 -322
  104. package/src/array/to-map.ts +0 -371
  105. package/src/array/to-record.ts +0 -351
  106. package/src/array/to-set.ts +0 -80
  107. package/src/array/toggle.ts +0 -79
  108. package/src/array/union.ts +0 -72
  109. package/src/array/unique.ts +0 -67
  110. package/src/array/update.ts +0 -75
  111. package/src/beacon.ts +0 -378
  112. package/src/color/constants.ts +0 -137
  113. package/src/color/index.ts +0 -83
  114. package/src/color/instance.ts +0 -162
  115. package/src/color/misc/alpha.ts +0 -56
  116. package/src/color/misc/format.ts +0 -29
  117. package/src/color/misc/get.ts +0 -210
  118. package/src/color/misc/is.ts +0 -221
  119. package/src/color/misc/state.ts +0 -243
  120. package/src/color/models.ts +0 -389
  121. package/src/color/space/hex.ts +0 -184
  122. package/src/color/space/hsl.ts +0 -174
  123. package/src/color/space/hwb.ts +0 -153
  124. package/src/color/space/rgb.ts +0 -240
  125. package/src/function/assert.ts +0 -174
  126. package/src/function/index.ts +0 -2
  127. package/src/function/limit.ts +0 -83
  128. package/src/function/memoize.ts +0 -202
  129. package/src/function/once.ts +0 -200
  130. package/src/function/retry.ts +0 -175
  131. package/src/function/work.ts +0 -1007
  132. package/src/herald.ts +0 -216
  133. package/src/index.ts +0 -68
  134. package/src/internal/array/callbacks.ts +0 -46
  135. package/src/internal/array/chunk.ts +0 -51
  136. package/src/internal/array/compact.ts +0 -56
  137. package/src/internal/array/find.ts +0 -200
  138. package/src/internal/array/group.ts +0 -42
  139. package/src/internal/array/index-of.ts +0 -182
  140. package/src/internal/array/insert.ts +0 -80
  141. package/src/internal/array/overlap.ts +0 -35
  142. package/src/internal/array/sets.ts +0 -74
  143. package/src/internal/array/shuffle.ts +0 -33
  144. package/src/internal/array/update.ts +0 -52
  145. package/src/internal/function/misc.ts +0 -8
  146. package/src/internal/function/timer.ts +0 -168
  147. package/src/internal/is.ts +0 -244
  148. package/src/internal/math/aggregate.ts +0 -164
  149. package/src/internal/math/misc.ts +0 -28
  150. package/src/internal/number.ts +0 -129
  151. package/src/internal/random.ts +0 -49
  152. package/src/internal/result.ts +0 -92
  153. package/src/internal/sized.ts +0 -25
  154. package/src/internal/string.ts +0 -128
  155. package/src/internal/subscription.ts +0 -264
  156. package/src/internal/value/compare.ts +0 -170
  157. package/src/internal/value/equal.ts +0 -465
  158. package/src/internal/value/get.ts +0 -57
  159. package/src/internal/value/handlers.ts +0 -95
  160. package/src/internal/value/has.ts +0 -118
  161. package/src/internal/value/misc.ts +0 -125
  162. package/src/internal/value/partial.ts +0 -46
  163. package/src/internal/value/set.ts +0 -132
  164. package/src/is.ts +0 -182
  165. package/src/logger.ts +0 -230
  166. package/src/math.ts +0 -281
  167. package/src/models.ts +0 -286
  168. package/src/number.ts +0 -1
  169. package/src/promise/delay.ts +0 -62
  170. package/src/promise/helpers.ts +0 -96
  171. package/src/promise/index.ts +0 -300
  172. package/src/promise/misc.ts +0 -96
  173. package/src/promise/models.ts +0 -195
  174. package/src/promise/timed.ts +0 -91
  175. package/src/query.ts +0 -147
  176. package/src/queue.ts +0 -814
  177. package/src/random.ts +0 -125
  178. package/src/result/index.ts +0 -105
  179. package/src/result/match.ts +0 -124
  180. package/src/result/misc.ts +0 -129
  181. package/src/result/models.ts +0 -83
  182. package/src/result/work/flow.ts +0 -488
  183. package/src/result/work/pipe.ts +0 -496
  184. package/src/sized/map.ts +0 -110
  185. package/src/sized/set.ts +0 -107
  186. package/src/string/case.ts +0 -236
  187. package/src/string/fuzzy.ts +0 -444
  188. package/src/string/index.ts +0 -199
  189. package/src/string/match.ts +0 -80
  190. package/src/string/normalize.ts +0 -205
  191. package/src/string/template.ts +0 -161
  192. package/src/value/clone.ts +0 -319
  193. package/src/value/collection.ts +0 -63
  194. package/src/value/diff.ts +0 -232
  195. package/src/value/freeze.ts +0 -322
  196. package/src/value/handle.ts +0 -3
  197. package/src/value/index.ts +0 -5
  198. package/src/value/merge.ts +0 -278
  199. package/src/value/omit.ts +0 -20
  200. package/src/value/pick.ts +0 -20
  201. package/src/value/shake.ts +0 -45
  202. package/src/value/smush.ts +0 -89
  203. package/src/value/transform.ts +0 -153
  204. package/src/value/unsmush.ts +0 -84
@@ -1,162 +0,0 @@
1
- import {
2
- clearSubscriptions,
3
- getSubscription,
4
- getSubscriptions,
5
- type Subscription,
6
- type SubscriptionProperty,
7
- type Subscriptions,
8
- } from '../internal/subscription';
9
- import {COLOR_PROPERTY, COLOR_TYPE} from './constants';
10
- import {getAlpha} from './misc/alpha';
11
- import {formatHslColor, formatHwbColor, formatRgbColor} from './misc/format';
12
- import {
13
- getColorFromState,
14
- getColorState,
15
- setHexColor,
16
- setHSLColor,
17
- setHWBColor,
18
- setRGBColor,
19
- } from './misc/state';
20
- import type {Color, ColorState, ColorType} from './models';
21
-
22
- // #region Types
23
-
24
- type SetValue = (
25
- color: Color,
26
- state: ColorState,
27
- subscriptions: Subscriptions<Function>,
28
- value: any,
29
- alpha: boolean,
30
- ) => void;
31
-
32
- type SetValues = Record<ColorType, SetValue>;
33
-
34
- // #endregion
35
-
36
- // #region Functions
37
-
38
- export function color(value: unknown): Color {
39
- const state = getColorState(value);
40
-
41
- const subscriptions = getSubscriptions<Function>(COLOR_TYPE.all);
42
-
43
- const instance = {
44
- subscribe: (first?: unknown, second?: unknown) =>
45
- subscribeToColor(subscriptions, first, second),
46
- toHexString: (alpha?: boolean) =>
47
- `#${alpha === true ? (instance as Color).hexa : (instance as Color).hex}`,
48
- toHslString: (alpha?: boolean) => formatHslColor(state, alpha),
49
- toHwbString: (alpha?: boolean) => formatHwbColor(state, alpha),
50
- toRgbString: (alpha?: boolean) => formatRgbColor(state, alpha),
51
- toString: () => (instance as Color).toHexString(),
52
- unsubscribe: () => clearSubscriptions(subscriptions),
53
- };
54
-
55
- Object.defineProperties(instance, {
56
- [COLOR_PROPERTY.name]: {
57
- enumerable: false,
58
- value: true,
59
- },
60
- alpha: {
61
- enumerable: true,
62
- get: () => state.alpha.value,
63
- set: (value: unknown) => setAlphaValue(state, value),
64
- },
65
- hex: getProperty(COLOR_TYPE.hex, instance as Color, state, subscriptions, false),
66
- hexa: getProperty(COLOR_TYPE.hex, instance as Color, state, subscriptions, true),
67
- hsl: getProperty(COLOR_TYPE.hsl, instance as Color, state, subscriptions, false),
68
- hsla: getProperty(COLOR_TYPE.hsl, instance as Color, state, subscriptions, true),
69
- hwb: getProperty(COLOR_TYPE.hwb, instance as Color, state, subscriptions, false),
70
- hwba: getProperty(COLOR_TYPE.hwb, instance as Color, state, subscriptions, true),
71
- origin: {
72
- enumerable: true,
73
- get: () => state.origin,
74
- },
75
- rgb: getProperty(COLOR_TYPE.rgb, instance as Color, state, subscriptions, false),
76
- rgba: getProperty(COLOR_TYPE.rgb, instance as Color, state, subscriptions, true),
77
- });
78
-
79
- return Object.freeze(instance) as Color;
80
- }
81
-
82
- function getProperty(
83
- space: keyof typeof setters,
84
- color: Color,
85
- state: ColorState,
86
- subscriptions: Subscriptions<Function>,
87
- alpha: boolean,
88
- ): PropertyDescriptor {
89
- const setValue = setters[space];
90
-
91
- return {
92
- enumerable: true,
93
- get: () => getValue(space, state, alpha),
94
- set: (value: unknown) => setValue(color, state, subscriptions, value, alpha),
95
- };
96
- }
97
-
98
- function getValue(space: keyof typeof setters, state: ColorState, alpha: boolean) {
99
- const value = getColorFromState(state, space);
100
-
101
- if (typeof value === 'string') {
102
- return alpha ? `${value}${state.alpha.hex}` : value;
103
- }
104
-
105
- return alpha
106
- ? {
107
- ...value,
108
- alpha: state.alpha.value,
109
- }
110
- : {...value};
111
- }
112
-
113
- function setAlphaValue(state: ColorState, value: unknown): void {
114
- if (typeof value === 'number' && !Number.isNaN(value)) {
115
- state.alpha = getAlpha(value, false);
116
- }
117
- }
118
-
119
- function subscribeToColor(
120
- subscriptions: Subscriptions,
121
- first: unknown,
122
- second?: unknown,
123
- ): Subscription {
124
- let key: string | undefined;
125
- let value: Function | undefined;
126
-
127
- if (typeof first === 'string') {
128
- key = first;
129
- value = second as Function;
130
- } else if (typeof first === 'function') {
131
- value = first;
132
- }
133
-
134
- if (typeof value !== 'function') {
135
- throw new Error();
136
- }
137
-
138
- return getSubscription({
139
- key,
140
- subscriptions,
141
- value,
142
- property: colorSubscription,
143
- });
144
- }
145
-
146
- // #endregion
147
-
148
- // #region Variables
149
-
150
- const colorSubscription: SubscriptionProperty = {
151
- key: COLOR_PROPERTY.name,
152
- value: COLOR_PROPERTY.subscription,
153
- };
154
-
155
- const setters: SetValues = {
156
- hex: setHexColor,
157
- hsl: setHSLColor,
158
- hwb: setHWBColor,
159
- rgb: setRGBColor,
160
- };
161
-
162
- // #endregion
@@ -1,56 +0,0 @@
1
- import {round} from '../../internal/math/misc';
2
- import {clamp} from '../../internal/number';
3
- import {COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX} from '../constants';
4
- import type {Alpha} from '../models';
5
-
6
- // #region Functions
7
-
8
- export function getAlpha(value: unknown, hex: boolean): Alpha {
9
- if (typeof value === 'number') {
10
- return getAlphaFromValue(value);
11
- }
12
-
13
- if (typeof value !== 'string' || value.toLowerCase() === COLOR_ALPHA.fullHexLong) {
14
- return {...COLOR_DEFAULTS.alpha};
15
- }
16
-
17
- if (hex && COLOR_EXPRESSION.alphaHex.test(value)) {
18
- return {
19
- hex: value,
20
- value: Number.parseInt(value, 16) / COLOR_MAX.hex,
21
- };
22
- }
23
-
24
- return getAlphaFromValue(Number.parseFloat(value));
25
- }
26
-
27
- export function getAlphaHexadecimal(value: number): string {
28
- if (value === COLOR_ALPHA.noneValue) {
29
- return COLOR_ALPHA.noneHex;
30
- }
31
-
32
- if (value === COLOR_MAX.percent) {
33
- return COLOR_ALPHA.fullHexLong;
34
- }
35
-
36
- return round((value / COLOR_MAX.percent) * COLOR_MAX.hex).toString(16);
37
- }
38
-
39
- function getAlphaFromValue(value: number): Alpha {
40
- const alpha = getAlphaValue(value);
41
-
42
- return {
43
- hex: getAlphaHexadecimal(alpha),
44
- value: alpha,
45
- };
46
- }
47
-
48
- export function getAlphaValue(original: number): number {
49
- if (Number.isNaN(original)) {
50
- return COLOR_MAX.percent;
51
- }
52
-
53
- return clamp(original, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
54
- }
55
-
56
- // #endregion
@@ -1,29 +0,0 @@
1
- import {COLOR_TYPE} from '../constants';
2
- import type {ColorState} from '../models';
3
- import {getColorFromState} from './state';
4
-
5
- // #region Functions
6
-
7
- export function formatHslColor(state: ColorState, alpha?: boolean): string {
8
- const {hue, lightness, saturation} = getColorFromState(state, COLOR_TYPE.hsl);
9
-
10
- return `hsl(${hue}deg ${saturation}% ${lightness}%${getSuffix(state, alpha)})`;
11
- }
12
-
13
- export function formatHwbColor(state: ColorState, alpha?: boolean): string {
14
- const {blackness, hue, whiteness} = getColorFromState(state, COLOR_TYPE.hwb);
15
-
16
- return `hwb(${hue}deg ${whiteness}% ${blackness}%${getSuffix(state, alpha)})`;
17
- }
18
-
19
- export function formatRgbColor(state: ColorState, alpha?: boolean): string {
20
- const {blue, green, red} = getColorFromState(state, COLOR_TYPE.rgb);
21
-
22
- return `rgb(${red} ${green} ${blue}${getSuffix(state, alpha)})`;
23
- }
24
-
25
- function getSuffix(state: ColorState, alpha?: boolean): string {
26
- return alpha === true ? ` / ${state.alpha.value}%` : '';
27
- }
28
-
29
- // #endregion
@@ -1,210 +0,0 @@
1
- import {round} from '../../internal/math/misc';
2
- import {clamp} from '../../internal/number';
3
- import {COLOR_DEFAULTS, COLOR_MAX, COLOR_SRGB, COLOR_TYPE} from '../constants';
4
- import {color} from '../instance';
5
- import type {Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor} from '../models';
6
- import {getColorFromState, getColorState} from './state';
7
-
8
- // #region Functions
9
-
10
- function getClampedValue(
11
- value: unknown,
12
- minimum: number,
13
- maximum: number,
14
- rounding?: boolean,
15
- ): number {
16
- return typeof value === 'number' && !Number.isNaN(value)
17
- ? clamp((rounding ?? false) ? round(value) : value, minimum, maximum)
18
- : minimum;
19
- }
20
-
21
- /**
22
- * Get a foreground color _(usually text)_ based on a background color's luminance as an unprefixed hex color string
23
- *
24
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
25
- * - If the value cannot be parsed, a white foreground color will be returned
26
- *
27
- * @param value Original value
28
- * @returns Foreground color
29
- */
30
- export function getForegroundColor(value: unknown, hex: true): string;
31
-
32
- /**
33
- * Get a foreground color _(usually text)_ based on a background color's luminance
34
- *
35
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
36
- * - If the value cannot be parsed, a white foreground color will be returned
37
- *
38
- * @param value Original value
39
- * @returns Foreground color
40
- */
41
- export function getForegroundColor(value: unknown): Color;
42
-
43
- export function getForegroundColor(value: unknown, hex?: unknown): string | Color {
44
- const rgb = getColorFromState(getColorState(value), COLOR_TYPE.rgb);
45
- const {blue, green, red} = rgb;
46
-
47
- const values = [blue / COLOR_MAX.hex, green / COLOR_MAX.hex, red / COLOR_MAX.hex];
48
- const {length} = values;
49
-
50
- for (let index = 0; index < length; index += 1) {
51
- const color = values[index];
52
-
53
- if (color <= COLOR_SRGB.luminanceMinimum) {
54
- values[index] /= COLOR_SRGB.luminanceMultiplier;
55
- } else {
56
- values[index] =
57
- ((color + COLOR_SRGB.luminanceOffset) / COLOR_SRGB.luminanceModifier) **
58
- COLOR_SRGB.luminanceExponent;
59
- }
60
- }
61
-
62
- const luminance =
63
- COLOR_SRGB.luminanceRed * values[2] +
64
- COLOR_SRGB.luminanceGreen * values[1] +
65
- COLOR_SRGB.luminanceBlue * values[0];
66
-
67
- // Rudimentary and ureliable?; implement APCA for more reliable results?
68
- const foreground =
69
- luminance > COLOR_SRGB.luminanceThreshold ? COLOR_DEFAULTS.hexBlack : COLOR_DEFAULTS.hexWhite;
70
-
71
- return hex === true ? foreground : color(foreground);
72
- }
73
-
74
- /**
75
- * Get the hex color _(with alpha channel, i.e., opacity)_ from any kind of value
76
- *
77
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
78
- * - If the value cannot be parsed, a black hex color will be returned
79
- *
80
- * @param value Original value
81
- * @returns Hex color string
82
- */
83
- export function getHexaColor(value: unknown): string {
84
- const {alpha, hex} = getColorState(value);
85
-
86
- return `${hex}${alpha.hex}`;
87
- }
88
-
89
- /**
90
- * Get the hex color from any kind of value
91
- *
92
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
93
- * - If the value cannot be parsed, a black hex color will be returned
94
- *
95
- * @param value Original value
96
- * @returns Hex color string
97
- */
98
- export function getHexColor(value: unknown): string {
99
- return getColorFromState(getColorState(value), COLOR_TYPE.hex);
100
- }
101
-
102
- export function getHexValue(value: unknown): number {
103
- return getClampedValue(value, 0, COLOR_MAX.hex, true);
104
- }
105
-
106
- export function getDegrees(value: unknown): number {
107
- return getClampedValue(value, 0, COLOR_MAX.degree);
108
- }
109
-
110
- /**
111
- * Get the _HSLA_ color from any kind of value
112
- *
113
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
114
- * - If the value cannot be parsed, a black _HSLA_ color will be returned
115
- *
116
- * @param value Original value
117
- * @returns _HSLA_ color
118
- */
119
- export function getHslaColor(value: unknown): HSLAColor {
120
- const state = getColorState(value);
121
- const hsl = getColorFromState(state, COLOR_TYPE.hsl);
122
-
123
- return {
124
- ...hsl,
125
- alpha: state.alpha.value,
126
- };
127
- }
128
-
129
- /**
130
- * Get the _HWB_ color from any kind of value
131
- *
132
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
133
- * - If the value cannot be parsed, a black _HWB_ color will be returned
134
- *
135
- * @param value Original value
136
- * @returns _HWB_ color
137
- */
138
- export function getHwbColor(value: unknown): HWBColor {
139
- return getColorFromState(getColorState(value), COLOR_TYPE.hwb);
140
- }
141
-
142
- /**
143
- * Get the _HWBA_ color from any kind of value
144
- *
145
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
146
- * - If the value cannot be parsed, a black _HWBA_ color will be returned
147
- *
148
- * @param value Original value
149
- * @returns _HWBA_ color
150
- */
151
- export function getHwbaColor(value: unknown): HWBAColor {
152
- const state = getColorState(value);
153
- const hwb = getColorFromState(state, COLOR_TYPE.hwb);
154
-
155
- return {
156
- ...hwb,
157
- alpha: state.alpha.value,
158
- };
159
- }
160
-
161
- /**
162
- * Get the _HSL_ color from any kind of value
163
- *
164
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
165
- * - If the value cannot be parsed, a black _HSL_ color will be returned
166
- *
167
- * @param value Original value
168
- * @returns _HSL_ color
169
- */
170
- export function getHslColor(value: unknown): HSLColor {
171
- return getColorFromState(getColorState(value), COLOR_TYPE.hsl);
172
- }
173
-
174
- export function getPercentage(value: unknown): number {
175
- return getClampedValue(value, 0, COLOR_MAX.percent);
176
- }
177
-
178
- /**
179
- * Get the _RGBA_ color from any kind of value
180
- *
181
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
182
- * - If the value cannot be parsed, a black _RGBA_ color will be returned
183
- *
184
- * @param value Original value
185
- * @returns _RGBA_ color
186
- */
187
- export function getRgbaColor(value: unknown): RGBAColor {
188
- const state = getColorState(value);
189
- const rgb = getColorFromState(state, COLOR_TYPE.rgb);
190
-
191
- return {
192
- ...rgb,
193
- alpha: state.alpha.value,
194
- };
195
- }
196
-
197
- /**
198
- * Get the _RGB_ color from any kind of value
199
- *
200
- * - Values that can be parsed are: hex(a) color strings, as well as _HSL(A)_, _HWB(A)_, and _RGB(A)_ color objects
201
- * - If the value cannot be parsed, a black _RGB_ color will be returned
202
- *
203
- * @param value Original value
204
- * @returns _RGB_ color
205
- */
206
- export function getRgbColor(value: unknown): RGBColor {
207
- return getColorFromState(getColorState(value), COLOR_TYPE.rgb);
208
- }
209
-
210
- // #endregion
@@ -1,221 +0,0 @@
1
- import {isPlainObject} from '../../internal/is';
2
- import {between} from '../../internal/number';
3
- import type {PlainObject} from '../../models';
4
- import {
5
- COLOR_ALPHA,
6
- COLOR_EXPRESSION,
7
- COLOR_KEYS,
8
- COLOR_LENGTHS,
9
- COLOR_MAX,
10
- COLOR_PROPERTY,
11
- } from '../constants';
12
- import type {Color} from '../index';
13
- import type {
14
- ColorProperty,
15
- HSLAColor,
16
- HSLColor,
17
- HWBAColor,
18
- HWBColor,
19
- RGBAColor,
20
- RGBColor,
21
- } from '../models';
22
-
23
- // ##region Functions
24
-
25
- function hasKeys(value: unknown, keys: ColorProperty[]): boolean {
26
- return typeof value === 'object' && value !== null && keys.every(key => key in value);
27
- }
28
-
29
- function isAlpha(value: unknown): value is number {
30
- return typeof value === 'number' && between(value, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
31
- }
32
-
33
- function isBytey(value: unknown): value is number {
34
- return typeof value === 'number' && between(value, 0, COLOR_MAX.hex);
35
- }
36
-
37
- /**
38
- * Is the value a _Color_?
39
- *
40
- * @param value Value to check
41
- * @returns `true` if the value is a _Color_, otherwise `false`
42
- */
43
- export function isColor(value: unknown): value is Color {
44
- return (
45
- isPlainObject(value) && COLOR_PROPERTY.name in value && value[COLOR_PROPERTY.name] === true
46
- );
47
- }
48
-
49
- function isColorValue(obj: unknown, properties: ColorProperty[]): boolean {
50
- if (typeof obj !== 'object' || obj === null) {
51
- return false;
52
- }
53
-
54
- const keys = Object.keys(obj);
55
- const {length} = keys;
56
-
57
- if (length !== properties.length) {
58
- return false;
59
- }
60
-
61
- for (let index = 0; index < length; index += 1) {
62
- const key = keys[index];
63
-
64
- if (
65
- !(
66
- properties.includes(key as never) &&
67
- validators[key as ColorProperty]((obj as PlainObject)[key])
68
- )
69
- ) {
70
- return false;
71
- }
72
- }
73
-
74
- return true;
75
- }
76
-
77
- function isDegree(value: unknown): value is number {
78
- return typeof value === 'number' && between(value, 0, COLOR_MAX.degree);
79
- }
80
-
81
- /**
82
- * Is the value a hex color?
83
- *
84
- * @param value Value to check
85
- * @param alpha Allow alpha channel _(opacity)_? _(defaults to `true`)_
86
- * @returns `true` if the value is a hex color, otherwise `false`
87
- *
88
- * @example
89
- * ```typescript
90
- * isHexColor('ff0000'); // => true
91
- * isHexColor('#ff0000'); // => true
92
- *
93
- * isHexColor('#ff000050'); // => true
94
- * isHexColor('#ff000050', false); // => false
95
- * ```
96
- */
97
- export function isHexColor(value: unknown, alpha?: boolean): value is string {
98
- if (typeof value !== 'string') {
99
- return false;
100
- }
101
-
102
- if (!(COLOR_EXPRESSION.hexShort.test(value) || COLOR_EXPRESSION.hexLong.test(value))) {
103
- return false;
104
- }
105
-
106
- if (alpha === false) {
107
- const unprefixed = value.replace(COLOR_EXPRESSION.prefix, '');
108
-
109
- return (
110
- unprefixed.length === COLOR_LENGTHS.hexShort || unprefixed.length === COLOR_LENGTHS.hexLong
111
- );
112
- }
113
-
114
- return true;
115
- }
116
-
117
- /**
118
- * Is the value an _HSLA_ color?
119
- *
120
- * @param value Value to check
121
- * @returns `true` if the value is an _HSLA_ color, otherwise `false`
122
- */
123
- export function isHslaColor(value: unknown): value is HSLAColor {
124
- return isColorValue(value, COLOR_KEYS.hsla);
125
- }
126
-
127
- export function isHslaLike(value: unknown): value is Record<keyof HSLAColor, unknown> {
128
- return hasKeys(value, COLOR_KEYS.hsla);
129
- }
130
-
131
- /**
132
- * Is the value an _HSL_ color?
133
- *
134
- * @param value Value to check
135
- * @returns `true` if the value is an _HSL_ color, otherwise `false`
136
- */
137
- export function isHslColor(value: unknown): value is HSLColor {
138
- return isColorValue(value, COLOR_KEYS.hsla) || isColorValue(value, COLOR_KEYS.hsl);
139
- }
140
-
141
- export function isHslLike(value: unknown): value is Record<keyof HSLColor, unknown> {
142
- return hasKeys(value, COLOR_KEYS.hsl);
143
- }
144
-
145
- /**
146
- * Is the value an _HWBA_ color?
147
- *
148
- * @param value Value to check
149
- * @returns `true` if the value is an _HWBA_ color, otherwise `false`
150
- */
151
- export function isHwbaColor(value: unknown): value is HWBAColor {
152
- return isColorValue(value, COLOR_KEYS.hwba);
153
- }
154
-
155
- export function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, unknown> {
156
- return hasKeys(value, COLOR_KEYS.hwba);
157
- }
158
-
159
- /**
160
- * Is the value an _HWB_ color?
161
- *
162
- * @param value Value to check
163
- * @returns `true` if the value is an _HWB_ color, otherwise `false`
164
- */
165
- export function isHwbColor(value: unknown): value is HWBColor {
166
- return isColorValue(value, COLOR_KEYS.hwb) || isColorValue(value, COLOR_KEYS.hwba);
167
- }
168
-
169
- export function isHwbLike(value: unknown): value is Record<keyof HWBColor, unknown> {
170
- return hasKeys(value, COLOR_KEYS.hwb);
171
- }
172
-
173
- /**
174
- * Is the value an _RGBA_ color?
175
- *
176
- * @param value Value to check
177
- * @returns `true` if the value is an _RGBA_ color, otherwise `false`
178
- */
179
- export function isRgbaColor(value: unknown): value is RGBAColor {
180
- return isColorValue(value, COLOR_KEYS.rgba);
181
- }
182
-
183
- export function isRgbaLike(value: unknown): value is Record<keyof RGBAColor, unknown> {
184
- return hasKeys(value, COLOR_KEYS.rgba);
185
- }
186
-
187
- /**
188
- * Is the value an _RGB_ color?
189
- *
190
- * @param value Value to check
191
- * @returns `true` if the value is an _RGB_ color, otherwise `false`
192
- */
193
- export function isRgbColor(value: unknown): value is RGBColor {
194
- return isColorValue(value, COLOR_KEYS.rgba) || isColorValue(value, COLOR_KEYS.rgb);
195
- }
196
-
197
- export function isRgbLike(value: unknown): value is Record<keyof RGBColor, unknown> {
198
- return hasKeys(value, COLOR_KEYS.rgb);
199
- }
200
-
201
- function isPercentage(value: unknown): value is number {
202
- return typeof value === 'number' && between(value, 0, COLOR_MAX.percent);
203
- }
204
-
205
- // #endregion
206
-
207
- // #region Variables
208
-
209
- const validators: Record<ColorProperty, (value: unknown) => value is number> = {
210
- alpha: isAlpha,
211
- blackness: isPercentage,
212
- blue: isBytey,
213
- green: isBytey,
214
- hue: isDegree,
215
- lightness: isPercentage,
216
- saturation: isPercentage,
217
- red: isBytey,
218
- whiteness: isPercentage,
219
- };
220
-
221
- // #endregion