@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.
- package/dist/index.d.mts +25 -23
- package/dist/index.mjs +30 -31
- package/dist/queue.d.mts +25 -23
- package/dist/queue.mjs +27 -28
- package/package.json +3 -4
- package/src/array/difference.ts +0 -73
- package/src/array/exists.ts +0 -93
- package/src/array/filter.ts +0 -182
- package/src/array/find.ts +0 -183
- package/src/array/first.ts +0 -196
- package/src/array/flatten.ts +0 -20
- package/src/array/from.ts +0 -129
- package/src/array/get.ts +0 -131
- package/src/array/group-by.ts +0 -369
- package/src/array/index.ts +0 -26
- package/src/array/insert.ts +0 -52
- package/src/array/intersection.ts +0 -72
- package/src/array/last.ts +0 -194
- package/src/array/match.ts +0 -436
- package/src/array/move.ts +0 -315
- package/src/array/partition.ts +0 -91
- package/src/array/push.ts +0 -23
- package/src/array/reverse.ts +0 -33
- package/src/array/select.ts +0 -260
- package/src/array/single.ts +0 -93
- package/src/array/slice.ts +0 -335
- package/src/array/sort.ts +0 -607
- package/src/array/splice.ts +0 -106
- package/src/array/swap.ts +0 -322
- package/src/array/to-map.ts +0 -371
- package/src/array/to-record.ts +0 -351
- package/src/array/to-set.ts +0 -80
- package/src/array/toggle.ts +0 -79
- package/src/array/union.ts +0 -72
- package/src/array/unique.ts +0 -67
- package/src/array/update.ts +0 -75
- package/src/beacon.ts +0 -378
- package/src/color/constants.ts +0 -137
- package/src/color/index.ts +0 -83
- package/src/color/instance.ts +0 -162
- package/src/color/misc/alpha.ts +0 -56
- package/src/color/misc/format.ts +0 -29
- package/src/color/misc/get.ts +0 -210
- package/src/color/misc/is.ts +0 -221
- package/src/color/misc/state.ts +0 -243
- package/src/color/models.ts +0 -389
- package/src/color/space/hex.ts +0 -184
- package/src/color/space/hsl.ts +0 -174
- package/src/color/space/hwb.ts +0 -153
- package/src/color/space/rgb.ts +0 -240
- package/src/function/assert.ts +0 -174
- package/src/function/index.ts +0 -2
- package/src/function/limit.ts +0 -83
- package/src/function/memoize.ts +0 -202
- package/src/function/once.ts +0 -200
- package/src/function/retry.ts +0 -175
- package/src/function/work.ts +0 -1007
- package/src/herald.ts +0 -216
- package/src/index.ts +0 -68
- package/src/internal/array/callbacks.ts +0 -46
- package/src/internal/array/chunk.ts +0 -51
- package/src/internal/array/compact.ts +0 -56
- package/src/internal/array/find.ts +0 -200
- package/src/internal/array/group.ts +0 -42
- package/src/internal/array/index-of.ts +0 -182
- package/src/internal/array/insert.ts +0 -80
- package/src/internal/array/overlap.ts +0 -35
- package/src/internal/array/sets.ts +0 -74
- package/src/internal/array/shuffle.ts +0 -33
- package/src/internal/array/update.ts +0 -52
- package/src/internal/function/misc.ts +0 -8
- package/src/internal/function/timer.ts +0 -168
- package/src/internal/is.ts +0 -244
- package/src/internal/math/aggregate.ts +0 -164
- package/src/internal/math/misc.ts +0 -28
- package/src/internal/number.ts +0 -129
- package/src/internal/random.ts +0 -49
- package/src/internal/result.ts +0 -92
- package/src/internal/sized.ts +0 -25
- package/src/internal/string.ts +0 -128
- package/src/internal/subscription.ts +0 -264
- package/src/internal/value/compare.ts +0 -170
- package/src/internal/value/equal.ts +0 -465
- package/src/internal/value/get.ts +0 -57
- package/src/internal/value/handlers.ts +0 -95
- package/src/internal/value/has.ts +0 -118
- package/src/internal/value/misc.ts +0 -125
- package/src/internal/value/partial.ts +0 -46
- package/src/internal/value/set.ts +0 -132
- package/src/is.ts +0 -182
- package/src/logger.ts +0 -230
- package/src/math.ts +0 -281
- package/src/models.ts +0 -286
- package/src/number.ts +0 -1
- package/src/promise/delay.ts +0 -62
- package/src/promise/helpers.ts +0 -96
- package/src/promise/index.ts +0 -300
- package/src/promise/misc.ts +0 -96
- package/src/promise/models.ts +0 -195
- package/src/promise/timed.ts +0 -91
- package/src/query.ts +0 -147
- package/src/queue.ts +0 -814
- package/src/random.ts +0 -125
- package/src/result/index.ts +0 -105
- package/src/result/match.ts +0 -124
- package/src/result/misc.ts +0 -129
- package/src/result/models.ts +0 -83
- package/src/result/work/flow.ts +0 -488
- package/src/result/work/pipe.ts +0 -496
- package/src/sized/map.ts +0 -110
- package/src/sized/set.ts +0 -107
- package/src/string/case.ts +0 -236
- package/src/string/fuzzy.ts +0 -444
- package/src/string/index.ts +0 -199
- package/src/string/match.ts +0 -80
- package/src/string/normalize.ts +0 -205
- package/src/string/template.ts +0 -161
- package/src/value/clone.ts +0 -319
- package/src/value/collection.ts +0 -63
- package/src/value/diff.ts +0 -232
- package/src/value/freeze.ts +0 -322
- package/src/value/handle.ts +0 -3
- package/src/value/index.ts +0 -5
- package/src/value/merge.ts +0 -278
- package/src/value/omit.ts +0 -20
- package/src/value/pick.ts +0 -20
- package/src/value/shake.ts +0 -45
- package/src/value/smush.ts +0 -89
- package/src/value/transform.ts +0 -153
- package/src/value/unsmush.ts +0 -84
package/src/color/space/hex.ts
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import {join} from '../../internal/string';
|
|
2
|
-
import {
|
|
3
|
-
COLOR_ALPHA,
|
|
4
|
-
COLOR_DEFAULTS,
|
|
5
|
-
COLOR_EXPRESSION,
|
|
6
|
-
COLOR_LENGTHS,
|
|
7
|
-
COLOR_MAX,
|
|
8
|
-
COLOR_TYPE,
|
|
9
|
-
} from '../constants';
|
|
10
|
-
import {getHexValue, getPercentage} from '../misc/get';
|
|
11
|
-
import {isHexColor} from '../misc/is';
|
|
12
|
-
import type {
|
|
13
|
-
ColorState,
|
|
14
|
-
ColorType,
|
|
15
|
-
HSLAColor,
|
|
16
|
-
HSLColor,
|
|
17
|
-
HWBAColor,
|
|
18
|
-
HWBColor,
|
|
19
|
-
RGBAColor,
|
|
20
|
-
RGBColor,
|
|
21
|
-
} from '../models';
|
|
22
|
-
import {convertRgbToHsla, convertRgbToHwba} from './rgb';
|
|
23
|
-
|
|
24
|
-
// #region Functions
|
|
25
|
-
|
|
26
|
-
function convertHexToRgba(value: string): RGBAColor {
|
|
27
|
-
const normalized = getNormalizedHex(value, true);
|
|
28
|
-
const pairs = COLOR_EXPRESSION.hexLong.exec(normalized) as RegExpExecArray;
|
|
29
|
-
const values: number[] = [];
|
|
30
|
-
|
|
31
|
-
const {length} = pairs;
|
|
32
|
-
|
|
33
|
-
for (let index = 1; index < length; index += 1) {
|
|
34
|
-
values.push(Number.parseInt(pairs[index], 16));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
alpha: getPercentage((values[3] / COLOR_MAX.hex) * 100),
|
|
39
|
-
blue: getHexValue(values[2]),
|
|
40
|
-
green: getHexValue(values[1]),
|
|
41
|
-
red: getHexValue(values[0]),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getColorFromHex<Type extends ColorType>(
|
|
46
|
-
state: ColorState,
|
|
47
|
-
type: Type,
|
|
48
|
-
): NonNullable<ColorState[Type]> {
|
|
49
|
-
switch (type) {
|
|
50
|
-
case COLOR_TYPE.hsl:
|
|
51
|
-
state.hsl ??= hexToHsl(state.hex!);
|
|
52
|
-
break;
|
|
53
|
-
|
|
54
|
-
case COLOR_TYPE.hwb:
|
|
55
|
-
state.hwb ??= hexToHwb(state.hex!);
|
|
56
|
-
break;
|
|
57
|
-
|
|
58
|
-
case COLOR_TYPE.rgb:
|
|
59
|
-
state.rgb ??= hexToRgb(state.hex!);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return state[type]!;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Get the normalized hex color from a value
|
|
68
|
-
*
|
|
69
|
-
* _If the value is unable to be parsed or normalized, a hex color of `000000` will be returned, with the alpha channel _(opacity)_ value `0` included, if specified_
|
|
70
|
-
*
|
|
71
|
-
* @param value Value to normalize
|
|
72
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
73
|
-
* @returns Normalized hex color
|
|
74
|
-
*/
|
|
75
|
-
export function getNormalizedHex(value: unknown, alpha?: boolean): string {
|
|
76
|
-
const includeAlpha = alpha ?? false;
|
|
77
|
-
|
|
78
|
-
if (!isHexColor(value)) {
|
|
79
|
-
return `${COLOR_DEFAULTS.hexBlack}${includeAlpha ? COLOR_ALPHA.fullHexLong : ''}`;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const normalized = value.replace(COLOR_EXPRESSION.prefix, '');
|
|
83
|
-
|
|
84
|
-
if (normalized.length < COLOR_LENGTHS.hexLong) {
|
|
85
|
-
const hex = normalized.slice(0, COLOR_LENGTHS.hexShort);
|
|
86
|
-
const a = includeAlpha ? (normalized[COLOR_LENGTHS.hexShort] ?? COLOR_ALPHA.fullHexShort) : '';
|
|
87
|
-
|
|
88
|
-
return join(`${hex}${a}`.split('').map(character => character.repeat(2)));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const hex = normalized.slice(0, COLOR_LENGTHS.hexLong);
|
|
92
|
-
|
|
93
|
-
const a = includeAlpha ? normalized.slice(COLOR_LENGTHS.hexLong) || COLOR_ALPHA.fullHexLong : '';
|
|
94
|
-
|
|
95
|
-
return `${hex}${a}`;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Convert a hex color string to a _HSL_ color
|
|
100
|
-
*
|
|
101
|
-
* _If the value is unable to be converted, a black _HSL_ color will be returned_
|
|
102
|
-
*
|
|
103
|
-
* @param value Hex color string
|
|
104
|
-
* @returns _HSL_ color
|
|
105
|
-
*/
|
|
106
|
-
export function hexToHsl(value: string): HSLColor {
|
|
107
|
-
const {hue, lightness, saturation} = hexToHsla(value);
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
hue,
|
|
111
|
-
lightness,
|
|
112
|
-
saturation,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Convert a hex color string to a _HSLA_ color
|
|
118
|
-
*
|
|
119
|
-
* _If the value is unable to be converted, a black _HSLA_ color will be returned_
|
|
120
|
-
*
|
|
121
|
-
* @param value Hex color string
|
|
122
|
-
* @returns _HSLA_ color
|
|
123
|
-
*/
|
|
124
|
-
export function hexToHsla(value: string): HSLAColor {
|
|
125
|
-
return convertRgbToHsla(convertHexToRgba(value));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Convert a hex color string to a _HWB_ color
|
|
130
|
-
*
|
|
131
|
-
* _If the value is unable to be converted, a black _HWB_ color will be returned_
|
|
132
|
-
*
|
|
133
|
-
* @param value Hex color string
|
|
134
|
-
* @returns _HWB_ color
|
|
135
|
-
*/
|
|
136
|
-
export function hexToHwb(value: string): HWBColor {
|
|
137
|
-
const {blackness, hue, whiteness} = hexToHwba(value);
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
hue,
|
|
141
|
-
whiteness,
|
|
142
|
-
blackness,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Convert a hex color string to a _HWBA_ color
|
|
148
|
-
*
|
|
149
|
-
* _If the value is unable to be converted, a black _HWBA_ color will be returned_
|
|
150
|
-
*
|
|
151
|
-
* @param value Hex color string
|
|
152
|
-
* @returns _HWBA_ color
|
|
153
|
-
*/
|
|
154
|
-
export function hexToHwba(value: string): HWBAColor {
|
|
155
|
-
return convertRgbToHwba(convertHexToRgba(value));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Convert a hex color to an _RGB_ color
|
|
160
|
-
*
|
|
161
|
-
* _If the value is unable to be converted, a black _RGB_ color will be returned_
|
|
162
|
-
*
|
|
163
|
-
* @param value Original value
|
|
164
|
-
* @returns _RGB_ color
|
|
165
|
-
*/
|
|
166
|
-
export function hexToRgb(value: string): RGBColor {
|
|
167
|
-
const {blue, green, red} = convertHexToRgba(value);
|
|
168
|
-
|
|
169
|
-
return {blue, green, red};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Convert a hex color to an _RGBA_ color
|
|
174
|
-
*
|
|
175
|
-
* _If the value is unable to be converted, a black _RGBA_ color will be returned_
|
|
176
|
-
*
|
|
177
|
-
* @param value Original value
|
|
178
|
-
* @returns _RGBA_ color
|
|
179
|
-
*/
|
|
180
|
-
export function hexToRgba(value: string): RGBAColor {
|
|
181
|
-
return convertHexToRgba(value);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// #endregion
|
package/src/color/space/hsl.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
2
|
-
import {getAlphaValue} from '../misc/alpha';
|
|
3
|
-
import {getDegrees, getHexValue, getPercentage} from '../misc/get';
|
|
4
|
-
import {isHslLike} from '../misc/is';
|
|
5
|
-
import type {
|
|
6
|
-
ColorState,
|
|
7
|
-
ColorType,
|
|
8
|
-
HSLAColor,
|
|
9
|
-
HSLColor,
|
|
10
|
-
HWBAColor,
|
|
11
|
-
HWBColor,
|
|
12
|
-
RGBAColor,
|
|
13
|
-
RGBColor,
|
|
14
|
-
} from '../models';
|
|
15
|
-
import {convertRgbToHex} from './rgb';
|
|
16
|
-
|
|
17
|
-
// #region Functions
|
|
18
|
-
|
|
19
|
-
function convertHslToHwba(input: unknown): HWBAColor {
|
|
20
|
-
const hsl = isHslLike(input) ? getHslValue(input) : {...COLOR_DEFAULTS.hsl};
|
|
21
|
-
|
|
22
|
-
let {hue, lightness, saturation} = hsl;
|
|
23
|
-
|
|
24
|
-
lightness /= COLOR_MAX.percent;
|
|
25
|
-
saturation /= COLOR_MAX.percent;
|
|
26
|
-
|
|
27
|
-
const value = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
28
|
-
|
|
29
|
-
if (value === 0) {
|
|
30
|
-
saturation = 0;
|
|
31
|
-
} else {
|
|
32
|
-
saturation = 2 * (1 - lightness / value);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const blackness = 1 - value;
|
|
36
|
-
const whiteness = (1 - saturation) * value;
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
hue,
|
|
40
|
-
whiteness: whiteness * COLOR_MAX.percent,
|
|
41
|
-
blackness: blackness * COLOR_MAX.percent,
|
|
42
|
-
alpha: getAlphaValue((input as HSLAColor)?.alpha ?? COLOR_MAX.percent),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function convertHslToRgba(input: unknown): RGBAColor {
|
|
47
|
-
const hsl = isHslLike(input) ? getHslValue(input) : {...COLOR_DEFAULTS.hsl};
|
|
48
|
-
|
|
49
|
-
const hue = hsl.hue % COLOR_MAX.degree;
|
|
50
|
-
const saturation = hsl.saturation / COLOR_MAX.percent;
|
|
51
|
-
const lightness = hsl.lightness / COLOR_MAX.percent;
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
alpha: getAlphaValue((input as HSLAColor)?.alpha ?? COLOR_MAX.percent),
|
|
55
|
-
blue: getHexValue(getHexyValue(hue, lightness, saturation, 4)),
|
|
56
|
-
green: getHexValue(getHexyValue(hue, lightness, saturation, 8)),
|
|
57
|
-
red: getHexValue(getHexyValue(hue, lightness, saturation, 0)),
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function getColorFromHsl<Type extends ColorType>(
|
|
62
|
-
state: ColorState,
|
|
63
|
-
type: Type,
|
|
64
|
-
): NonNullable<ColorState[Type]> {
|
|
65
|
-
switch (type) {
|
|
66
|
-
case COLOR_TYPE.hex:
|
|
67
|
-
state.hex = hslToHex(state.hsl!);
|
|
68
|
-
break;
|
|
69
|
-
|
|
70
|
-
case COLOR_TYPE.hwb:
|
|
71
|
-
state.hwb = hslToHwb(state.hsl!);
|
|
72
|
-
break;
|
|
73
|
-
|
|
74
|
-
case COLOR_TYPE.rgb:
|
|
75
|
-
state.rgb = hslToRgb(state.hsl!);
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return state[type]!;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getHexyValue(hue: number, lightness: number, saturation: number, value: number): number {
|
|
83
|
-
const part = (value + hue / 30) % 12;
|
|
84
|
-
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
85
|
-
|
|
86
|
-
return (lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * COLOR_MAX.hex;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function getHslValue(value: Record<keyof HSLColor, unknown>): HSLColor {
|
|
90
|
-
return {
|
|
91
|
-
hue: getDegrees(value.hue),
|
|
92
|
-
saturation: getPercentage(value.saturation),
|
|
93
|
-
lightness: getPercentage(value.lightness),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Convert an _HSL(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
99
|
-
*
|
|
100
|
-
* _If the value is unable to be converted, a black hex color will be returned_
|
|
101
|
-
*
|
|
102
|
-
* @param hsl _HSL(A)_ color
|
|
103
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
104
|
-
* @returns Hex color string
|
|
105
|
-
*/
|
|
106
|
-
export function hslToHex(hsl: HSLAColor | HSLColor, alpha?: boolean): string {
|
|
107
|
-
return convertRgbToHex(convertHslToRgba(hsl), alpha ?? false);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Convert an _HSL(A)_ color to an _HWB_ color
|
|
112
|
-
*
|
|
113
|
-
* _If the value is unable to be converted, a black _HWB_ color will be returned_
|
|
114
|
-
*
|
|
115
|
-
* @param hsl HSL(A) color
|
|
116
|
-
* @returns _HWB_ color
|
|
117
|
-
*/
|
|
118
|
-
export function hslToHwb(hsl: HSLAColor | HSLColor): HWBColor {
|
|
119
|
-
const {blackness, hue, whiteness} = convertHslToHwba(hsl);
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
hue,
|
|
123
|
-
whiteness,
|
|
124
|
-
blackness,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Convert an _HSL(A)_ color to an _HWBA_ color
|
|
130
|
-
*
|
|
131
|
-
* _If the value is unable to be converted, a black _HWBA_ color will be returned_
|
|
132
|
-
*
|
|
133
|
-
* @param hsl HSL(A) color
|
|
134
|
-
* @returns _HWBA_ color
|
|
135
|
-
*/
|
|
136
|
-
export function hslToHwba(hsl: HSLAColor | HSLColor): HWBAColor {
|
|
137
|
-
return convertHslToHwba(hsl);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Convert an _HSL(A)_ color to an _RGB_ color
|
|
142
|
-
*
|
|
143
|
-
* _If the value is unable to be converted, a black _RGB_ color will be returned_
|
|
144
|
-
*
|
|
145
|
-
* _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
|
|
146
|
-
*
|
|
147
|
-
* @param hsl HSL(A) color
|
|
148
|
-
* @returns _RGB_ color
|
|
149
|
-
*/
|
|
150
|
-
export function hslToRgb(hsl: HSLAColor | HSLColor): RGBColor {
|
|
151
|
-
const {blue, green, red} = convertHslToRgba(hsl);
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
blue,
|
|
155
|
-
green,
|
|
156
|
-
red,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Convert an _HSL(A)_ color to an _RGBA_ color
|
|
162
|
-
*
|
|
163
|
-
* _If the value is unable to be converted, a black _RGBA_ color will be returned_
|
|
164
|
-
*
|
|
165
|
-
* _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
|
|
166
|
-
*
|
|
167
|
-
* @param hsl HSL(A) color
|
|
168
|
-
* @returns _RGBA_ color
|
|
169
|
-
*/
|
|
170
|
-
export function hslToRgba(hsl: HSLAColor | HSLColor): RGBAColor {
|
|
171
|
-
return convertHslToRgba(hsl);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// #endregion
|
package/src/color/space/hwb.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
2
|
-
import {getAlphaValue} from '../misc/alpha';
|
|
3
|
-
import {getDegrees, getPercentage} from '../misc/get';
|
|
4
|
-
import {isHwbLike} from '../misc/is';
|
|
5
|
-
import type {
|
|
6
|
-
ColorState,
|
|
7
|
-
ColorType,
|
|
8
|
-
HSLAColor,
|
|
9
|
-
HSLColor,
|
|
10
|
-
HWBAColor,
|
|
11
|
-
HWBColor,
|
|
12
|
-
RGBAColor,
|
|
13
|
-
RGBColor,
|
|
14
|
-
} from '../models';
|
|
15
|
-
import {hslToRgb, hslToRgba} from './hsl';
|
|
16
|
-
import {rgbToHex} from './rgb';
|
|
17
|
-
|
|
18
|
-
// #region Functions
|
|
19
|
-
|
|
20
|
-
export function convertHwbToHsl(input: HWBAColor | HWBColor): HSLAColor {
|
|
21
|
-
let {blackness, hue, whiteness} = isHwbLike(input) ? getHwbValue(input) : COLOR_DEFAULTS.hwb;
|
|
22
|
-
|
|
23
|
-
blackness /= COLOR_MAX.percent;
|
|
24
|
-
whiteness /= COLOR_MAX.percent;
|
|
25
|
-
|
|
26
|
-
if (blackness + whiteness > 1) {
|
|
27
|
-
const total = blackness + whiteness;
|
|
28
|
-
|
|
29
|
-
blackness /= total;
|
|
30
|
-
whiteness /= total;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const lightness = (1 - blackness + whiteness) / 2;
|
|
34
|
-
|
|
35
|
-
const saturation = getHwbSaturation(blackness, whiteness, lightness);
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
hue,
|
|
39
|
-
saturation: saturation * COLOR_MAX.percent,
|
|
40
|
-
lightness: lightness * COLOR_MAX.percent,
|
|
41
|
-
alpha: getAlphaValue((input as HWBAColor)?.alpha ?? COLOR_MAX.percent),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getColorFromHwb<Type extends ColorType>(
|
|
46
|
-
state: ColorState,
|
|
47
|
-
type: Type,
|
|
48
|
-
): NonNullable<ColorState[Type]> {
|
|
49
|
-
switch (type) {
|
|
50
|
-
case COLOR_TYPE.hex:
|
|
51
|
-
state.hex = hwbToHex(state.hwb!);
|
|
52
|
-
break;
|
|
53
|
-
|
|
54
|
-
case COLOR_TYPE.hsl:
|
|
55
|
-
state.hsl = hwbToHsl(state.hwb!);
|
|
56
|
-
break;
|
|
57
|
-
|
|
58
|
-
case COLOR_TYPE.rgb:
|
|
59
|
-
state.rgb = hwbToRgb(state.hwb!);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return state[type]!;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function getHwbSaturation(blackness: number, whiteness: number, lightness: number): number {
|
|
67
|
-
const value = 1 - blackness;
|
|
68
|
-
|
|
69
|
-
const hue = value - whiteness;
|
|
70
|
-
|
|
71
|
-
if (hue <= 0) {
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return hue / Math.min(2 * lightness, 2 - 2 * lightness);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function getHwbValue(value: Record<keyof HWBColor, unknown>): HWBColor {
|
|
79
|
-
return {
|
|
80
|
-
hue: getDegrees(value.hue),
|
|
81
|
-
whiteness: getPercentage(value.whiteness),
|
|
82
|
-
blackness: getPercentage(value.blackness),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Convert an _HWB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
88
|
-
*
|
|
89
|
-
* _If the value is unable to be converted, a black hex color will be returned_
|
|
90
|
-
*
|
|
91
|
-
* @param hwb _HWB(A)_ color
|
|
92
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
93
|
-
* @returns Hex color string
|
|
94
|
-
*/
|
|
95
|
-
export function hwbToHex(hwb: HWBAColor | HWBColor, alpha?: boolean): string {
|
|
96
|
-
return rgbToHex(hwbToRgb(hwb), alpha);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Convert an _HWB(A)_ color to an _RGB_ color
|
|
101
|
-
*
|
|
102
|
-
* _If the value is unable to be converted, a black _RGB_ color will be returned_
|
|
103
|
-
*
|
|
104
|
-
* @param hwb _HWB(A)_ color
|
|
105
|
-
* @returns _RGB_ color
|
|
106
|
-
*/
|
|
107
|
-
export function hwbToHsl(hwb: HWBAColor | HWBColor): HSLColor {
|
|
108
|
-
const {hue, lightness, saturation} = convertHwbToHsl(hwb);
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
hue,
|
|
112
|
-
lightness,
|
|
113
|
-
saturation,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Convert an _HWB(A)_ color to an _HSLA_ color
|
|
119
|
-
*
|
|
120
|
-
* _If the value is unable to be converted, a black _HSLA_ color will be returned_
|
|
121
|
-
*
|
|
122
|
-
* @param hwb _HWB(A)_ color
|
|
123
|
-
* @returns _HSLA_ color
|
|
124
|
-
*/
|
|
125
|
-
export function hwbToHsla(hwb: HWBAColor | HWBColor): HSLAColor {
|
|
126
|
-
return convertHwbToHsl(hwb);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Convert an _HWB(A)_ color to an _RGB_ color
|
|
131
|
-
*
|
|
132
|
-
* _If the value is unable to be converted, a black _RGB_ color will be returned_
|
|
133
|
-
*
|
|
134
|
-
* @param hwb _HWB(A)_ color
|
|
135
|
-
* @returns _RGB_ color
|
|
136
|
-
*/
|
|
137
|
-
export function hwbToRgb(hwb: HWBAColor | HWBColor): RGBColor {
|
|
138
|
-
return hslToRgb(hwbToHsl(hwb));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Convert an _HWB(A)_ color to an _RGBA_ color
|
|
143
|
-
*
|
|
144
|
-
* _If the value is unable to be converted, a black _RGBA_ color will be returned_
|
|
145
|
-
*
|
|
146
|
-
* @param hwb _HWB(A)_ color
|
|
147
|
-
* @returns _RGBA_ color
|
|
148
|
-
*/
|
|
149
|
-
export function hwbToRgba(hwb: HWBAColor | HWBColor): RGBAColor {
|
|
150
|
-
return hslToRgba(hwbToHsla(hwb));
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// #endregion
|