@oscarpalmer/atoms 0.191.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 -41
- package/dist/color/constants.mjs +102 -63
- package/dist/color/instance.mjs +36 -13
- package/dist/color/misc/alpha.mjs +9 -10
- package/dist/color/misc/format.mjs +4 -4
- package/dist/color/misc/get.mjs +18 -18
- package/dist/color/misc/is.mjs +22 -21
- package/dist/color/misc/state.d.mts +6 -5
- package/dist/color/misc/state.mjs +31 -26
- package/dist/color/models.d.mts +78 -1
- package/dist/color/space/hex.mjs +12 -12
- package/dist/color/space/hsl.mjs +16 -16
- package/dist/color/space/hwb.mjs +10 -10
- package/dist/color/space/rgb.mjs +16 -16
- package/dist/herald.d.mts +2 -3
- package/dist/index.d.mts +117 -45
- package/dist/index.mjs +522 -360
- 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 +1 -1
- package/src/beacon.ts +32 -117
- package/src/color/constants.ts +115 -78
- package/src/color/instance.ts +65 -12
- package/src/color/misc/alpha.ts +13 -21
- package/src/color/misc/format.ts +4 -4
- package/src/color/misc/get.ts +23 -40
- package/src/color/misc/is.ts +29 -39
- package/src/color/misc/state.ts +77 -41
- package/src/color/models.ts +94 -1
- package/src/color/space/hex.ts +18 -23
- package/src/color/space/hsl.ts +16 -24
- package/src/color/space/hwb.ts +10 -10
- package/src/color/space/rgb.ts +19 -16
- 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/beacon.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Subscription } from "./internal/subscription.mjs";
|
|
1
2
|
//#region src/beacon.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
|
|
@@ -7,10 +8,6 @@ type Beacon<Value> = {
|
|
|
7
8
|
* Is the beacon active?
|
|
8
9
|
*/
|
|
9
10
|
get active(): boolean;
|
|
10
|
-
/**
|
|
11
|
-
* Is the beacon closed?
|
|
12
|
-
*/
|
|
13
|
-
get closed(): boolean;
|
|
14
11
|
/**
|
|
15
12
|
* The observable that can be subscribed to
|
|
16
13
|
*/
|
|
@@ -20,9 +17,9 @@ type Beacon<Value> = {
|
|
|
20
17
|
*/
|
|
21
18
|
get value(): Value;
|
|
22
19
|
/**
|
|
23
|
-
*
|
|
20
|
+
* Deactivate the beacon
|
|
24
21
|
*/
|
|
25
|
-
|
|
22
|
+
deactivate(): void;
|
|
26
23
|
/**
|
|
27
24
|
* Emit a new value
|
|
28
25
|
*
|
|
@@ -42,9 +39,6 @@ type Beacon<Value> = {
|
|
|
42
39
|
*/
|
|
43
40
|
finish(): void;
|
|
44
41
|
};
|
|
45
|
-
/**
|
|
46
|
-
* Options for Beacon
|
|
47
|
-
*/
|
|
48
42
|
type BeaconOptions<Value> = {
|
|
49
43
|
/**
|
|
50
44
|
* Method for comparing values for equality
|
|
@@ -62,13 +56,9 @@ type Observable<Value> = {
|
|
|
62
56
|
*/
|
|
63
57
|
get active(): boolean;
|
|
64
58
|
/**
|
|
65
|
-
*
|
|
66
|
-
*/
|
|
67
|
-
get closed(): boolean;
|
|
68
|
-
/**
|
|
69
|
-
* Close the observable
|
|
59
|
+
* Deactivate the observable
|
|
70
60
|
*/
|
|
71
|
-
|
|
61
|
+
deactivate(): void;
|
|
72
62
|
/**
|
|
73
63
|
* Subscribe to value changes
|
|
74
64
|
*
|
|
@@ -86,9 +76,6 @@ type Observable<Value> = {
|
|
|
86
76
|
*/
|
|
87
77
|
subscribe(observer: Observer<Value>): Subscription;
|
|
88
78
|
};
|
|
89
|
-
/**
|
|
90
|
-
* An observer receives notifications from an observable
|
|
91
|
-
*/
|
|
92
79
|
type Observer<Value> = {
|
|
93
80
|
/**
|
|
94
81
|
* Callback for when the observable is completed
|
|
@@ -103,24 +90,6 @@ type Observer<Value> = {
|
|
|
103
90
|
*/
|
|
104
91
|
next?: (value: Value) => void;
|
|
105
92
|
};
|
|
106
|
-
type Subscription = {
|
|
107
|
-
/**
|
|
108
|
-
* Is the subscription active?
|
|
109
|
-
*/
|
|
110
|
-
get active(): boolean;
|
|
111
|
-
/**
|
|
112
|
-
* Is the subscription closed?
|
|
113
|
-
*/
|
|
114
|
-
get closed(): boolean;
|
|
115
|
-
/**
|
|
116
|
-
* Close the subscription
|
|
117
|
-
*/
|
|
118
|
-
close(): void;
|
|
119
|
-
/**
|
|
120
|
-
* Unsubscribe from its observable
|
|
121
|
-
*/
|
|
122
|
-
unsubscribe(): void;
|
|
123
|
-
};
|
|
124
93
|
/**
|
|
125
94
|
* Create a new beacon
|
|
126
95
|
*
|
|
@@ -143,12 +112,5 @@ declare function isBeacon<Value = unknown>(value: unknown): value is Beacon<Valu
|
|
|
143
112
|
* @returns `true` if the value is an observable, otherwise `false`
|
|
144
113
|
*/
|
|
145
114
|
declare function isObservable<Value = unknown>(value: unknown): value is Observable<Value>;
|
|
146
|
-
/**
|
|
147
|
-
* Is the value a subscription?
|
|
148
|
-
*
|
|
149
|
-
* @param value Value to check
|
|
150
|
-
* @returns `true` if the value is a subscription, otherwise `false`
|
|
151
|
-
*/
|
|
152
|
-
declare function isSubscription(value: unknown): value is Subscription;
|
|
153
115
|
//#endregion
|
|
154
|
-
export { Beacon, BeaconOptions, Observable,
|
|
116
|
+
export { Beacon, BeaconOptions, Observable, beacon, isBeacon, isObservable };
|
package/dist/beacon.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
import { isPlainObject } from "./internal/is.mjs";
|
|
3
|
+
import { clearSubscriptions, getSubscription, getSubscriptions } from "./internal/subscription.mjs";
|
|
3
4
|
//#region src/beacon.ts
|
|
4
5
|
/**
|
|
5
6
|
* Create a new beacon
|
|
@@ -13,11 +14,11 @@ function beacon(value, options) {
|
|
|
13
14
|
value,
|
|
14
15
|
active: true,
|
|
15
16
|
observable: void 0,
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
options: getBeaconOptions(options),
|
|
18
|
+
subscriptions: getSubscriptions()
|
|
18
19
|
};
|
|
19
20
|
const instance = {
|
|
20
|
-
|
|
21
|
+
deactivate: () => {
|
|
21
22
|
finishBeacon(state, false);
|
|
22
23
|
},
|
|
23
24
|
emit: (value, finish) => {
|
|
@@ -39,10 +40,6 @@ function beacon(value, options) {
|
|
|
39
40
|
enumerable: true,
|
|
40
41
|
get: () => state.active
|
|
41
42
|
},
|
|
42
|
-
closed: {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
get: () => !state.active
|
|
45
|
-
},
|
|
46
43
|
observable: {
|
|
47
44
|
enumerable: true,
|
|
48
45
|
get: () => {
|
|
@@ -61,15 +58,9 @@ function beacon(value, options) {
|
|
|
61
58
|
function finishBeacon(state, emit) {
|
|
62
59
|
if (!state.active) return;
|
|
63
60
|
state.active = false;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const [subscription, observer] = entries[index];
|
|
68
|
-
if (emit) observer.complete?.();
|
|
69
|
-
subscription.close();
|
|
70
|
-
}
|
|
71
|
-
state.observable?.close();
|
|
72
|
-
state.observers.clear();
|
|
61
|
+
for (const [, observer] of state.subscriptions.values.to.any) if (emit) observer.complete?.();
|
|
62
|
+
clearSubscriptions(state.subscriptions);
|
|
63
|
+
state.observable?.deactivate();
|
|
73
64
|
state.observable = void 0;
|
|
74
65
|
}
|
|
75
66
|
function getBeaconOptions(input) {
|
|
@@ -115,29 +106,24 @@ function isBeaconInstance(name, value) {
|
|
|
115
106
|
function isObservable(value) {
|
|
116
107
|
return isBeaconInstance(NAME_OBSERVABLE, value);
|
|
117
108
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Is the value a subscription?
|
|
120
|
-
*
|
|
121
|
-
* @param value Value to check
|
|
122
|
-
* @returns `true` if the value is a subscription, otherwise `false`
|
|
123
|
-
*/
|
|
124
|
-
function isSubscription(value) {
|
|
125
|
-
return isBeaconInstance(NAME_SUBSCRIPTION, value);
|
|
126
|
-
}
|
|
127
109
|
function observe(beacon) {
|
|
128
110
|
const state = {
|
|
129
111
|
beacon,
|
|
130
112
|
active: beacon.active
|
|
131
113
|
};
|
|
132
114
|
const instance = {
|
|
133
|
-
|
|
115
|
+
deactivate: () => {
|
|
134
116
|
state.active = false;
|
|
135
117
|
},
|
|
136
118
|
subscribe(first, second, third) {
|
|
137
119
|
if (!beacon.active || !state.active) throw new Error(MESSAGE_OBSERVABLE);
|
|
138
120
|
const observer = getObserver(first, second, third);
|
|
139
|
-
const subscription =
|
|
140
|
-
|
|
121
|
+
const subscription = getSubscription({
|
|
122
|
+
isActive: () => beacon.active && state.active,
|
|
123
|
+
property: beaconSubscription,
|
|
124
|
+
subscriptions: beacon.subscriptions,
|
|
125
|
+
value: observer
|
|
126
|
+
});
|
|
141
127
|
observer.next?.(beacon.value);
|
|
142
128
|
return subscription;
|
|
143
129
|
}
|
|
@@ -150,42 +136,6 @@ function observe(beacon) {
|
|
|
150
136
|
active: {
|
|
151
137
|
enumerable: true,
|
|
152
138
|
get: () => beacon.active && state.active
|
|
153
|
-
},
|
|
154
|
-
closed: {
|
|
155
|
-
enumerable: true,
|
|
156
|
-
get: () => !beacon.active || !state.active
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
return Object.freeze(instance);
|
|
160
|
-
}
|
|
161
|
-
function subscribe(observable) {
|
|
162
|
-
function unsubscribe() {
|
|
163
|
-
if (active) {
|
|
164
|
-
active = false;
|
|
165
|
-
observable.beacon.observers.delete(instance);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
let active = true;
|
|
169
|
-
const instance = {
|
|
170
|
-
close: () => {
|
|
171
|
-
unsubscribe();
|
|
172
|
-
},
|
|
173
|
-
unsubscribe: () => {
|
|
174
|
-
unsubscribe();
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
Object.defineProperties(instance, {
|
|
178
|
-
[KEY_BEACON]: {
|
|
179
|
-
enumerable: false,
|
|
180
|
-
value: NAME_SUBSCRIPTION
|
|
181
|
-
},
|
|
182
|
-
active: {
|
|
183
|
-
enumerable: true,
|
|
184
|
-
get: () => observable.beacon.active && observable.active && active
|
|
185
|
-
},
|
|
186
|
-
closed: {
|
|
187
|
-
enumerable: true,
|
|
188
|
-
get: () => !observable.beacon.active || !observable.active || !active
|
|
189
139
|
}
|
|
190
140
|
});
|
|
191
141
|
return Object.freeze(instance);
|
|
@@ -196,7 +146,7 @@ function update(type, state, value, finish) {
|
|
|
196
146
|
if (state.options.equal(state.value, value)) return;
|
|
197
147
|
state.value = value;
|
|
198
148
|
}
|
|
199
|
-
for (const [, observer] of state.
|
|
149
|
+
for (const [, observer] of state.subscriptions.values.to.any) observer[type]?.(value);
|
|
200
150
|
if (finish === true) finishBeacon(state, true);
|
|
201
151
|
}
|
|
202
152
|
const KEY_BEACON = "$beacon";
|
|
@@ -204,8 +154,11 @@ const MESSAGE_BEACON = "Cannot retrieve observable from a closed beacon";
|
|
|
204
154
|
const MESSAGE_OBSERVABLE = "Cannot subscribe to a closed observable";
|
|
205
155
|
const NAME_BEACON = "beacon";
|
|
206
156
|
const NAME_OBSERVABLE = "observable";
|
|
207
|
-
const NAME_SUBSCRIPTION = "subscription";
|
|
208
157
|
const TYPE_ERROR = "error";
|
|
209
158
|
const TYPE_NEXT = "next";
|
|
159
|
+
const beaconSubscription = {
|
|
160
|
+
key: KEY_BEACON,
|
|
161
|
+
value: "subscription"
|
|
162
|
+
};
|
|
210
163
|
//#endregion
|
|
211
|
-
export { beacon, isBeacon, isObservable
|
|
164
|
+
export { beacon, isBeacon, isObservable };
|
|
@@ -1,43 +1,84 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ColorTypeExtended, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "./models.mjs";
|
|
2
2
|
//#region src/color/constants.d.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
3
|
+
type ColorTypes = {
|
|
4
|
+
all: Set<'alpha' | ColorTypeExtended>;
|
|
5
|
+
hex: 'hex';
|
|
6
|
+
hexa: 'hexa';
|
|
7
|
+
hsl: 'hsl';
|
|
8
|
+
hsla: 'hsla';
|
|
9
|
+
hwb: 'hwb';
|
|
10
|
+
hwba: 'hwba';
|
|
11
|
+
rgb: 'rgb';
|
|
12
|
+
rgba: 'rgba';
|
|
13
|
+
};
|
|
14
|
+
declare const COLOR_ALPHA: {
|
|
15
|
+
fullHexShort: string;
|
|
16
|
+
fullHexLong: string;
|
|
17
|
+
name: string;
|
|
18
|
+
noneHex: string;
|
|
19
|
+
noneValue: number;
|
|
20
|
+
};
|
|
21
|
+
declare const COLOR_DEFAULTS: {
|
|
22
|
+
alpha: {
|
|
23
|
+
hex: string;
|
|
24
|
+
value: number;
|
|
25
|
+
};
|
|
26
|
+
hexBlack: string;
|
|
27
|
+
hexWhite: string;
|
|
28
|
+
hsl: {
|
|
29
|
+
hue: number;
|
|
30
|
+
saturation: number;
|
|
31
|
+
lightness: number;
|
|
32
|
+
};
|
|
33
|
+
hwb: {
|
|
34
|
+
hue: number;
|
|
35
|
+
whiteness: number;
|
|
36
|
+
blackness: number;
|
|
37
|
+
};
|
|
38
|
+
rgb: {
|
|
39
|
+
blue: number;
|
|
40
|
+
green: number;
|
|
41
|
+
red: number;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
declare const COLOR_EXPRESSION: {
|
|
45
|
+
alphaHex: RegExp;
|
|
46
|
+
hexLong: RegExp;
|
|
47
|
+
hexShort: RegExp;
|
|
48
|
+
prefix: RegExp;
|
|
49
|
+
};
|
|
50
|
+
declare const COLOR_LENGTHS: {
|
|
51
|
+
hexLong: number;
|
|
52
|
+
hexShort: number;
|
|
53
|
+
};
|
|
54
|
+
declare const COLOR_KEYS: {
|
|
55
|
+
hsl: Array<keyof HSLColor>;
|
|
56
|
+
hsla: Array<keyof HSLAColor>;
|
|
57
|
+
hwb: Array<keyof HWBColor>;
|
|
58
|
+
hwba: Array<keyof HWBAColor>;
|
|
59
|
+
rgb: Array<keyof RGBColor>;
|
|
60
|
+
rgba: Array<keyof RGBAColor>;
|
|
61
|
+
};
|
|
62
|
+
declare const COLOR_MAX: {
|
|
63
|
+
degree: number;
|
|
64
|
+
hex: number;
|
|
65
|
+
percent: number;
|
|
66
|
+
};
|
|
67
|
+
declare const COLOR_PROPERTY: {
|
|
68
|
+
name: string;
|
|
69
|
+
subscription: string;
|
|
70
|
+
};
|
|
71
|
+
declare const COLOR_TYPE: ColorTypes;
|
|
72
|
+
declare const COLOR_SRGB: {
|
|
73
|
+
luminanceBlue: number;
|
|
74
|
+
luminanceExponent: number;
|
|
75
|
+
luminanceGreen: number;
|
|
76
|
+
luminanceMinimum: number;
|
|
77
|
+
luminanceModifier: number;
|
|
78
|
+
luminanceMultiplier: number;
|
|
79
|
+
luminanceOffset: number;
|
|
80
|
+
luminanceRed: number;
|
|
81
|
+
luminanceThreshold: number;
|
|
82
|
+
};
|
|
42
83
|
//#endregion
|
|
43
|
-
export {
|
|
84
|
+
export { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_PROPERTY, COLOR_SRGB, COLOR_TYPE };
|
package/dist/color/constants.mjs
CHANGED
|
@@ -1,69 +1,108 @@
|
|
|
1
1
|
//#region src/color/constants.ts
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
value: 100
|
|
2
|
+
const COLOR_ALPHA = {
|
|
3
|
+
fullHexShort: "f",
|
|
4
|
+
fullHexLong: "ff",
|
|
5
|
+
name: "alpha",
|
|
6
|
+
noneHex: "00",
|
|
7
|
+
noneValue: 0
|
|
9
8
|
};
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const COLOR_DEFAULTS = {
|
|
10
|
+
alpha: {
|
|
11
|
+
hex: COLOR_ALPHA.fullHexLong,
|
|
12
|
+
value: 100
|
|
13
|
+
},
|
|
14
|
+
hexBlack: "000000",
|
|
15
|
+
hexWhite: "ffffff",
|
|
16
|
+
hsl: {
|
|
17
|
+
hue: 0,
|
|
18
|
+
saturation: 0,
|
|
19
|
+
lightness: 0
|
|
20
|
+
},
|
|
21
|
+
hwb: {
|
|
22
|
+
hue: 0,
|
|
23
|
+
whiteness: 0,
|
|
24
|
+
blackness: 100
|
|
25
|
+
},
|
|
26
|
+
rgb: {
|
|
27
|
+
blue: 0,
|
|
28
|
+
green: 0,
|
|
29
|
+
red: 0
|
|
30
|
+
}
|
|
14
31
|
};
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
const COLOR_EXPRESSION = {
|
|
33
|
+
alphaHex: /^([a-f0-9]{1,2})$/i,
|
|
34
|
+
hexLong: /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?$/i,
|
|
35
|
+
hexShort: /^#?([a-f0-9]{3,4})$/i,
|
|
36
|
+
prefix: /^#/
|
|
19
37
|
};
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
38
|
+
const COLOR_LENGTHS = {
|
|
39
|
+
hexLong: 6,
|
|
40
|
+
hexShort: 3
|
|
41
|
+
};
|
|
42
|
+
const COLOR_KEYS = {
|
|
43
|
+
hsl: [
|
|
44
|
+
"hue",
|
|
45
|
+
"saturation",
|
|
46
|
+
"lightness"
|
|
47
|
+
],
|
|
48
|
+
hsla: [],
|
|
49
|
+
hwb: [
|
|
50
|
+
"hue",
|
|
51
|
+
"whiteness",
|
|
52
|
+
"blackness"
|
|
53
|
+
],
|
|
54
|
+
hwba: [],
|
|
55
|
+
rgb: [
|
|
56
|
+
"red",
|
|
57
|
+
"green",
|
|
58
|
+
"blue"
|
|
59
|
+
],
|
|
60
|
+
rgba: []
|
|
61
|
+
};
|
|
62
|
+
COLOR_KEYS.hsla = [...COLOR_KEYS.hsl, COLOR_ALPHA.name];
|
|
63
|
+
COLOR_KEYS.hwba = [...COLOR_KEYS.hwb, COLOR_ALPHA.name];
|
|
64
|
+
COLOR_KEYS.rgba = [...COLOR_KEYS.rgb, COLOR_ALPHA.name];
|
|
65
|
+
const COLOR_MAX = {
|
|
66
|
+
degree: 360,
|
|
67
|
+
hex: 255,
|
|
68
|
+
percent: 100
|
|
69
|
+
};
|
|
70
|
+
const COLOR_PROPERTY = {
|
|
71
|
+
name: "$color",
|
|
72
|
+
subscription: "subscription"
|
|
73
|
+
};
|
|
74
|
+
const COLOR_TYPE = {
|
|
75
|
+
all: void 0,
|
|
76
|
+
hex: "hex",
|
|
77
|
+
hexa: "hexa",
|
|
78
|
+
hsl: "hsl",
|
|
79
|
+
hsla: "hsla",
|
|
80
|
+
hwb: "hwb",
|
|
81
|
+
hwba: "hwba",
|
|
82
|
+
rgb: "rgb",
|
|
83
|
+
rgba: "rgba"
|
|
84
|
+
};
|
|
85
|
+
COLOR_TYPE.all = /* @__PURE__ */ new Set([
|
|
86
|
+
COLOR_ALPHA.name,
|
|
87
|
+
COLOR_TYPE.hex,
|
|
88
|
+
COLOR_TYPE.hexa,
|
|
89
|
+
COLOR_TYPE.hsl,
|
|
90
|
+
COLOR_TYPE.hsla,
|
|
91
|
+
COLOR_TYPE.hwb,
|
|
92
|
+
COLOR_TYPE.hwba,
|
|
93
|
+
COLOR_TYPE.rgb,
|
|
94
|
+
COLOR_TYPE.rgba
|
|
95
|
+
]);
|
|
96
|
+
const COLOR_SRGB = {
|
|
97
|
+
luminanceBlue: .0722,
|
|
98
|
+
luminanceExponent: 2.4,
|
|
99
|
+
luminanceGreen: .7152,
|
|
100
|
+
luminanceMinimum: .03928,
|
|
101
|
+
luminanceModifier: 1.055,
|
|
102
|
+
luminanceMultiplier: 12.92,
|
|
103
|
+
luminanceOffset: .055,
|
|
104
|
+
luminanceRed: .2126,
|
|
105
|
+
luminanceThreshold: .625
|
|
24
106
|
};
|
|
25
|
-
const EXPRESSION_ALPHA_HEX = /^([a-f0-9]{1,2})$/i;
|
|
26
|
-
const EXPRESSION_HEX_LONG = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?$/i;
|
|
27
|
-
const EXPRESSION_HEX_SHORT = /^#?([a-f0-9]{3,4})$/i;
|
|
28
|
-
const EXPRESSION_PREFIX = /^#/;
|
|
29
|
-
const HEX_BLACK = "000000";
|
|
30
|
-
const HEX_WHITE = "ffffff";
|
|
31
|
-
const LENGTH_LONG = 6;
|
|
32
|
-
const LENGTH_SHORT = 3;
|
|
33
|
-
const KEYS_HSL = [
|
|
34
|
-
"hue",
|
|
35
|
-
"saturation",
|
|
36
|
-
"lightness"
|
|
37
|
-
];
|
|
38
|
-
const KEYS_HSLA = [...KEYS_HSL, "alpha"];
|
|
39
|
-
const KEYS_HWB = [
|
|
40
|
-
"hue",
|
|
41
|
-
"whiteness",
|
|
42
|
-
"blackness"
|
|
43
|
-
];
|
|
44
|
-
const KEYS_HWBA = [...KEYS_HWB, "alpha"];
|
|
45
|
-
const KEYS_RGB = [
|
|
46
|
-
"red",
|
|
47
|
-
"green",
|
|
48
|
-
"blue"
|
|
49
|
-
];
|
|
50
|
-
const KEYS_RGBA = [...KEYS_RGB, "alpha"];
|
|
51
|
-
const MAX_DEGREE = 360;
|
|
52
|
-
const MAX_HEX = 255;
|
|
53
|
-
const MAX_PERCENT = 100;
|
|
54
|
-
const PROPERTY_COLOR = "$color";
|
|
55
|
-
const TYPE_HEX = "hex";
|
|
56
|
-
const TYPE_HSL = "hsl";
|
|
57
|
-
const TYPE_HWB = "hwb";
|
|
58
|
-
const TYPE_RGB = "rgb";
|
|
59
|
-
const SRGB_LUMINANCE_BLUE = .0722;
|
|
60
|
-
const SRGB_LUMINANCE_EXPONENT = 2.4;
|
|
61
|
-
const SRGB_LUMINANCE_GREEN = .7152;
|
|
62
|
-
const SRGB_LUMINANCE_MINIMUM = .03928;
|
|
63
|
-
const SRGB_LUMINANCE_MODIFIER = 1.055;
|
|
64
|
-
const SRGB_LUMINANCE_MULTIPLIER = 12.92;
|
|
65
|
-
const SRGB_LUMINANCE_OFFSET = .055;
|
|
66
|
-
const SRGB_LUMINANCE_RED = .2126;
|
|
67
|
-
const SRGB_LUMINANCE_THRESHOLD = .625;
|
|
68
107
|
//#endregion
|
|
69
|
-
export {
|
|
108
|
+
export { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_PROPERTY, COLOR_SRGB, COLOR_TYPE };
|
package/dist/color/instance.mjs
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { clearSubscriptions, getSubscription, getSubscriptions } from "../internal/subscription.mjs";
|
|
2
|
+
import { COLOR_PROPERTY, COLOR_TYPE } from "./constants.mjs";
|
|
2
3
|
import { getAlpha } from "./misc/alpha.mjs";
|
|
3
4
|
import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
4
5
|
import { formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
|
|
5
6
|
//#region src/color/instance.ts
|
|
6
7
|
function color(value) {
|
|
7
8
|
const state = getColorState(value);
|
|
9
|
+
const subscriptions = getSubscriptions(COLOR_TYPE.all);
|
|
8
10
|
const instance = {
|
|
11
|
+
subscribe: (first, second) => subscribeToColor(subscriptions, first, second),
|
|
9
12
|
toHexString: (alpha) => `#${alpha === true ? instance.hexa : instance.hex}`,
|
|
10
13
|
toHslString: (alpha) => formatHslColor(state, alpha),
|
|
11
14
|
toHwbString: (alpha) => formatHwbColor(state, alpha),
|
|
12
15
|
toRgbString: (alpha) => formatRgbColor(state, alpha),
|
|
13
|
-
toString: () => instance.toHexString()
|
|
16
|
+
toString: () => instance.toHexString(),
|
|
17
|
+
unsubscribe: () => clearSubscriptions(subscriptions)
|
|
14
18
|
};
|
|
15
19
|
Object.defineProperties(instance, {
|
|
16
|
-
[
|
|
20
|
+
[COLOR_PROPERTY.name]: {
|
|
17
21
|
enumerable: false,
|
|
18
22
|
value: true
|
|
19
23
|
},
|
|
@@ -22,27 +26,27 @@ function color(value) {
|
|
|
22
26
|
get: () => state.alpha.value,
|
|
23
27
|
set: (value) => setAlphaValue(state, value)
|
|
24
28
|
},
|
|
25
|
-
hex: getProperty(
|
|
26
|
-
hexa: getProperty(
|
|
27
|
-
hsl: getProperty(
|
|
28
|
-
hsla: getProperty(
|
|
29
|
-
hwb: getProperty(
|
|
30
|
-
hwba: getProperty(
|
|
29
|
+
hex: getProperty(COLOR_TYPE.hex, instance, state, subscriptions, false),
|
|
30
|
+
hexa: getProperty(COLOR_TYPE.hex, instance, state, subscriptions, true),
|
|
31
|
+
hsl: getProperty(COLOR_TYPE.hsl, instance, state, subscriptions, false),
|
|
32
|
+
hsla: getProperty(COLOR_TYPE.hsl, instance, state, subscriptions, true),
|
|
33
|
+
hwb: getProperty(COLOR_TYPE.hwb, instance, state, subscriptions, false),
|
|
34
|
+
hwba: getProperty(COLOR_TYPE.hwb, instance, state, subscriptions, true),
|
|
31
35
|
origin: {
|
|
32
36
|
enumerable: true,
|
|
33
37
|
get: () => state.origin
|
|
34
38
|
},
|
|
35
|
-
rgb: getProperty(
|
|
36
|
-
rgba: getProperty(
|
|
39
|
+
rgb: getProperty(COLOR_TYPE.rgb, instance, state, subscriptions, false),
|
|
40
|
+
rgba: getProperty(COLOR_TYPE.rgb, instance, state, subscriptions, true)
|
|
37
41
|
});
|
|
38
42
|
return Object.freeze(instance);
|
|
39
43
|
}
|
|
40
|
-
function getProperty(space, state, alpha) {
|
|
44
|
+
function getProperty(space, color, state, subscriptions, alpha) {
|
|
41
45
|
const setValue = setters[space];
|
|
42
46
|
return {
|
|
43
47
|
enumerable: true,
|
|
44
48
|
get: () => getValue(space, state, alpha),
|
|
45
|
-
set: (value) => setValue(state, value, alpha)
|
|
49
|
+
set: (value) => setValue(color, state, subscriptions, value, alpha)
|
|
46
50
|
};
|
|
47
51
|
}
|
|
48
52
|
function getValue(space, state, alpha) {
|
|
@@ -56,6 +60,25 @@ function getValue(space, state, alpha) {
|
|
|
56
60
|
function setAlphaValue(state, value) {
|
|
57
61
|
if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
|
|
58
62
|
}
|
|
63
|
+
function subscribeToColor(subscriptions, first, second) {
|
|
64
|
+
let key;
|
|
65
|
+
let value;
|
|
66
|
+
if (typeof first === "string") {
|
|
67
|
+
key = first;
|
|
68
|
+
value = second;
|
|
69
|
+
} else if (typeof first === "function") value = first;
|
|
70
|
+
if (typeof value !== "function") throw new Error();
|
|
71
|
+
return getSubscription({
|
|
72
|
+
key,
|
|
73
|
+
subscriptions,
|
|
74
|
+
value,
|
|
75
|
+
property: colorSubscription
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const colorSubscription = {
|
|
79
|
+
key: COLOR_PROPERTY.name,
|
|
80
|
+
value: COLOR_PROPERTY.subscription
|
|
81
|
+
};
|
|
59
82
|
const setters = {
|
|
60
83
|
hex: setHexColor,
|
|
61
84
|
hsl: setHSLColor,
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { clamp } from "../../internal/number.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX } from "../constants.mjs";
|
|
3
3
|
import { round } from "../../internal/math/misc.mjs";
|
|
4
|
-
import "../../index.mjs";
|
|
5
4
|
//#region src/color/misc/alpha.ts
|
|
6
5
|
function getAlpha(value, hex) {
|
|
7
6
|
if (typeof value === "number") return getAlphaFromValue(value);
|
|
8
|
-
if (typeof value !== "string" || value.toLowerCase() ===
|
|
9
|
-
if (hex &&
|
|
7
|
+
if (typeof value !== "string" || value.toLowerCase() === COLOR_ALPHA.fullHexLong) return { ...COLOR_DEFAULTS.alpha };
|
|
8
|
+
if (hex && COLOR_EXPRESSION.alphaHex.test(value)) return {
|
|
10
9
|
hex: value,
|
|
11
|
-
value: Number.parseInt(value, 16) /
|
|
10
|
+
value: Number.parseInt(value, 16) / COLOR_MAX.hex
|
|
12
11
|
};
|
|
13
12
|
return getAlphaFromValue(Number.parseFloat(value));
|
|
14
13
|
}
|
|
15
14
|
function getAlphaHexadecimal(value) {
|
|
16
|
-
if (value ===
|
|
17
|
-
if (value ===
|
|
18
|
-
return round(value /
|
|
15
|
+
if (value === COLOR_ALPHA.noneValue) return COLOR_ALPHA.noneHex;
|
|
16
|
+
if (value === COLOR_MAX.percent) return COLOR_ALPHA.fullHexLong;
|
|
17
|
+
return round(value / COLOR_MAX.percent * COLOR_MAX.hex).toString(16);
|
|
19
18
|
}
|
|
20
19
|
function getAlphaFromValue(value) {
|
|
21
20
|
const alpha = getAlphaValue(value);
|
|
@@ -25,8 +24,8 @@ function getAlphaFromValue(value) {
|
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
26
|
function getAlphaValue(original) {
|
|
28
|
-
if (Number.isNaN(original)) return
|
|
29
|
-
return clamp(original,
|
|
27
|
+
if (Number.isNaN(original)) return COLOR_MAX.percent;
|
|
28
|
+
return clamp(original, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
|
|
30
29
|
}
|
|
31
30
|
//#endregion
|
|
32
31
|
export { getAlpha, getAlphaHexadecimal, getAlphaValue };
|