@thi.ng/color 5.6.2 → 5.6.4
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/alpha.js +6 -10
- package/analog.js +35 -67
- package/api/constants.js +128 -156
- package/api/gradients.js +0 -1
- package/api/names.js +154 -151
- package/api/ranges.js +0 -1
- package/api/system.js +21 -26
- package/api.js +27 -32
- package/clamp.js +18 -29
- package/closest-hue.js +6 -12
- package/color-range.js +159 -228
- package/color.js +29 -28
- package/contrast.js +7 -17
- package/convert.js +19 -40
- package/cosine-gradients.js +177 -229
- package/css/css.js +27 -53
- package/css/parse-css.js +94 -160
- package/defcolor.js +97 -108
- package/distance.js +96 -187
- package/gradients.js +29 -37
- package/hcy/hcy-rgb.js +20 -13
- package/hcy/hcy.js +9 -11
- package/hsi/hsi-rgb.js +32 -30
- package/hsi/hsi.js +9 -11
- package/hsl/hsl-css.js +9 -10
- package/hsl/hsl-hsv.js +12 -9
- package/hsl/hsl-rgb.js +14 -6
- package/hsl/hsl.js +14 -16
- package/hsv/hsv-css.js +4 -1
- package/hsv/hsv-hsl.js +11 -8
- package/hsv/hsv-rgb.js +14 -6
- package/hsv/hsv.js +14 -16
- package/hue.js +12 -12
- package/int/int-css.js +10 -6
- package/int/int-int.js +6 -10
- package/int/int-rgb.js +16 -5
- package/int/int-srgb.js +22 -4
- package/int/int.js +136 -142
- package/internal/css.js +16 -12
- package/internal/dispatch.js +6 -2
- package/internal/ensure.js +18 -16
- package/internal/matrix-ops.js +39 -41
- package/internal/scale.js +4 -2
- package/invert.js +46 -39
- package/is-black.js +12 -5
- package/is-gamut.js +8 -13
- package/is-gray.js +12 -5
- package/is-white.js +14 -7
- package/lab/lab-css.js +4 -9
- package/lab/lab-lab.js +6 -2
- package/lab/lab-lch.js +18 -10
- package/lab/lab-rgb.js +6 -14
- package/lab/lab-xyz.js +16 -20
- package/lab/lab50.js +18 -15
- package/lab/lab65.js +18 -15
- package/lch/lch-css.js +4 -9
- package/lch/lch.js +18 -23
- package/lighten.js +13 -17
- package/linear.js +6 -16
- package/luminance-rgb.js +14 -22
- package/luminance.js +16 -14
- package/max-chroma.js +1340 -227
- package/mix.js +34 -82
- package/oklab/oklab-css.js +4 -9
- package/oklab/oklab-rgb.js +17 -23
- package/oklab/oklab-xyz.js +22 -9
- package/oklab/oklab.js +19 -22
- package/oklch/oklab-oklch.js +10 -11
- package/oklch/oklch-css.js +4 -9
- package/oklch/oklch-oklab.js +9 -9
- package/oklch/oklch.js +14 -20
- package/package.json +19 -17
- package/rgb/hue-rgb.js +14 -11
- package/rgb/kelvin-rgba.js +23 -22
- package/rgb/rgb-css.js +4 -1
- package/rgb/rgb-hcv.js +14 -15
- package/rgb/rgb-hcy.js +10 -14
- package/rgb/rgb-hsi.js +13 -8
- package/rgb/rgb-hsl.js +8 -5
- package/rgb/rgb-hsv.js +7 -4
- package/rgb/rgb-lab.js +6 -18
- package/rgb/rgb-oklab.js +13 -19
- package/rgb/rgb-srgb.js +18 -8
- package/rgb/rgb-xyz.js +6 -19
- package/rgb/rgb-ycc.js +12 -17
- package/rgb/rgb.js +25 -22
- package/rotate.js +25 -38
- package/sort.js +24 -72
- package/srgb/srgb-css.js +9 -9
- package/srgb/srgb-int.js +6 -10
- package/srgb/srgb-rgb.js +18 -8
- package/srgb/srgb.js +16 -13
- package/strategies.js +42 -112
- package/swatches.js +28 -5
- package/tint.js +27 -53
- package/transform.js +274 -149
- package/variations.js +10 -9
- package/xyy/xyy-xyz.js +12 -11
- package/xyy/xyy.js +16 -13
- package/xyz/wavelength-xyz.js +14 -28
- package/xyz/xyz-lab.js +17 -26
- package/xyz/xyz-oklab.js +4 -1
- package/xyz/xyz-rgb.js +6 -18
- package/xyz/xyz-xyy.js +13 -12
- package/xyz/xyz-xyz.js +6 -2
- package/xyz/xyz50.js +20 -17
- package/xyz/xyz65.js +20 -17
- package/ycc/ycc-rgb.js +14 -19
- package/ycc/ycc.js +12 -9
package/color-range.js
CHANGED
|
@@ -14,247 +14,178 @@ import { isBlack } from "./is-black.js";
|
|
|
14
14
|
import { isGray } from "./is-gray.js";
|
|
15
15
|
import { isWhite } from "./is-white.js";
|
|
16
16
|
import { lch } from "./lch/lch.js";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
[0.2, 0.35],
|
|
82
|
-
[0.8, 1],
|
|
83
|
-
],
|
|
84
|
-
},
|
|
17
|
+
const COLOR_RANGES = {
|
|
18
|
+
light: {
|
|
19
|
+
c: [[0.3, 0.7]],
|
|
20
|
+
l: [[0.9, 1]],
|
|
21
|
+
b: [[0.35, 0.5]],
|
|
22
|
+
w: [[0.6, 1]]
|
|
23
|
+
},
|
|
24
|
+
dark: {
|
|
25
|
+
c: [[0.7, 1]],
|
|
26
|
+
l: [[0.15, 0.4]],
|
|
27
|
+
b: [[0, 0.4]],
|
|
28
|
+
w: [[0.4, 0.6]]
|
|
29
|
+
},
|
|
30
|
+
bright: {
|
|
31
|
+
c: [[0.75, 0.95]],
|
|
32
|
+
l: [[0.8, 1]]
|
|
33
|
+
},
|
|
34
|
+
weak: {
|
|
35
|
+
c: [[0.15, 0.3]],
|
|
36
|
+
l: [[0.7, 1]],
|
|
37
|
+
b: [[0.4, 0.6]],
|
|
38
|
+
w: [[0.8, 1]]
|
|
39
|
+
},
|
|
40
|
+
neutral: {
|
|
41
|
+
c: [[0.25, 0.35]],
|
|
42
|
+
l: [[0.3, 0.7]],
|
|
43
|
+
b: [[0.25, 0.4]],
|
|
44
|
+
w: [[0.9, 1]]
|
|
45
|
+
},
|
|
46
|
+
fresh: {
|
|
47
|
+
c: [[0.4, 0.8]],
|
|
48
|
+
l: [[0.8, 1]],
|
|
49
|
+
b: [[0.05, 0.3]],
|
|
50
|
+
w: [[0.8, 1]]
|
|
51
|
+
},
|
|
52
|
+
soft: {
|
|
53
|
+
c: [[0.2, 0.3]],
|
|
54
|
+
l: [[0.6, 0.9]],
|
|
55
|
+
b: [[0.05, 0.15]],
|
|
56
|
+
w: [[0.6, 0.9]]
|
|
57
|
+
},
|
|
58
|
+
hard: {
|
|
59
|
+
c: [[0.85, 0.95]],
|
|
60
|
+
l: [[0.4, 1]]
|
|
61
|
+
},
|
|
62
|
+
warm: {
|
|
63
|
+
c: [[0.6, 0.9]],
|
|
64
|
+
l: [[0.4, 0.9]],
|
|
65
|
+
b: [[0.2, 0.3]],
|
|
66
|
+
w: [[0.8, 1]]
|
|
67
|
+
},
|
|
68
|
+
cool: {
|
|
69
|
+
c: [[0.05, 0.2]],
|
|
70
|
+
l: [[0.9, 1]],
|
|
71
|
+
b: [[0, 0.95]],
|
|
72
|
+
w: [[0.95, 1]]
|
|
73
|
+
},
|
|
74
|
+
intense: {
|
|
75
|
+
c: [[0.9, 1]],
|
|
76
|
+
l: [
|
|
77
|
+
[0.2, 0.35],
|
|
78
|
+
[0.8, 1]
|
|
79
|
+
]
|
|
80
|
+
}
|
|
85
81
|
};
|
|
86
82
|
const FULL = [[0, 1]];
|
|
87
83
|
const DEFAULT_RANGE = {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
84
|
+
h: FULL,
|
|
85
|
+
c: FULL,
|
|
86
|
+
l: FULL,
|
|
87
|
+
b: FULL,
|
|
88
|
+
w: FULL,
|
|
89
|
+
a: [[1, 1]]
|
|
94
90
|
};
|
|
95
91
|
const DEFAULT_OPTS = {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
num: Infinity,
|
|
93
|
+
variance: 0.025,
|
|
94
|
+
eps: 1e-3,
|
|
95
|
+
rnd: SYSTEM
|
|
100
96
|
};
|
|
101
97
|
const $rnd = (ranges, rnd) => rnd.minmax(...ranges[rnd.int() % ranges.length]);
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const { base, variance, rnd, eps } = { ...DEFAULT_OPTS, ...opts };
|
|
128
|
-
let h;
|
|
129
|
-
let c;
|
|
130
|
-
let l;
|
|
131
|
-
let a;
|
|
132
|
-
if (base) {
|
|
133
|
-
const col = lch(base);
|
|
134
|
-
h = col[2];
|
|
135
|
-
a = __ensureAlpha(col[3]);
|
|
136
|
-
if (isBlack(col, eps)) {
|
|
137
|
-
c = 0;
|
|
138
|
-
l = $rnd(range.b, rnd);
|
|
139
|
-
}
|
|
140
|
-
else if (isWhite(col, eps)) {
|
|
141
|
-
c = 0;
|
|
142
|
-
l = $rnd(range.w, rnd);
|
|
143
|
-
}
|
|
144
|
-
else if (isGray(col, eps)) {
|
|
145
|
-
c = 0;
|
|
146
|
-
l = $rnd(coin(rnd) ? range.b : range.w, rnd);
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
h = fract(h + rnd.norm(variance));
|
|
150
|
-
}
|
|
98
|
+
const colorFromRange = (range, opts) => {
|
|
99
|
+
range = {
|
|
100
|
+
...DEFAULT_RANGE,
|
|
101
|
+
...isString(range) ? COLOR_RANGES[range] : range
|
|
102
|
+
};
|
|
103
|
+
const { base, variance, rnd, eps } = { ...DEFAULT_OPTS, ...opts };
|
|
104
|
+
let h;
|
|
105
|
+
let c;
|
|
106
|
+
let l;
|
|
107
|
+
let a;
|
|
108
|
+
if (base) {
|
|
109
|
+
const col = lch(base);
|
|
110
|
+
h = col[2];
|
|
111
|
+
a = __ensureAlpha(col[3]);
|
|
112
|
+
if (isBlack(col, eps)) {
|
|
113
|
+
c = 0;
|
|
114
|
+
l = $rnd(range.b, rnd);
|
|
115
|
+
} else if (isWhite(col, eps)) {
|
|
116
|
+
c = 0;
|
|
117
|
+
l = $rnd(range.w, rnd);
|
|
118
|
+
} else if (isGray(col, eps)) {
|
|
119
|
+
c = 0;
|
|
120
|
+
l = $rnd(coin(rnd) ? range.b : range.w, rnd);
|
|
121
|
+
} else {
|
|
122
|
+
h = fract(h + rnd.norm(variance));
|
|
151
123
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
124
|
+
} else {
|
|
125
|
+
h = $rnd(range.h, rnd);
|
|
126
|
+
a = $rnd(range.a, rnd);
|
|
127
|
+
}
|
|
128
|
+
return lch([
|
|
129
|
+
l != void 0 ? l : $rnd(range.l, rnd),
|
|
130
|
+
c !== void 0 ? c : $rnd(range.c, rnd),
|
|
131
|
+
h,
|
|
132
|
+
a
|
|
133
|
+
]);
|
|
162
134
|
};
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
* @param range -
|
|
169
|
-
* @param opts -
|
|
170
|
-
*/
|
|
171
|
-
export function* colorsFromRange(range, opts = {}) {
|
|
172
|
-
let num = opts.num != undefined ? opts.num : Infinity;
|
|
173
|
-
while (num-- > 0)
|
|
174
|
-
yield colorFromRange(range, opts);
|
|
135
|
+
function* colorsFromRange(range, opts = {}) {
|
|
136
|
+
let num = opts.num != void 0 ? opts.num : Infinity;
|
|
137
|
+
while (num-- > 0)
|
|
138
|
+
yield colorFromRange(range, opts);
|
|
175
139
|
}
|
|
176
|
-
/** @internal */
|
|
177
140
|
const compileThemePart = (part, opts) => {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
return { spec, opts };
|
|
141
|
+
let spec;
|
|
142
|
+
if (isArray(part)) {
|
|
143
|
+
spec = themePartFromTuple(part);
|
|
144
|
+
} else if (isString(part)) {
|
|
145
|
+
spec = themePartFromString(part);
|
|
146
|
+
} else {
|
|
147
|
+
spec = { ...part };
|
|
148
|
+
spec.weight == null && (spec.weight = 1);
|
|
149
|
+
}
|
|
150
|
+
isString(spec.range) && (spec.range = COLOR_RANGES[spec.range]);
|
|
151
|
+
isString(spec.base) && (spec.base = lch(parseCss(spec.base)));
|
|
152
|
+
if (spec.base !== void 0) {
|
|
153
|
+
opts = { ...opts, base: spec.base };
|
|
154
|
+
}
|
|
155
|
+
return { spec, opts };
|
|
195
156
|
};
|
|
196
|
-
/** @internal */
|
|
197
157
|
const themePartFromTuple = (part) => {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return ((xs.length === 1
|
|
208
|
-
? { range, base: xs[0], weight }
|
|
209
|
-
: xs.length === 0
|
|
210
|
-
? COLOR_RANGES[range]
|
|
211
|
-
? { range, weight }
|
|
212
|
-
: { base: range, weight }
|
|
213
|
-
: illegalArgs(`invalid theme part: "${part}"`)));
|
|
158
|
+
let weight;
|
|
159
|
+
const [range, ...xs] = part;
|
|
160
|
+
if (isNumber(peek(xs))) {
|
|
161
|
+
weight = peek(xs);
|
|
162
|
+
xs.pop();
|
|
163
|
+
} else {
|
|
164
|
+
weight = 1;
|
|
165
|
+
}
|
|
166
|
+
return xs.length === 1 ? { range, base: xs[0], weight } : xs.length === 0 ? COLOR_RANGES[range] ? { range, weight } : { base: range, weight } : illegalArgs(`invalid theme part: "${part}"`);
|
|
214
167
|
};
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
* `range` can be either a {@link ColorRange} or the name of a
|
|
231
|
-
* {@link COLOR_RANGES} preset. Likewise, `color` can be a color instance or CSS
|
|
232
|
-
* color name. The `weight` of each part defines the relative
|
|
233
|
-
* importance/probability of this theme part, compared to others. Default weight
|
|
234
|
-
* is 1.0.
|
|
235
|
-
*
|
|
236
|
-
* @example
|
|
237
|
-
* ```ts
|
|
238
|
-
* [...colorsFromTheme(
|
|
239
|
-
* [["cool", "aliceblue"], ["bright", "orange", 0.25], ["hotpink", 0.1]],
|
|
240
|
-
* { num: 10 }
|
|
241
|
-
* )]
|
|
242
|
-
* ```
|
|
243
|
-
*
|
|
244
|
-
* @param parts -
|
|
245
|
-
* @param opts -
|
|
246
|
-
*/
|
|
247
|
-
export function* colorsFromTheme(parts, opts = {}) {
|
|
248
|
-
let { num, variance, rnd } = { ...DEFAULT_OPTS, ...opts };
|
|
249
|
-
const theme = parts.map((p) => compileThemePart(p, opts));
|
|
250
|
-
const choice = weightedRandom(theme, theme.map((x) => x.spec.weight), rnd);
|
|
251
|
-
while (--num >= 0) {
|
|
252
|
-
const { spec, opts } = choice();
|
|
253
|
-
if (spec.range) {
|
|
254
|
-
yield colorFromRange(spec.range, opts);
|
|
255
|
-
}
|
|
256
|
-
else if (spec.base) {
|
|
257
|
-
yield analog(lch(), lch(spec.base), variance, rnd);
|
|
258
|
-
}
|
|
168
|
+
const themePartFromString = (part) => COLOR_RANGES[part] ? { range: part, weight: 1 } : { base: part, weight: 1 };
|
|
169
|
+
function* colorsFromTheme(parts, opts = {}) {
|
|
170
|
+
let { num, variance, rnd } = { ...DEFAULT_OPTS, ...opts };
|
|
171
|
+
const theme = parts.map((p) => compileThemePart(p, opts));
|
|
172
|
+
const choice = weightedRandom(
|
|
173
|
+
theme,
|
|
174
|
+
theme.map((x) => x.spec.weight),
|
|
175
|
+
rnd
|
|
176
|
+
);
|
|
177
|
+
while (--num >= 0) {
|
|
178
|
+
const { spec, opts: opts2 } = choice();
|
|
179
|
+
if (spec.range) {
|
|
180
|
+
yield colorFromRange(spec.range, opts2);
|
|
181
|
+
} else if (spec.base) {
|
|
182
|
+
yield analog(lch(), lch(spec.base), variance, rnd);
|
|
259
183
|
}
|
|
184
|
+
}
|
|
260
185
|
}
|
|
186
|
+
export {
|
|
187
|
+
COLOR_RANGES,
|
|
188
|
+
colorFromRange,
|
|
189
|
+
colorsFromRange,
|
|
190
|
+
colorsFromTheme
|
|
191
|
+
};
|
package/color.js
CHANGED
|
@@ -17,33 +17,34 @@ import { xyzD50 } from "./xyz/xyz50.js";
|
|
|
17
17
|
import { xyzD65 } from "./xyz/xyz65.js";
|
|
18
18
|
import { ycc } from "./ycc/ycc.js";
|
|
19
19
|
const FACTORIES = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
20
|
+
argb32,
|
|
21
|
+
abgr32,
|
|
22
|
+
hcy,
|
|
23
|
+
hsi,
|
|
24
|
+
hsl,
|
|
25
|
+
hsv,
|
|
26
|
+
lab50: labD50,
|
|
27
|
+
lab65: labD65,
|
|
28
|
+
lch,
|
|
29
|
+
oklab,
|
|
30
|
+
oklch,
|
|
31
|
+
rgb,
|
|
32
|
+
srgb,
|
|
33
|
+
xyy,
|
|
34
|
+
xyz50: xyzD50,
|
|
35
|
+
xyz65: xyzD65,
|
|
36
|
+
ycc
|
|
37
37
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
return FACTORIES[src.mode](src.deref());
|
|
38
|
+
function color(src, buf, idx, stride) {
|
|
39
|
+
if (isString(src))
|
|
40
|
+
return buf ? FACTORIES[src](buf, idx, stride) : color(parseCss(src));
|
|
41
|
+
if (buf) {
|
|
42
|
+
const res = FACTORIES[src.mode](buf, idx, stride);
|
|
43
|
+
res.set(src.deref());
|
|
44
|
+
return res;
|
|
45
|
+
}
|
|
46
|
+
return FACTORIES[src.mode](src.deref());
|
|
49
47
|
}
|
|
48
|
+
export {
|
|
49
|
+
color
|
|
50
|
+
};
|
package/contrast.js
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
import { luminanceRgb } from "./luminance-rgb.js";
|
|
2
2
|
import { rgb } from "./rgb/rgb.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* Reference: https://www.w3.org/TR/WCAG20/#contrast-ratiodef
|
|
12
|
-
*
|
|
13
|
-
* @param a
|
|
14
|
-
* @param b
|
|
15
|
-
*/
|
|
16
|
-
export const contrast = (a, b) => {
|
|
17
|
-
const lumA = luminanceRgb(rgb(a)) + 0.05;
|
|
18
|
-
const lumB = luminanceRgb(rgb(b)) + 0.05;
|
|
19
|
-
return lumA > lumB ? lumA / lumB : lumB / lumA;
|
|
3
|
+
const contrast = (a, b) => {
|
|
4
|
+
const lumA = luminanceRgb(rgb(a)) + 0.05;
|
|
5
|
+
const lumB = luminanceRgb(rgb(b)) + 0.05;
|
|
6
|
+
return lumA > lumB ? lumA / lumB : lumB / lumA;
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
contrast
|
|
20
10
|
};
|
package/convert.js
CHANGED
|
@@ -1,46 +1,25 @@
|
|
|
1
1
|
import { isArray } from "@thi.ng/checks/is-array";
|
|
2
2
|
import { assert } from "@thi.ng/errors/assert";
|
|
3
3
|
import { unsupported } from "@thi.ng/errors/unsupported";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
export const defConversions = (mode, spec) => {
|
|
14
|
-
for (let id in spec) {
|
|
15
|
-
const val = spec[id];
|
|
16
|
-
if (isArray(val)) {
|
|
17
|
-
const [a, b, c, d] = val;
|
|
18
|
-
spec[id] =
|
|
19
|
-
val.length === 2
|
|
20
|
-
? (out, src) => b(out, a(out, src))
|
|
21
|
-
: val.length === 3
|
|
22
|
-
? (out, src) => c(out, b(out, a(out, src)))
|
|
23
|
-
: (out, src) => d(out, c(out, b(out, a(out, src))));
|
|
24
|
-
}
|
|
4
|
+
const CONVERSIONS = {};
|
|
5
|
+
const defConversions = (mode, spec) => {
|
|
6
|
+
for (let id in spec) {
|
|
7
|
+
const val = spec[id];
|
|
8
|
+
if (isArray(val)) {
|
|
9
|
+
const [a, b, c, d] = val;
|
|
10
|
+
spec[id] = val.length === 2 ? (out, src) => b(out, a(out, src)) : val.length === 3 ? (out, src) => c(out, b(out, a(out, src))) : (out, src) => d(out, c(out, b(out, a(out, src))));
|
|
25
11
|
}
|
|
26
|
-
|
|
12
|
+
}
|
|
13
|
+
CONVERSIONS[mode] = { ...CONVERSIONS[mode], ...spec };
|
|
27
14
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const spec = CONVERSIONS[destMode];
|
|
39
|
-
assert(!!spec, `no conversions available for ${destMode}`);
|
|
40
|
-
let $convert = spec[srcMode];
|
|
41
|
-
return $convert
|
|
42
|
-
? $convert(res, src)
|
|
43
|
-
: CONVERSIONS.rgb[srcMode]
|
|
44
|
-
? spec.rgb(res, CONVERSIONS.rgb[srcMode]([], src))
|
|
45
|
-
: unsupported(`can't convert: ${srcMode} -> ${destMode}`);
|
|
15
|
+
const convert = (res, src, destMode, srcMode) => {
|
|
16
|
+
const spec = CONVERSIONS[destMode];
|
|
17
|
+
assert(!!spec, `no conversions available for ${destMode}`);
|
|
18
|
+
let $convert = spec[srcMode];
|
|
19
|
+
return $convert ? $convert(res, src) : CONVERSIONS.rgb[srcMode] ? spec.rgb(res, CONVERSIONS.rgb[srcMode]([], src)) : unsupported(`can't convert: ${srcMode} -> ${destMode}`);
|
|
20
|
+
};
|
|
21
|
+
export {
|
|
22
|
+
CONVERSIONS,
|
|
23
|
+
convert,
|
|
24
|
+
defConversions
|
|
46
25
|
};
|