@oscarpalmer/atoms 0.190.0 → 0.192.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/beacon.d.mts +6 -44
- package/dist/beacon.mjs +20 -67
- package/dist/color/constants.d.mts +82 -35
- package/dist/color/constants.mjs +106 -61
- package/dist/color/index.d.mts +3 -3
- package/dist/color/index.mjs +2 -2
- package/dist/color/instance.mjs +60 -35
- package/dist/color/misc/alpha.d.mts +1 -1
- package/dist/color/misc/alpha.mjs +12 -10
- package/dist/color/misc/format.d.mts +7 -0
- package/dist/color/misc/format.mjs +20 -0
- package/dist/color/misc/get.mjs +25 -25
- package/dist/color/misc/is.d.mts +0 -36
- package/dist/color/misc/is.mjs +22 -57
- package/dist/color/misc/state.d.mts +8 -6
- package/dist/color/misc/state.mjs +79 -88
- package/dist/color/models.d.mts +106 -6
- package/dist/color/space/hex.d.mts +3 -2
- package/dist/color/space/hex.mjs +22 -10
- package/dist/color/space/hsl.d.mts +3 -2
- package/dist/color/space/hsl.mjs +26 -14
- package/dist/color/space/hwb.d.mts +3 -2
- package/dist/color/space/hwb.mjs +20 -8
- package/dist/color/space/rgb.d.mts +3 -2
- package/dist/color/space/rgb.mjs +27 -15
- package/dist/herald.d.mts +2 -3
- package/dist/index.d.mts +139 -73
- package/dist/index.mjs +653 -477
- package/dist/internal/subscription.d.mts +52 -0
- package/dist/internal/subscription.mjs +127 -0
- package/dist/models.d.mts +5 -1
- package/package.json +4 -4
- package/src/beacon.ts +32 -117
- package/src/color/constants.ts +115 -66
- package/src/color/index.ts +26 -8
- package/src/color/instance.ts +113 -45
- package/src/color/misc/alpha.ts +18 -20
- package/src/color/misc/format.ts +29 -0
- package/src/color/misc/get.ts +31 -41
- package/src/color/misc/is.ts +31 -71
- package/src/color/misc/state.ts +165 -141
- package/src/color/models.ts +126 -5
- package/src/color/space/hex.ts +46 -18
- package/src/color/space/hsl.ts +44 -14
- package/src/color/space/hwb.ts +38 -8
- package/src/color/space/rgb.ts +48 -15
- package/src/herald.ts +1 -3
- package/src/index.ts +1 -0
- package/src/internal/subscription.ts +264 -0
- package/src/models.ts +5 -0
- package/src/string/fuzzy.ts +1 -1
- package/src/value/freeze.ts +2 -2
- package/src/value/shake.ts +1 -1
- package/src/value/transform.ts +1 -1
- package/dist/color/misc/index.d.mts +0 -7
- package/dist/color/misc/index.mjs +0 -15
- package/src/color/misc/index.ts +0 -29
package/src/color/index.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import {color} from './instance';
|
|
2
2
|
import {isColor} from './misc/is';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
Color,
|
|
5
|
+
ColorType,
|
|
6
|
+
ColorTypeExtended,
|
|
7
|
+
ColorValue,
|
|
8
|
+
ColorValueExtended,
|
|
9
|
+
HSLAColor,
|
|
10
|
+
HSLColor,
|
|
11
|
+
HWBAColor,
|
|
12
|
+
HWBColor,
|
|
13
|
+
RGBAColor,
|
|
14
|
+
RGBColor,
|
|
15
|
+
} from './models';
|
|
4
16
|
|
|
5
17
|
// #region Functions
|
|
6
18
|
|
|
@@ -32,22 +44,16 @@ export {
|
|
|
32
44
|
getRgbaColor,
|
|
33
45
|
getRgbColor,
|
|
34
46
|
} from './misc/get';
|
|
35
|
-
|
|
36
47
|
export {
|
|
37
48
|
isColor,
|
|
38
49
|
isHexColor,
|
|
39
50
|
isHslaColor,
|
|
40
51
|
isHslColor,
|
|
41
|
-
isHslLike,
|
|
42
52
|
isHwbaColor,
|
|
43
|
-
isHwbaLike,
|
|
44
53
|
isHwbColor,
|
|
45
|
-
isHwbLike,
|
|
46
54
|
isRgbaColor,
|
|
47
55
|
isRgbColor,
|
|
48
|
-
isRgbLike,
|
|
49
56
|
} from './misc/is';
|
|
50
|
-
|
|
51
57
|
export {
|
|
52
58
|
getNormalizedHex,
|
|
53
59
|
hexToHsl,
|
|
@@ -60,6 +66,18 @@ export {
|
|
|
60
66
|
export {hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba} from './space/hsl';
|
|
61
67
|
export {hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba} from './space/hwb';
|
|
62
68
|
export {rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba} from './space/rgb';
|
|
63
|
-
export type {
|
|
69
|
+
export type {
|
|
70
|
+
Color,
|
|
71
|
+
ColorType,
|
|
72
|
+
ColorTypeExtended,
|
|
73
|
+
ColorValue,
|
|
74
|
+
ColorValueExtended,
|
|
75
|
+
HSLAColor,
|
|
76
|
+
HSLColor,
|
|
77
|
+
HWBAColor,
|
|
78
|
+
HWBColor,
|
|
79
|
+
RGBAColor,
|
|
80
|
+
RGBColor,
|
|
81
|
+
};
|
|
64
82
|
|
|
65
83
|
// #endregion
|
package/src/color/instance.ts
CHANGED
|
@@ -1,90 +1,158 @@
|
|
|
1
|
-
import {
|
|
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';
|
|
2
10
|
import {getAlpha} from './misc/alpha';
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
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';
|
|
5
21
|
|
|
6
22
|
// #region Types
|
|
7
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
|
+
|
|
8
38
|
export function color(value: unknown): Color {
|
|
9
39
|
const state = getColorState(value);
|
|
10
40
|
|
|
41
|
+
const subscriptions = getSubscriptions<Function>(COLOR_TYPE.all);
|
|
42
|
+
|
|
11
43
|
const instance = {
|
|
44
|
+
subscribe: (first?: unknown, second?: unknown) =>
|
|
45
|
+
subscribeToColor(subscriptions, first, second),
|
|
12
46
|
toHexString: (alpha?: boolean) =>
|
|
13
47
|
`#${alpha === true ? (instance as Color).hexa : (instance as Color).hex}`,
|
|
14
|
-
toHslString: (alpha?: boolean) => formatHslColor(state, alpha
|
|
15
|
-
toHwbString: (alpha?: boolean) => formatHwbColor(state, alpha
|
|
16
|
-
toRgbString: (alpha?: boolean) => formatRgbColor(state, alpha
|
|
48
|
+
toHslString: (alpha?: boolean) => formatHslColor(state, alpha),
|
|
49
|
+
toHwbString: (alpha?: boolean) => formatHwbColor(state, alpha),
|
|
50
|
+
toRgbString: (alpha?: boolean) => formatRgbColor(state, alpha),
|
|
17
51
|
toString: () => (instance as Color).toHexString(),
|
|
52
|
+
unsubscribe: () => clearSubscriptions(subscriptions),
|
|
18
53
|
};
|
|
19
54
|
|
|
20
55
|
Object.defineProperties(instance, {
|
|
21
|
-
|
|
56
|
+
[COLOR_PROPERTY.name]: {
|
|
22
57
|
enumerable: false,
|
|
23
|
-
|
|
58
|
+
value: true,
|
|
24
59
|
},
|
|
25
60
|
alpha: {
|
|
26
61
|
enumerable: true,
|
|
27
62
|
get: () => state.alpha.value,
|
|
28
|
-
set: (value:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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,
|
|
33
74
|
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
hsl: getProperty('hsl', state, false),
|
|
37
|
-
hsla: getProperty('hsl', state, true),
|
|
38
|
-
hwb: getProperty('hwb', state, false),
|
|
39
|
-
hwba: getProperty('hwb', state, true),
|
|
40
|
-
rgb: getProperty('rgb', state, false),
|
|
41
|
-
rgba: getProperty('rgb', state, true),
|
|
75
|
+
rgb: getProperty(COLOR_TYPE.rgb, instance as Color, state, subscriptions, false),
|
|
76
|
+
rgba: getProperty(COLOR_TYPE.rgb, instance as Color, state, subscriptions, true),
|
|
42
77
|
});
|
|
43
78
|
|
|
44
79
|
return Object.freeze(instance) as Color;
|
|
45
80
|
}
|
|
46
81
|
|
|
47
|
-
function getHex(state: ColorState, alpha: boolean): string {
|
|
48
|
-
return alpha ? `${state.hex}${state.alpha.hex}` : state.hex;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
82
|
function getProperty(
|
|
52
83
|
space: keyof typeof setters,
|
|
84
|
+
color: Color,
|
|
53
85
|
state: ColorState,
|
|
86
|
+
subscriptions: Subscriptions<Function>,
|
|
54
87
|
alpha: boolean,
|
|
55
88
|
): PropertyDescriptor {
|
|
56
|
-
const
|
|
89
|
+
const setValue = setters[space];
|
|
57
90
|
|
|
58
91
|
return {
|
|
59
92
|
enumerable: true,
|
|
60
|
-
get: () =>
|
|
61
|
-
|
|
93
|
+
get: () => getValue(space, state, alpha),
|
|
94
|
+
set: (value: unknown) => setValue(color, state, subscriptions, value, alpha),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
62
97
|
|
|
63
|
-
|
|
64
|
-
|
|
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,
|
|
65
109
|
}
|
|
110
|
+
: {...value};
|
|
111
|
+
}
|
|
66
112
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
});
|
|
78
144
|
}
|
|
79
145
|
|
|
80
146
|
// #endregion
|
|
81
147
|
|
|
82
148
|
// #region Variables
|
|
83
149
|
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
150
|
+
const colorSubscription: SubscriptionProperty = {
|
|
151
|
+
key: COLOR_PROPERTY.name,
|
|
152
|
+
value: COLOR_PROPERTY.subscription,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const setters: SetValues = {
|
|
88
156
|
hex: setHexColor,
|
|
89
157
|
hsl: setHSLColor,
|
|
90
158
|
hwb: setHWBColor,
|
package/src/color/misc/alpha.ts
CHANGED
|
@@ -1,41 +1,39 @@
|
|
|
1
1
|
import {round} from '../../internal/math/misc';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
ALPHA_NONE_HEX,
|
|
5
|
-
ALPHA_NONE_VALUE,
|
|
6
|
-
DEFAULT_ALPHA,
|
|
7
|
-
MAX_HEX,
|
|
8
|
-
MAX_PERCENT,
|
|
9
|
-
} from '../constants';
|
|
2
|
+
import {clamp} from '../../internal/number';
|
|
3
|
+
import {COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX} from '../constants';
|
|
10
4
|
import type {Alpha} from '../models';
|
|
11
5
|
|
|
12
6
|
// #region Functions
|
|
13
7
|
|
|
14
|
-
export function getAlpha(value: unknown): Alpha {
|
|
8
|
+
export function getAlpha(value: unknown, hex: boolean): Alpha {
|
|
15
9
|
if (typeof value === 'number') {
|
|
16
10
|
return getAlphaFromValue(value);
|
|
17
11
|
}
|
|
18
12
|
|
|
19
|
-
if (typeof value
|
|
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)) {
|
|
20
18
|
return {
|
|
21
19
|
hex: value,
|
|
22
|
-
value: Number.parseInt(value, 16) /
|
|
20
|
+
value: Number.parseInt(value, 16) / COLOR_MAX.hex,
|
|
23
21
|
};
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
return
|
|
24
|
+
return getAlphaFromValue(Number.parseFloat(value));
|
|
27
25
|
}
|
|
28
26
|
|
|
29
27
|
export function getAlphaHexadecimal(value: number): string {
|
|
30
|
-
if (value ===
|
|
31
|
-
return
|
|
28
|
+
if (value === COLOR_ALPHA.noneValue) {
|
|
29
|
+
return COLOR_ALPHA.noneHex;
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
if (value ===
|
|
35
|
-
return
|
|
32
|
+
if (value === COLOR_MAX.percent) {
|
|
33
|
+
return COLOR_ALPHA.fullHexLong;
|
|
36
34
|
}
|
|
37
35
|
|
|
38
|
-
return round(value *
|
|
36
|
+
return round((value / COLOR_MAX.percent) * COLOR_MAX.hex).toString(16);
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
function getAlphaFromValue(value: number): Alpha {
|
|
@@ -48,11 +46,11 @@ function getAlphaFromValue(value: number): Alpha {
|
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
export function getAlphaValue(original: number): number {
|
|
51
|
-
if (Number.isNaN(original)
|
|
52
|
-
return
|
|
49
|
+
if (Number.isNaN(original)) {
|
|
50
|
+
return COLOR_MAX.percent;
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
return original
|
|
53
|
+
return clamp(original, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
// #endregion
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
package/src/color/misc/get.ts
CHANGED
|
@@ -1,24 +1,9 @@
|
|
|
1
1
|
import {round} from '../../internal/math/misc';
|
|
2
|
-
import {clamp} from '../../number';
|
|
3
|
-
import {
|
|
4
|
-
HEX_BLACK,
|
|
5
|
-
HEX_WHITE,
|
|
6
|
-
MAX_DEGREE,
|
|
7
|
-
MAX_HEX,
|
|
8
|
-
MAX_PERCENT,
|
|
9
|
-
SRGB_LUMINANCE_BLUE,
|
|
10
|
-
SRGB_LUMINANCE_EXPONENT,
|
|
11
|
-
SRGB_LUMINANCE_GREEN,
|
|
12
|
-
SRGB_LUMINANCE_MINIMUM,
|
|
13
|
-
SRGB_LUMINANCE_MODIFIER,
|
|
14
|
-
SRGB_LUMINANCE_MULTIPLIER,
|
|
15
|
-
SRGB_LUMINANCE_OFFSET,
|
|
16
|
-
SRGB_LUMINANCE_RED,
|
|
17
|
-
SRGB_LUMINANCE_THRESHOLD,
|
|
18
|
-
} from '../constants';
|
|
2
|
+
import {clamp} from '../../internal/number';
|
|
3
|
+
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_SRGB, COLOR_TYPE} from '../constants';
|
|
19
4
|
import {color} from '../instance';
|
|
20
5
|
import type {Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor} from '../models';
|
|
21
|
-
import {getColorState} from './state';
|
|
6
|
+
import {getColorFromState, getColorState} from './state';
|
|
22
7
|
|
|
23
8
|
// #region Functions
|
|
24
9
|
|
|
@@ -56,30 +41,32 @@ export function getForegroundColor(value: unknown, hex: true): string;
|
|
|
56
41
|
export function getForegroundColor(value: unknown): Color;
|
|
57
42
|
|
|
58
43
|
export function getForegroundColor(value: unknown, hex?: unknown): string | Color {
|
|
59
|
-
const
|
|
60
|
-
const {blue, green, red} =
|
|
44
|
+
const rgb = getColorFromState(getColorState(value), COLOR_TYPE.rgb);
|
|
45
|
+
const {blue, green, red} = rgb;
|
|
61
46
|
|
|
62
|
-
const values = [blue /
|
|
47
|
+
const values = [blue / COLOR_MAX.hex, green / COLOR_MAX.hex, red / COLOR_MAX.hex];
|
|
63
48
|
const {length} = values;
|
|
64
49
|
|
|
65
50
|
for (let index = 0; index < length; index += 1) {
|
|
66
51
|
const color = values[index];
|
|
67
52
|
|
|
68
|
-
if (color <=
|
|
69
|
-
values[index] /=
|
|
53
|
+
if (color <= COLOR_SRGB.luminanceMinimum) {
|
|
54
|
+
values[index] /= COLOR_SRGB.luminanceMultiplier;
|
|
70
55
|
} else {
|
|
71
56
|
values[index] =
|
|
72
|
-
((color +
|
|
57
|
+
((color + COLOR_SRGB.luminanceOffset) / COLOR_SRGB.luminanceModifier) **
|
|
58
|
+
COLOR_SRGB.luminanceExponent;
|
|
73
59
|
}
|
|
74
60
|
}
|
|
75
61
|
|
|
76
62
|
const luminance =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
COLOR_SRGB.luminanceRed * values[2] +
|
|
64
|
+
COLOR_SRGB.luminanceGreen * values[1] +
|
|
65
|
+
COLOR_SRGB.luminanceBlue * values[0];
|
|
80
66
|
|
|
81
67
|
// Rudimentary and ureliable?; implement APCA for more reliable results?
|
|
82
|
-
const foreground =
|
|
68
|
+
const foreground =
|
|
69
|
+
luminance > COLOR_SRGB.luminanceThreshold ? COLOR_DEFAULTS.hexBlack : COLOR_DEFAULTS.hexWhite;
|
|
83
70
|
|
|
84
71
|
return hex === true ? foreground : color(foreground);
|
|
85
72
|
}
|
|
@@ -109,15 +96,15 @@ export function getHexaColor(value: unknown): string {
|
|
|
109
96
|
* @returns Hex color string
|
|
110
97
|
*/
|
|
111
98
|
export function getHexColor(value: unknown): string {
|
|
112
|
-
return getColorState(value).hex;
|
|
99
|
+
return getColorFromState(getColorState(value), COLOR_TYPE.hex);
|
|
113
100
|
}
|
|
114
101
|
|
|
115
102
|
export function getHexValue(value: unknown): number {
|
|
116
|
-
return getClampedValue(value, 0,
|
|
103
|
+
return getClampedValue(value, 0, COLOR_MAX.hex, true);
|
|
117
104
|
}
|
|
118
105
|
|
|
119
106
|
export function getDegrees(value: unknown): number {
|
|
120
|
-
return getClampedValue(value, 0,
|
|
107
|
+
return getClampedValue(value, 0, COLOR_MAX.degree);
|
|
121
108
|
}
|
|
122
109
|
|
|
123
110
|
/**
|
|
@@ -130,11 +117,12 @@ export function getDegrees(value: unknown): number {
|
|
|
130
117
|
* @returns _HSLA_ color
|
|
131
118
|
*/
|
|
132
119
|
export function getHslaColor(value: unknown): HSLAColor {
|
|
133
|
-
const
|
|
120
|
+
const state = getColorState(value);
|
|
121
|
+
const hsl = getColorFromState(state, COLOR_TYPE.hsl);
|
|
134
122
|
|
|
135
123
|
return {
|
|
136
124
|
...hsl,
|
|
137
|
-
alpha: alpha.value,
|
|
125
|
+
alpha: state.alpha.value,
|
|
138
126
|
};
|
|
139
127
|
}
|
|
140
128
|
|
|
@@ -148,7 +136,7 @@ export function getHslaColor(value: unknown): HSLAColor {
|
|
|
148
136
|
* @returns _HWB_ color
|
|
149
137
|
*/
|
|
150
138
|
export function getHwbColor(value: unknown): HWBColor {
|
|
151
|
-
return getColorState(value).hwb;
|
|
139
|
+
return getColorFromState(getColorState(value), COLOR_TYPE.hwb);
|
|
152
140
|
}
|
|
153
141
|
|
|
154
142
|
/**
|
|
@@ -161,11 +149,12 @@ export function getHwbColor(value: unknown): HWBColor {
|
|
|
161
149
|
* @returns _HWBA_ color
|
|
162
150
|
*/
|
|
163
151
|
export function getHwbaColor(value: unknown): HWBAColor {
|
|
164
|
-
const
|
|
152
|
+
const state = getColorState(value);
|
|
153
|
+
const hwb = getColorFromState(state, COLOR_TYPE.hwb);
|
|
165
154
|
|
|
166
155
|
return {
|
|
167
156
|
...hwb,
|
|
168
|
-
alpha: alpha.value,
|
|
157
|
+
alpha: state.alpha.value,
|
|
169
158
|
};
|
|
170
159
|
}
|
|
171
160
|
|
|
@@ -179,11 +168,11 @@ export function getHwbaColor(value: unknown): HWBAColor {
|
|
|
179
168
|
* @returns _HSL_ color
|
|
180
169
|
*/
|
|
181
170
|
export function getHslColor(value: unknown): HSLColor {
|
|
182
|
-
return getColorState(value).hsl;
|
|
171
|
+
return getColorFromState(getColorState(value), COLOR_TYPE.hsl);
|
|
183
172
|
}
|
|
184
173
|
|
|
185
174
|
export function getPercentage(value: unknown): number {
|
|
186
|
-
return getClampedValue(value, 0,
|
|
175
|
+
return getClampedValue(value, 0, COLOR_MAX.percent);
|
|
187
176
|
}
|
|
188
177
|
|
|
189
178
|
/**
|
|
@@ -196,11 +185,12 @@ export function getPercentage(value: unknown): number {
|
|
|
196
185
|
* @returns _RGBA_ color
|
|
197
186
|
*/
|
|
198
187
|
export function getRgbaColor(value: unknown): RGBAColor {
|
|
199
|
-
const
|
|
188
|
+
const state = getColorState(value);
|
|
189
|
+
const rgb = getColorFromState(state, COLOR_TYPE.rgb);
|
|
200
190
|
|
|
201
191
|
return {
|
|
202
192
|
...rgb,
|
|
203
|
-
alpha: alpha.value,
|
|
193
|
+
alpha: state.alpha.value,
|
|
204
194
|
};
|
|
205
195
|
}
|
|
206
196
|
|
|
@@ -214,7 +204,7 @@ export function getRgbaColor(value: unknown): RGBAColor {
|
|
|
214
204
|
* @returns _RGB_ color
|
|
215
205
|
*/
|
|
216
206
|
export function getRgbColor(value: unknown): RGBColor {
|
|
217
|
-
return getColorState(value).rgb;
|
|
207
|
+
return getColorFromState(getColorState(value), COLOR_TYPE.rgb);
|
|
218
208
|
}
|
|
219
209
|
|
|
220
210
|
// #endregion
|