@tbela99/css-parser 1.1.1 → 1.3.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/CHANGELOG.md +11 -0
- package/README.md +53 -6
- package/dist/index-umd-web.js +5503 -5037
- package/dist/index.cjs +5510 -5044
- package/dist/index.d.ts +148 -41
- package/dist/lib/ast/expand.js +81 -65
- package/dist/lib/ast/features/calc.js +14 -36
- package/dist/lib/ast/features/inlinecssvariables.js +6 -17
- package/dist/lib/ast/features/prefix.js +22 -19
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +17 -2
- package/dist/lib/ast/math/expression.js +74 -172
- package/dist/lib/ast/math/math.js +24 -22
- package/dist/lib/ast/minify.js +249 -199
- package/dist/lib/ast/transform/compute.js +18 -41
- package/dist/lib/ast/transform/matrix.js +36 -36
- package/dist/lib/ast/transform/minify.js +37 -59
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +13 -13
- package/dist/lib/ast/transform/scale.js +8 -8
- package/dist/lib/ast/transform/skew.js +4 -4
- package/dist/lib/ast/transform/translate.js +8 -8
- package/dist/lib/ast/transform/utils.js +80 -28
- package/dist/lib/ast/types.js +122 -2
- package/dist/lib/fs/resolve.js +1 -14
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/declaration/map.js +1 -1
- package/dist/lib/parser/declaration/set.js +3 -3
- package/dist/lib/parser/parse.js +53 -107
- package/dist/lib/parser/tokenize.js +29 -53
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/parser/utils/type.js +2 -2
- package/dist/lib/renderer/render.js +79 -194
- package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
- package/dist/lib/syntax/color/cmyk.js +104 -0
- package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
- package/dist/lib/syntax/color/color.js +581 -0
- package/dist/lib/syntax/color/hex.js +179 -0
- package/dist/lib/syntax/color/hsl.js +201 -0
- package/dist/lib/syntax/color/hwb.js +204 -0
- package/dist/lib/syntax/color/lab.js +262 -0
- package/dist/lib/syntax/color/lch.js +194 -0
- package/dist/lib/syntax/color/oklab.js +237 -0
- package/dist/lib/syntax/color/oklch.js +166 -0
- package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
- package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
- package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
- package/dist/lib/syntax/color/rgb.js +140 -0
- package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
- package/dist/lib/syntax/color/utils/components.js +46 -0
- package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
- package/dist/lib/syntax/color/utils/distance.js +40 -0
- package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
- package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
- package/dist/lib/syntax/syntax.js +78 -77
- package/dist/lib/syntax/utils.js +70 -0
- package/dist/lib/validation/at-rules/container.js +1 -1
- package/dist/lib/validation/at-rules/counter-style.js +1 -1
- package/dist/lib/validation/at-rules/custom-media.js +1 -1
- package/dist/lib/validation/at-rules/document.js +2 -2
- package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
- package/dist/lib/validation/at-rules/import.js +5 -5
- package/dist/lib/validation/at-rules/keyframes.js +3 -13
- package/dist/lib/validation/at-rules/layer.js +1 -1
- package/dist/lib/validation/at-rules/media.js +1 -1
- package/dist/lib/validation/at-rules/namespace.js +1 -1
- package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
- package/dist/lib/validation/at-rules/page.js +1 -1
- package/dist/lib/validation/at-rules/supports.js +7 -7
- package/dist/lib/validation/at-rules/when.js +1 -1
- package/dist/lib/validation/atrule.js +2 -2
- package/dist/lib/validation/config.js +0 -3
- package/dist/lib/validation/config.json.js +1 -1
- package/dist/lib/validation/parser/parse.js +8 -11
- package/dist/lib/validation/selector.js +1 -9
- package/dist/lib/validation/syntax.js +67 -137
- package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
- package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
- package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
- package/dist/lib/validation/syntaxes/family-name.js +5 -40
- package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
- package/dist/lib/validation/syntaxes/layer-name.js +1 -1
- package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
- package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
- package/dist/lib/validation/syntaxes/url.js +3 -34
- package/dist/lib/validation/utils/list.js +2 -9
- package/dist/lib/validation/utils/whitespace.js +1 -1
- package/dist/node/index.js +4 -2
- package/dist/web/index.js +4 -2
- package/package.json +4 -4
- package/.editorconfig +0 -484
- package/dist/lib/ast/transform/convert.js +0 -33
- package/dist/lib/ast/utils/utils.js +0 -104
- package/dist/lib/renderer/color/color.js +0 -654
- package/dist/lib/renderer/color/hex.js +0 -105
- package/dist/lib/renderer/color/hsl.js +0 -125
- package/dist/lib/renderer/color/hwb.js +0 -103
- package/dist/lib/renderer/color/lab.js +0 -148
- package/dist/lib/renderer/color/lch.js +0 -90
- package/dist/lib/renderer/color/oklab.js +0 -131
- package/dist/lib/renderer/color/oklch.js +0 -75
- package/dist/lib/renderer/color/rgb.js +0 -50
- package/dist/lib/renderer/color/utils/components.js +0 -34
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
- package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
- package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
- /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { COLORS_NAMES } from './utils/constants.js';
|
|
2
2
|
import { getComponents } from './utils/components.js';
|
|
3
3
|
import { color2srgbvalues, getNumber, getAngle } from './color.js';
|
|
4
|
-
import {
|
|
4
|
+
import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
|
|
5
|
+
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
5
6
|
import '../../ast/minify.js';
|
|
6
7
|
import '../../ast/walk.js';
|
|
7
8
|
import '../../parser/parse.js';
|
|
8
9
|
import '../../parser/tokenize.js';
|
|
9
10
|
import '../../parser/utils/config.js';
|
|
10
11
|
import { expandHexValue } from './hex.js';
|
|
11
|
-
import {
|
|
12
|
-
import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
|
|
12
|
+
import { lchvalues2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
|
|
13
13
|
import { getLCHComponents } from './lch.js';
|
|
14
14
|
import { getOKLCHComponents } from './oklch.js';
|
|
15
15
|
import { XYZ_to_lin_sRGB } from './xyz.js';
|
|
16
|
-
import '
|
|
16
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
17
17
|
|
|
18
18
|
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
|
|
19
19
|
// srgb-linear -> srgb
|
|
20
20
|
// 0 <= r, g, b <= 1
|
|
21
21
|
function srgbvalues(token) {
|
|
22
22
|
switch (token.kin) {
|
|
23
|
-
case
|
|
24
|
-
case
|
|
25
|
-
return
|
|
26
|
-
case
|
|
27
|
-
case
|
|
23
|
+
case ColorType.LIT:
|
|
24
|
+
case ColorType.HEX:
|
|
25
|
+
return hex2srgbvalues(token);
|
|
26
|
+
case ColorType.RGB:
|
|
27
|
+
case ColorType.RGBA:
|
|
28
28
|
return rgb2srgb(token);
|
|
29
|
-
case
|
|
30
|
-
case
|
|
29
|
+
case ColorType.HSL:
|
|
30
|
+
case ColorType.HSLA:
|
|
31
31
|
return hsl2srgb(token);
|
|
32
|
-
case
|
|
33
|
-
return
|
|
34
|
-
case
|
|
35
|
-
return
|
|
36
|
-
case
|
|
37
|
-
return
|
|
38
|
-
case
|
|
39
|
-
return
|
|
40
|
-
case
|
|
41
|
-
return
|
|
42
|
-
case
|
|
32
|
+
case ColorType.HWB:
|
|
33
|
+
return hwb2srgbvalues(token);
|
|
34
|
+
case ColorType.LAB:
|
|
35
|
+
return lab2srgbvalues(token);
|
|
36
|
+
case ColorType.LCH:
|
|
37
|
+
return lch2srgbvalues(token);
|
|
38
|
+
case ColorType.OKLAB:
|
|
39
|
+
return oklab2srgbvalues(token);
|
|
40
|
+
case ColorType.OKLCH:
|
|
41
|
+
return oklch2srgbvalues(token);
|
|
42
|
+
case ColorType.COLOR:
|
|
43
43
|
return color2srgbvalues(token);
|
|
44
44
|
}
|
|
45
45
|
return null;
|
|
@@ -47,24 +47,31 @@ function srgbvalues(token) {
|
|
|
47
47
|
function rgb2srgb(token) {
|
|
48
48
|
return getComponents(token)?.map?.((t, index) => index == 3 ? ((t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255) ?? null;
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
|
|
50
|
+
function rgb2srgbvalues(token) {
|
|
51
|
+
return getComponents(token)?.map?.((t, index) => index == 3 ? getNumber(t) : getNumber(t) / 255) ?? null;
|
|
52
|
+
}
|
|
53
|
+
function hex2srgbvalues(token) {
|
|
54
|
+
const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
|
|
52
55
|
const rgb = [];
|
|
53
56
|
for (let i = 1; i < value.length; i += 2) {
|
|
54
57
|
rgb.push(parseInt(value.slice(i, i + 2), 16) / 255);
|
|
55
58
|
}
|
|
59
|
+
if (rgb.length == 4) {
|
|
60
|
+
rgb[3] = +rgb[3].toFixed(2);
|
|
61
|
+
}
|
|
56
62
|
return rgb;
|
|
57
63
|
}
|
|
58
|
-
|
|
64
|
+
// xyz d65 input
|
|
65
|
+
function xyz2srgb(x, y, z, alpha = null) {
|
|
59
66
|
// @ts-ignore
|
|
60
|
-
return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z));
|
|
67
|
+
return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z, alpha));
|
|
61
68
|
}
|
|
62
|
-
function
|
|
69
|
+
function hwb2srgbvalues(token) {
|
|
63
70
|
const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
|
|
64
71
|
if (hue == null || white == null || black == null) {
|
|
65
72
|
return [];
|
|
66
73
|
}
|
|
67
|
-
const rgb =
|
|
74
|
+
const rgb = hslvalues2srgbvalues(hue, 1, .5);
|
|
68
75
|
for (let i = 0; i < 3; i++) {
|
|
69
76
|
rgb[i] *= (1 - white - black);
|
|
70
77
|
rgb[i] = rgb[i] + white;
|
|
@@ -79,9 +86,9 @@ function hsl2srgb(token) {
|
|
|
79
86
|
if (h == null || s == null || l == null) {
|
|
80
87
|
return null;
|
|
81
88
|
}
|
|
82
|
-
return
|
|
89
|
+
return hslvalues2srgbvalues(h, s, l, a);
|
|
83
90
|
}
|
|
84
|
-
function
|
|
91
|
+
function cmyk2srgbvalues(token) {
|
|
85
92
|
const components = getComponents(token);
|
|
86
93
|
if (components == null) {
|
|
87
94
|
return null;
|
|
@@ -107,16 +114,21 @@ function cmyk2srgb(token) {
|
|
|
107
114
|
1 - Math.min(1, m * (1 - k) + k),
|
|
108
115
|
1 - Math.min(1, y * (1 - k) + k)
|
|
109
116
|
];
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
t = token.chi[8];
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
rgb.push(getNumber(t));
|
|
117
|
+
if (components.length == 5) {
|
|
118
|
+
rgb.push(getNumber(components[4]));
|
|
116
119
|
}
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
// if (token.chi.length >= 9) {
|
|
122
|
+
//
|
|
123
|
+
// // @ts-ignore
|
|
124
|
+
// t = <NumberToken | PercentageToken>token.chi[8];
|
|
125
|
+
//
|
|
126
|
+
// // @ts-ignore
|
|
127
|
+
// rgb.push(getNumber(t));
|
|
128
|
+
// }
|
|
117
129
|
return rgb;
|
|
118
130
|
}
|
|
119
|
-
function
|
|
131
|
+
function oklab2srgbvalues(token) {
|
|
120
132
|
const [l, a, b, alpha] = getOKLABComponents(token) ?? [];
|
|
121
133
|
if (l == null || a == null || b == null) {
|
|
122
134
|
return null;
|
|
@@ -127,13 +139,13 @@ function oklab2srgb(token) {
|
|
|
127
139
|
}
|
|
128
140
|
return rgb;
|
|
129
141
|
}
|
|
130
|
-
function
|
|
142
|
+
function oklch2srgbvalues(token) {
|
|
131
143
|
const [l, c, h, alpha] = getOKLCHComponents(token) ?? [];
|
|
132
144
|
if (l == null || c == null || h == null) {
|
|
133
145
|
return null;
|
|
134
146
|
}
|
|
135
147
|
// @ts-ignore
|
|
136
|
-
const rgb = OKLab_to_sRGB(...
|
|
148
|
+
const rgb = OKLab_to_sRGB(...lchvalues2labvalues(l, c, h));
|
|
137
149
|
if (alpha != 1) {
|
|
138
150
|
rgb.push(alpha);
|
|
139
151
|
}
|
|
@@ -164,7 +176,7 @@ function hslvalues(token) {
|
|
|
164
176
|
}
|
|
165
177
|
return a == null ? { h, s, l } : { h, s, l, a };
|
|
166
178
|
}
|
|
167
|
-
function
|
|
179
|
+
function hslvalues2srgbvalues(h, s, l, a = null) {
|
|
168
180
|
let v = l <= .5 ? l * (1.0 + s) : l + s - l * s;
|
|
169
181
|
let r = l;
|
|
170
182
|
let g = l;
|
|
@@ -217,7 +229,7 @@ function hsl2srgbvalues(h, s, l, a = null) {
|
|
|
217
229
|
}
|
|
218
230
|
return values;
|
|
219
231
|
}
|
|
220
|
-
function
|
|
232
|
+
function lab2srgbvalues(token) {
|
|
221
233
|
const [l, a, b, alpha] = getLABComponents(token) ?? [];
|
|
222
234
|
if (l == null || a == null || b == null) {
|
|
223
235
|
return null;
|
|
@@ -228,9 +240,9 @@ function lab2srgb(token) {
|
|
|
228
240
|
}
|
|
229
241
|
return rgb;
|
|
230
242
|
}
|
|
231
|
-
function
|
|
243
|
+
function lch2srgbvalues(token) {
|
|
232
244
|
// @ts-ignore
|
|
233
|
-
const [l, a, b, alpha] =
|
|
245
|
+
const [l, a, b, alpha] = lchvalues2labvalues(...getLCHComponents(token));
|
|
234
246
|
if (l == null || a == null || b == null) {
|
|
235
247
|
return null;
|
|
236
248
|
}
|
|
@@ -261,7 +273,7 @@ function srgb2lsrgbvalues(r, g, b, a = null) {
|
|
|
261
273
|
}
|
|
262
274
|
return rgb;
|
|
263
275
|
}
|
|
264
|
-
function lsrgb2srgbvalues(r, g, b, alpha) {
|
|
276
|
+
function lsrgb2srgbvalues(r, g, b, alpha = null) {
|
|
265
277
|
// convert an array of linear-light sRGB values in the range 0.0-1.0
|
|
266
278
|
// to gamma corrected form
|
|
267
279
|
// https://en.wikipedia.org/wiki/SRGB
|
|
@@ -281,4 +293,4 @@ function lsrgb2srgbvalues(r, g, b, alpha) {
|
|
|
281
293
|
return rgb;
|
|
282
294
|
}
|
|
283
295
|
|
|
284
|
-
export {
|
|
296
|
+
export { cmyk2srgbvalues, hex2srgbvalues, hsl2srgb, hslvalues, hslvalues2srgbvalues, hwb2srgbvalues, lab2srgbvalues, lch2srgbvalues, lsrgb2srgbvalues, oklab2srgbvalues, oklch2srgbvalues, rgb2srgb, rgb2srgbvalues, srgb2lsrgbvalues, srgbvalues, xyz2srgb };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ColorType, EnumToken } from '../../../ast/types.js';
|
|
2
|
+
import '../../../ast/minify.js';
|
|
3
|
+
import { walkValues } from '../../../ast/walk.js';
|
|
4
|
+
import '../../../parser/parse.js';
|
|
5
|
+
import '../../../parser/tokenize.js';
|
|
6
|
+
import '../../../parser/utils/config.js';
|
|
7
|
+
import { COLORS_NAMES } from './constants.js';
|
|
8
|
+
import { expandHexValue } from '../hex.js';
|
|
9
|
+
import '../../../renderer/sourcemap/lib/encode.js';
|
|
10
|
+
|
|
11
|
+
function getComponents(token) {
|
|
12
|
+
if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
|
|
13
|
+
const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t, index) => {
|
|
16
|
+
return { typ: EnumToken.Number, val: index < 3 ? parseInt(t, 16) : parseInt(t, 16) / 255 };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const result = [];
|
|
20
|
+
for (const child of (token.chi)) {
|
|
21
|
+
if ([
|
|
22
|
+
EnumToken.LiteralTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType
|
|
23
|
+
].includes(child.typ)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (child.typ == EnumToken.FunctionTokenType) {
|
|
27
|
+
if ('var' == child.val.toLowerCase()) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
for (const { value } of walkValues(child.chi)) {
|
|
32
|
+
if (value.typ == EnumToken.FunctionTokenType && 'var' === value.val.toLowerCase()) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (child.typ == EnumToken.ColorTokenType && 'currentcolor' === child.val.toLowerCase()) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
result.push(child);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { getComponents };
|
|
@@ -4,8 +4,10 @@ import '../../../ast/walk.js';
|
|
|
4
4
|
import '../../../parser/parse.js';
|
|
5
5
|
import '../../../parser/tokenize.js';
|
|
6
6
|
import '../../../parser/utils/config.js';
|
|
7
|
-
import '
|
|
7
|
+
import '../../../renderer/sourcemap/lib/encode.js';
|
|
8
8
|
|
|
9
|
+
const colorPrecision = 6;
|
|
10
|
+
const anglePrecision = .001;
|
|
9
11
|
const colorRange = {
|
|
10
12
|
lab: {
|
|
11
13
|
l: [0, 100],
|
|
@@ -28,38 +30,9 @@ const colorRange = {
|
|
|
28
30
|
b: [0, 0.4]
|
|
29
31
|
}
|
|
30
32
|
};
|
|
31
|
-
|
|
32
|
-
// display-p3 is an alias for srgb
|
|
33
|
-
var ColorKind;
|
|
34
|
-
(function (ColorKind) {
|
|
35
|
-
ColorKind[ColorKind["SYS"] = 0] = "SYS";
|
|
36
|
-
ColorKind[ColorKind["DPSYS"] = 1] = "DPSYS";
|
|
37
|
-
ColorKind[ColorKind["LIT"] = 2] = "LIT";
|
|
38
|
-
ColorKind[ColorKind["HEX"] = 3] = "HEX";
|
|
39
|
-
ColorKind[ColorKind["RGB"] = 4] = "RGB";
|
|
40
|
-
ColorKind[ColorKind["RGBA"] = 5] = "RGBA";
|
|
41
|
-
ColorKind[ColorKind["HSL"] = 6] = "HSL";
|
|
42
|
-
ColorKind[ColorKind["HSLA"] = 7] = "HSLA";
|
|
43
|
-
ColorKind[ColorKind["HWB"] = 8] = "HWB";
|
|
44
|
-
ColorKind[ColorKind["DEVICE_CMYK"] = 9] = "DEVICE_CMYK";
|
|
45
|
-
ColorKind[ColorKind["OKLAB"] = 10] = "OKLAB";
|
|
46
|
-
ColorKind[ColorKind["OKLCH"] = 11] = "OKLCH";
|
|
47
|
-
ColorKind[ColorKind["LAB"] = 12] = "LAB";
|
|
48
|
-
ColorKind[ColorKind["LCH"] = 13] = "LCH";
|
|
49
|
-
ColorKind[ColorKind["COLOR"] = 14] = "COLOR";
|
|
50
|
-
ColorKind[ColorKind["SRGB"] = 15] = "SRGB";
|
|
51
|
-
ColorKind[ColorKind["PROPHOTO_RGB"] = 16] = "PROPHOTO_RGB";
|
|
52
|
-
ColorKind[ColorKind["A98_RGB"] = 17] = "A98_RGB";
|
|
53
|
-
ColorKind[ColorKind["REC2020"] = 18] = "REC2020";
|
|
54
|
-
ColorKind[ColorKind["DISPLAY_P3"] = 19] = "DISPLAY_P3";
|
|
55
|
-
ColorKind[ColorKind["SRGB_LINEAR"] = 20] = "SRGB_LINEAR";
|
|
56
|
-
ColorKind[ColorKind["XYZ"] = 21] = "XYZ";
|
|
57
|
-
ColorKind[ColorKind["XYZ_D50"] = 22] = "XYZ_D50";
|
|
58
|
-
ColorKind[ColorKind["XYZ_D65"] = 23] = "XYZ_D65";
|
|
59
|
-
ColorKind[ColorKind["LIGHT_DARK"] = 24] = "LIGHT_DARK";
|
|
60
|
-
ColorKind[ColorKind["COLOR_MIX"] = 25] = "COLOR_MIX";
|
|
61
|
-
})(ColorKind || (ColorKind = {}));
|
|
33
|
+
const timelineFunc = ['view', 'scroll'];
|
|
62
34
|
const generalEnclosedFunc = ['selector', 'font-tech', 'font-format', 'media', 'supports'];
|
|
35
|
+
const timingFunc = ['ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear', 'step-start', 'step-end', 'steps', 'cubic-bezier'];
|
|
63
36
|
const funcLike = [
|
|
64
37
|
EnumToken.ParensTokenType,
|
|
65
38
|
EnumToken.FunctionTokenType,
|
|
@@ -238,4 +211,4 @@ const NAMES_COLORS = Object.seal(Object.entries(COLORS_NAMES).reduce((acc, [key,
|
|
|
238
211
|
return acc;
|
|
239
212
|
}, Object.create(null)));
|
|
240
213
|
|
|
241
|
-
export { COLORS_NAMES,
|
|
214
|
+
export { COLORS_NAMES, D50, NAMES_COLORS, anglePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, deprecatedSystemColors, e, funcLike, generalEnclosedFunc, k, systemColors, timelineFunc, timingFunc };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { convertColor } from '../color.js';
|
|
2
|
+
import { getOKLABComponents } from '../oklab.js';
|
|
3
|
+
import { ColorType } from '../../../ast/types.js';
|
|
4
|
+
import '../../../ast/minify.js';
|
|
5
|
+
import '../../../ast/walk.js';
|
|
6
|
+
import '../../../parser/parse.js';
|
|
7
|
+
import '../../../parser/tokenize.js';
|
|
8
|
+
import '../../../parser/utils/config.js';
|
|
9
|
+
import './constants.js';
|
|
10
|
+
import '../../../renderer/sourcemap/lib/encode.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Calculate the distance between two okLab colors.
|
|
14
|
+
* @param okLab1
|
|
15
|
+
* @param okLab2
|
|
16
|
+
*/
|
|
17
|
+
function okLabDistance(okLab1, okLab2) {
|
|
18
|
+
return Math.sqrt(Math.pow(okLab1[0] - okLab2[0], 2) + Math.pow(okLab1[1] - okLab2[1], 2) + Math.pow(okLab1[2] - okLab2[2], 2));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Check if two colors are close.
|
|
22
|
+
* @param color1
|
|
23
|
+
* @param color2
|
|
24
|
+
* @param threshold
|
|
25
|
+
*/
|
|
26
|
+
function isOkLabClose(color1, color2, threshold = .01) {
|
|
27
|
+
color1 = convertColor(color1, ColorType.OKLAB);
|
|
28
|
+
color2 = convertColor(color2, ColorType.OKLAB);
|
|
29
|
+
if (color1 == null || color2 == null) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const okLab1 = getOKLABComponents(color1);
|
|
33
|
+
const okLab2 = getOKLABComponents(color2);
|
|
34
|
+
if (okLab1 == null || okLab2 == null) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return okLabDistance(okLab1, okLab2) < threshold;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { isOkLabClose, okLabDistance };
|
|
@@ -7,9 +7,10 @@ import '../../parser/parse.js';
|
|
|
7
7
|
import '../../parser/tokenize.js';
|
|
8
8
|
import '../../parser/utils/config.js';
|
|
9
9
|
import { srgb2lsrgbvalues } from './srgb.js';
|
|
10
|
-
import '
|
|
10
|
+
import { XYZ_D65_to_D50 } from './xyzd50.js';
|
|
11
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
11
12
|
|
|
12
|
-
function XYZ_to_lin_sRGB(x, y, z) {
|
|
13
|
+
function XYZ_to_lin_sRGB(x, y, z, alpha = null) {
|
|
13
14
|
// convert XYZ to linear-light sRGB
|
|
14
15
|
const M = [
|
|
15
16
|
[12831 / 3959, -329 / 214, -1974 / 3959],
|
|
@@ -17,7 +18,7 @@ function XYZ_to_lin_sRGB(x, y, z) {
|
|
|
17
18
|
[705 / 12673, -2585 / 12673, 705 / 667],
|
|
18
19
|
];
|
|
19
20
|
const XYZ = [x, y, z]; // convert to XYZ
|
|
20
|
-
return multiplyMatrices(M, XYZ).map((v) => v);
|
|
21
|
+
return multiplyMatrices(M, XYZ).map((v) => v).concat(alpha == null || alpha == 1 ? [] : [alpha]);
|
|
21
22
|
}
|
|
22
23
|
function XYZ_D50_to_D65(x, y, z) {
|
|
23
24
|
// Bradford chromatic adaptation from D50 to D65
|
|
@@ -29,23 +30,35 @@ function XYZ_D50_to_D65(x, y, z) {
|
|
|
29
30
|
const XYZ = [x, y, z];
|
|
30
31
|
return multiplyMatrices(M, XYZ); //.map((v: number) => v);
|
|
31
32
|
}
|
|
33
|
+
// xyz d65
|
|
32
34
|
function srgb2xyz(r, g, b, alpha) {
|
|
33
35
|
[r, g, b] = srgb2lsrgbvalues(r, g, b);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
0.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
0.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
0.
|
|
36
|
+
// xyx d65
|
|
37
|
+
let rgb = [
|
|
38
|
+
0.4123907992659595 * r +
|
|
39
|
+
0.35758433938387796 * g +
|
|
40
|
+
0.1804807884018343 * b,
|
|
41
|
+
0.21263900587151036 * r +
|
|
42
|
+
0.7151686787677559 * g +
|
|
43
|
+
0.07219231536073371 * b,
|
|
44
|
+
0.01933081871559185 * r +
|
|
45
|
+
0.11919477979462599 * g +
|
|
46
|
+
0.9505321522496606 * b
|
|
44
47
|
];
|
|
45
48
|
if (alpha != null && alpha != 1) {
|
|
46
49
|
rgb.push(alpha);
|
|
47
50
|
}
|
|
48
51
|
return rgb;
|
|
49
52
|
}
|
|
53
|
+
// xyz d50
|
|
54
|
+
function srgb2xyz_d50(r, g, b, alpha) {
|
|
55
|
+
// xyx d65
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
let rgb = XYZ_D65_to_D50(...srgb2xyz(r, g, b));
|
|
58
|
+
if (alpha != null && alpha != 1) {
|
|
59
|
+
rgb.push(alpha);
|
|
60
|
+
}
|
|
61
|
+
return rgb;
|
|
62
|
+
}
|
|
50
63
|
|
|
51
|
-
export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz };
|
|
64
|
+
export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, srgb2xyz_d50 };
|
|
@@ -8,9 +8,9 @@ import '../../parser/parse.js';
|
|
|
8
8
|
import '../../parser/tokenize.js';
|
|
9
9
|
import '../../parser/utils/config.js';
|
|
10
10
|
import { xyz2lab } from './lab.js';
|
|
11
|
-
import {
|
|
11
|
+
import { labvalues2lchvalues } from './lch.js';
|
|
12
12
|
import { XYZ_D50_to_D65 } from './xyz.js';
|
|
13
|
-
import '
|
|
13
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
14
14
|
|
|
15
15
|
/*
|
|
16
16
|
*/
|
|
@@ -18,23 +18,23 @@ function xyzd502lch(x, y, z, alpha) {
|
|
|
18
18
|
// @ts-ignore
|
|
19
19
|
const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
|
|
20
20
|
// L in range [0,100]. For use in CSS, add a percent
|
|
21
|
-
return
|
|
21
|
+
return labvalues2lchvalues(l, a, b, alpha);
|
|
22
22
|
}
|
|
23
|
-
function XYZ_D65_to_D50(x, y, z) {
|
|
23
|
+
function XYZ_D65_to_D50(x, y, z, alpha = null) {
|
|
24
24
|
// Bradford chromatic adaptation from D65 to D50
|
|
25
25
|
// The matrix below is the result of three operations:
|
|
26
26
|
// - convert from XYZ to retinal cone domain
|
|
27
27
|
// - scale components from one reference white to another
|
|
28
28
|
// - convert back to XYZ
|
|
29
29
|
// see https://github.com/LeaVerou/color.js/pull/354/files
|
|
30
|
-
|
|
30
|
+
let M = [
|
|
31
31
|
[1.0479297925449969, 0.022946870601609652, -0.05019226628920524],
|
|
32
32
|
[0.02962780877005599, 0.9904344267538799, -0.017073799063418826],
|
|
33
33
|
[-0.009243040646204504, 0.015055191490298152, 0.7518742814281371]
|
|
34
34
|
];
|
|
35
|
-
return multiplyMatrices(M, [x, y, z]);
|
|
35
|
+
return multiplyMatrices(M, [x, y, z]).concat(alpha == null || alpha == 1 ? [] : [alpha]);
|
|
36
36
|
}
|
|
37
|
-
function xyzd502srgb(x, y, z) {
|
|
37
|
+
function xyzd502srgb(x, y, z, alpha = null) {
|
|
38
38
|
// @ts-ignore
|
|
39
39
|
return lsrgb2srgbvalues(
|
|
40
40
|
/* r: */
|
|
@@ -48,7 +48,7 @@ function xyzd502srgb(x, y, z) {
|
|
|
48
48
|
/* b: */
|
|
49
49
|
x * 0.07195537988411677 -
|
|
50
50
|
y * 0.2289768264158322 +
|
|
51
|
-
1.405386058324125 * z);
|
|
51
|
+
1.405386058324125 * z, alpha);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export { XYZ_D65_to_D50, xyzd502lch, xyzd502srgb };
|