@oscarpalmer/atoms 0.192.0 → 0.193.1

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/index.d.mts +25 -23
  2. package/dist/index.mjs +30 -31
  3. package/dist/queue.d.mts +25 -23
  4. package/dist/queue.mjs +27 -28
  5. package/package.json +3 -4
  6. package/src/array/difference.ts +0 -73
  7. package/src/array/exists.ts +0 -93
  8. package/src/array/filter.ts +0 -182
  9. package/src/array/find.ts +0 -183
  10. package/src/array/first.ts +0 -196
  11. package/src/array/flatten.ts +0 -20
  12. package/src/array/from.ts +0 -129
  13. package/src/array/get.ts +0 -131
  14. package/src/array/group-by.ts +0 -369
  15. package/src/array/index.ts +0 -26
  16. package/src/array/insert.ts +0 -52
  17. package/src/array/intersection.ts +0 -72
  18. package/src/array/last.ts +0 -194
  19. package/src/array/match.ts +0 -436
  20. package/src/array/move.ts +0 -315
  21. package/src/array/partition.ts +0 -91
  22. package/src/array/push.ts +0 -23
  23. package/src/array/reverse.ts +0 -33
  24. package/src/array/select.ts +0 -260
  25. package/src/array/single.ts +0 -93
  26. package/src/array/slice.ts +0 -335
  27. package/src/array/sort.ts +0 -607
  28. package/src/array/splice.ts +0 -106
  29. package/src/array/swap.ts +0 -322
  30. package/src/array/to-map.ts +0 -371
  31. package/src/array/to-record.ts +0 -351
  32. package/src/array/to-set.ts +0 -80
  33. package/src/array/toggle.ts +0 -79
  34. package/src/array/union.ts +0 -72
  35. package/src/array/unique.ts +0 -67
  36. package/src/array/update.ts +0 -75
  37. package/src/beacon.ts +0 -378
  38. package/src/color/constants.ts +0 -137
  39. package/src/color/index.ts +0 -83
  40. package/src/color/instance.ts +0 -162
  41. package/src/color/misc/alpha.ts +0 -56
  42. package/src/color/misc/format.ts +0 -29
  43. package/src/color/misc/get.ts +0 -210
  44. package/src/color/misc/is.ts +0 -221
  45. package/src/color/misc/state.ts +0 -243
  46. package/src/color/models.ts +0 -389
  47. package/src/color/space/hex.ts +0 -184
  48. package/src/color/space/hsl.ts +0 -174
  49. package/src/color/space/hwb.ts +0 -153
  50. package/src/color/space/rgb.ts +0 -240
  51. package/src/function/assert.ts +0 -174
  52. package/src/function/index.ts +0 -2
  53. package/src/function/limit.ts +0 -83
  54. package/src/function/memoize.ts +0 -202
  55. package/src/function/once.ts +0 -200
  56. package/src/function/retry.ts +0 -175
  57. package/src/function/work.ts +0 -1007
  58. package/src/herald.ts +0 -216
  59. package/src/index.ts +0 -68
  60. package/src/internal/array/callbacks.ts +0 -46
  61. package/src/internal/array/chunk.ts +0 -51
  62. package/src/internal/array/compact.ts +0 -56
  63. package/src/internal/array/find.ts +0 -200
  64. package/src/internal/array/group.ts +0 -42
  65. package/src/internal/array/index-of.ts +0 -182
  66. package/src/internal/array/insert.ts +0 -80
  67. package/src/internal/array/overlap.ts +0 -35
  68. package/src/internal/array/sets.ts +0 -74
  69. package/src/internal/array/shuffle.ts +0 -33
  70. package/src/internal/array/update.ts +0 -52
  71. package/src/internal/function/misc.ts +0 -8
  72. package/src/internal/function/timer.ts +0 -168
  73. package/src/internal/is.ts +0 -244
  74. package/src/internal/math/aggregate.ts +0 -164
  75. package/src/internal/math/misc.ts +0 -28
  76. package/src/internal/number.ts +0 -129
  77. package/src/internal/random.ts +0 -49
  78. package/src/internal/result.ts +0 -92
  79. package/src/internal/sized.ts +0 -25
  80. package/src/internal/string.ts +0 -128
  81. package/src/internal/subscription.ts +0 -264
  82. package/src/internal/value/compare.ts +0 -170
  83. package/src/internal/value/equal.ts +0 -465
  84. package/src/internal/value/get.ts +0 -57
  85. package/src/internal/value/handlers.ts +0 -95
  86. package/src/internal/value/has.ts +0 -118
  87. package/src/internal/value/misc.ts +0 -125
  88. package/src/internal/value/partial.ts +0 -46
  89. package/src/internal/value/set.ts +0 -132
  90. package/src/is.ts +0 -182
  91. package/src/logger.ts +0 -230
  92. package/src/math.ts +0 -281
  93. package/src/models.ts +0 -286
  94. package/src/number.ts +0 -1
  95. package/src/promise/delay.ts +0 -62
  96. package/src/promise/helpers.ts +0 -96
  97. package/src/promise/index.ts +0 -300
  98. package/src/promise/misc.ts +0 -96
  99. package/src/promise/models.ts +0 -195
  100. package/src/promise/timed.ts +0 -91
  101. package/src/query.ts +0 -147
  102. package/src/queue.ts +0 -814
  103. package/src/random.ts +0 -125
  104. package/src/result/index.ts +0 -105
  105. package/src/result/match.ts +0 -124
  106. package/src/result/misc.ts +0 -129
  107. package/src/result/models.ts +0 -83
  108. package/src/result/work/flow.ts +0 -488
  109. package/src/result/work/pipe.ts +0 -496
  110. package/src/sized/map.ts +0 -110
  111. package/src/sized/set.ts +0 -107
  112. package/src/string/case.ts +0 -236
  113. package/src/string/fuzzy.ts +0 -444
  114. package/src/string/index.ts +0 -199
  115. package/src/string/match.ts +0 -80
  116. package/src/string/normalize.ts +0 -205
  117. package/src/string/template.ts +0 -161
  118. package/src/value/clone.ts +0 -319
  119. package/src/value/collection.ts +0 -63
  120. package/src/value/diff.ts +0 -232
  121. package/src/value/freeze.ts +0 -322
  122. package/src/value/handle.ts +0 -3
  123. package/src/value/index.ts +0 -5
  124. package/src/value/merge.ts +0 -278
  125. package/src/value/omit.ts +0 -20
  126. package/src/value/pick.ts +0 -20
  127. package/src/value/shake.ts +0 -45
  128. package/src/value/smush.ts +0 -89
  129. package/src/value/transform.ts +0 -153
  130. 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