@tbela99/css-parser 1.1.1-alpha4 → 1.2.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 +8 -1
- package/README.md +53 -6
- package/dist/index-umd-web.js +4195 -3375
- package/dist/index.cjs +5468 -4648
- package/dist/index.d.ts +42 -31
- package/dist/lib/ast/expand.js +81 -65
- package/dist/lib/ast/features/calc.js +37 -35
- package/dist/lib/ast/features/inlinecssvariables.js +25 -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 +184 -159
- package/dist/lib/ast/math/math.js +22 -20
- package/dist/lib/ast/minify.js +249 -200
- package/dist/lib/ast/transform/compute.js +48 -38
- package/dist/lib/ast/transform/matrix.js +6 -5
- package/dist/lib/ast/transform/minify.js +31 -34
- package/dist/lib/ast/transform/utils.js +76 -16
- package/dist/lib/ast/types.js +32 -1
- 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 +1 -1
- package/dist/lib/parser/parse.js +19 -95
- package/dist/lib/parser/tokenize.js +1 -13
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/parser/utils/type.js +1 -1
- package/dist/lib/renderer/render.js +44 -168
- 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 +20 -21
- 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 +185 -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 +53 -40
- package/dist/lib/syntax/color/rgb.js +140 -0
- package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
- package/dist/lib/{renderer → syntax}/color/utils/components.js +7 -7
- package/dist/lib/{renderer → syntax}/color/utils/constants.js +7 -34
- package/dist/lib/syntax/color/utils/distance.js +30 -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 +77 -67
- 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 +1 -1
- package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
- package/dist/lib/validation/at-rules/import.js +1 -1
- package/dist/lib/validation/at-rules/keyframes.js +14 -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 +1 -1
- package/dist/lib/validation/at-rules/when.js +1 -1
- package/dist/lib/validation/atrule.js +2 -2
- package/dist/lib/validation/config.js +4 -3
- package/dist/lib/validation/config.json.js +1 -1
- package/dist/lib/validation/parser/parse.js +12 -7
- package/dist/lib/validation/selector.js +9 -8
- package/dist/lib/validation/syntax.js +171 -132
- package/dist/lib/validation/syntaxes/complex-selector-list.js +13 -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 +36 -39
- package/dist/lib/validation/syntaxes/keyframe-selector.js +14 -22
- package/dist/lib/validation/syntaxes/layer-name.js +1 -1
- package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -25
- package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
- package/dist/lib/validation/syntaxes/url.js +35 -33
- package/dist/lib/validation/utils/list.js +10 -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/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 };
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { EnumToken } from '../../../ast/types.js';
|
|
1
|
+
import { ColorType, EnumToken } from '../../../ast/types.js';
|
|
2
2
|
import '../../../ast/minify.js';
|
|
3
3
|
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 { COLORS_NAMES } from './constants.js';
|
|
8
8
|
import { expandHexValue } from '../hex.js';
|
|
9
|
-
import '
|
|
9
|
+
import '../../../renderer/sourcemap/lib/encode.js';
|
|
10
10
|
|
|
11
11
|
function getComponents(token) {
|
|
12
|
-
if (token.kin ==
|
|
13
|
-
const value = expandHexValue(token.kin ==
|
|
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
14
|
// @ts-ignore
|
|
15
|
-
return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t) => {
|
|
16
|
-
return { typ: EnumToken.Number, val: parseInt(t, 16).toString() };
|
|
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).toString() };
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
const result = [];
|
|
@@ -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,
|
|
@@ -77,7 +50,7 @@ const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
|
|
|
77
50
|
const k = Math.pow(29, 3) / Math.pow(3, 3);
|
|
78
51
|
const e = Math.pow(6, 3) / Math.pow(29, 3);
|
|
79
52
|
// color module v4
|
|
80
|
-
const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText'].map(m => m.toLowerCase()));
|
|
53
|
+
const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText', '-webkit-focus-ring-color'].map(m => m.toLowerCase()));
|
|
81
54
|
// deprecated
|
|
82
55
|
const deprecatedSystemColors = new Set(['ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder', 'InactiveCaption', 'InactiveCaptionText', 'InfoBackground', 'InfoText', 'Menu', 'MenuText', 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText'].map(t => t.toLowerCase()));
|
|
83
56
|
// name to color
|
|
@@ -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,30 @@
|
|
|
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
|
+
function okLabDistance(okLab1, okLab2) {
|
|
13
|
+
return Math.sqrt(Math.pow(okLab1[0] - okLab2[0], 2) + Math.pow(okLab1[1] - okLab2[1], 2) + Math.pow(okLab1[2] - okLab2[2], 2));
|
|
14
|
+
}
|
|
15
|
+
function isOkLabClose(color1, color2, threshold = .01) {
|
|
16
|
+
color1 = convertColor(color1, ColorType.OKLAB);
|
|
17
|
+
color2 = convertColor(color2, ColorType.OKLAB);
|
|
18
|
+
// console.error(JSON.stringify({color1, color2}, null, 1));
|
|
19
|
+
if (color1 == null || color2 == null) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const okLab1 = getOKLABComponents(color1);
|
|
23
|
+
const okLab2 = getOKLABComponents(color2);
|
|
24
|
+
if (okLab1 == null || okLab2 == null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return okLabDistance(okLab1, okLab2) < threshold;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
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 };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { EnumToken } from '../ast/types.js';
|
|
1
|
+
import { EnumToken, ColorType } from '../ast/types.js';
|
|
2
2
|
import '../ast/minify.js';
|
|
3
3
|
import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
|
|
4
4
|
import '../parser/parse.js';
|
|
5
5
|
import '../parser/tokenize.js';
|
|
6
6
|
import '../parser/utils/config.js';
|
|
7
|
-
import { COLORS_NAMES, colorsFunc, funcLike
|
|
7
|
+
import { COLORS_NAMES, colorsFunc, funcLike } from './color/utils/constants.js';
|
|
8
8
|
import { buildExpression } from '../ast/math/expression.js';
|
|
9
9
|
import '../renderer/sourcemap/lib/encode.js';
|
|
10
10
|
|
|
@@ -498,15 +498,24 @@ function isPolarColorspace(token) {
|
|
|
498
498
|
return ['hsl', 'hwb', 'lch', 'oklch'].includes(token.val);
|
|
499
499
|
}
|
|
500
500
|
function isHueInterpolationMethod(token) {
|
|
501
|
-
if (token
|
|
501
|
+
if (!Array.isArray(token)) {
|
|
502
|
+
return token.typ == EnumToken.IdenTokenType && 'hue'.localeCompare(token.val, undefined, { sensitivity: 'base' }) == 0;
|
|
503
|
+
}
|
|
504
|
+
if (token.length != 2 || token[0].typ != EnumToken.IdenTokenType || token[1].typ != EnumToken.IdenTokenType) {
|
|
502
505
|
return false;
|
|
503
506
|
}
|
|
504
|
-
return ['shorter', 'longer', 'increasing', 'decreasing'].includes(token.val);
|
|
507
|
+
return ['shorter', 'longer', 'increasing', 'decreasing'].includes(token[0].val) && 'hue'.localeCompare(token[1].val, undefined, { sensitivity: 'base' }) == 0;
|
|
505
508
|
}
|
|
506
509
|
function isIdentColor(token) {
|
|
507
|
-
return token.typ == EnumToken.ColorTokenType && [
|
|
510
|
+
return token.typ == EnumToken.ColorTokenType && [ColorType.SYS, ColorType.DPSYS, ColorType.LIT].includes(token.kin) && isIdent(token.val);
|
|
511
|
+
}
|
|
512
|
+
function isPercentageToken(token) {
|
|
513
|
+
return token.typ == EnumToken.PercentageTokenType || (token.typ == EnumToken.NumberTokenType && token.val == '0');
|
|
508
514
|
}
|
|
509
515
|
function isColor(token) {
|
|
516
|
+
if (token.typ == EnumToken.ColorTokenType) {
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
510
519
|
if (token.typ == EnumToken.IdenTokenType) {
|
|
511
520
|
// named color
|
|
512
521
|
return token.val.toLowerCase() in COLORS_NAMES;
|
|
@@ -595,79 +604,80 @@ function isColor(token) {
|
|
|
595
604
|
}
|
|
596
605
|
return true;
|
|
597
606
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
}
|
|
607
|
+
// @ts-ignore
|
|
608
|
+
else if (token.val == 'color-mix') {
|
|
609
|
+
// @ts-ignore
|
|
610
|
+
const children = token.chi.reduce((acc, t) => {
|
|
611
|
+
if (t.typ == EnumToken.CommaTokenType) {
|
|
612
|
+
acc.push([]);
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
|
|
616
|
+
acc[acc.length - 1].push(t);
|
|
609
617
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
618
|
+
}
|
|
619
|
+
return acc;
|
|
620
|
+
}, [[]]);
|
|
621
|
+
if (children.length == 3) {
|
|
622
|
+
if (children[0].length > 4 ||
|
|
623
|
+
children[0][0].typ != EnumToken.IdenTokenType ||
|
|
624
|
+
'in'.localeCompare(children[0][0].val, undefined, { sensitivity: 'base' }) != 0 ||
|
|
625
|
+
!isColorspace(children[0][1]) ||
|
|
626
|
+
(children[0].length >= 3 && !isHueInterpolationMethod(children[0].slice(2))) ||
|
|
627
|
+
children[1].length > 2 ||
|
|
628
|
+
!isColor(children[1][0]) ||
|
|
629
|
+
(children[1].length == 2 && !isPercentageToken(children[1][1])) ||
|
|
630
|
+
children[2].length > 2 ||
|
|
631
|
+
(children[2].length == 2 && !isPercentageToken(children[2][1])) ||
|
|
632
|
+
!isColor(children[2][0])) {
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
if (children[1].length == 2) {
|
|
636
|
+
if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
|
|
622
637
|
return false;
|
|
623
638
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
}
|
|
629
|
-
if (children[2].length == 2) {
|
|
630
|
-
if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
|
|
631
|
-
return false;
|
|
632
|
-
}
|
|
639
|
+
}
|
|
640
|
+
if (children[2].length == 2) {
|
|
641
|
+
if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
|
|
642
|
+
return false;
|
|
633
643
|
}
|
|
634
|
-
return true;
|
|
635
644
|
}
|
|
636
|
-
return
|
|
645
|
+
return true;
|
|
637
646
|
}
|
|
638
|
-
|
|
639
|
-
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
const keywords = ['from', 'none'];
|
|
651
|
+
// @ts-ignore
|
|
652
|
+
if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
|
|
640
653
|
// @ts-ignore
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
654
|
+
keywords.push('alpha', ...token.val.slice(-3).split(''));
|
|
655
|
+
}
|
|
656
|
+
// @ts-ignore
|
|
657
|
+
for (const v of token.chi) {
|
|
658
|
+
if (v.typ == EnumToken.CommaTokenType) {
|
|
659
|
+
isLegacySyntax = true;
|
|
644
660
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
isLegacySyntax = true;
|
|
661
|
+
if (v.typ == EnumToken.IdenTokenType) {
|
|
662
|
+
if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
|
|
663
|
+
return false;
|
|
649
664
|
}
|
|
650
|
-
if (v.
|
|
651
|
-
if (
|
|
665
|
+
if (keywords.includes(v.val)) {
|
|
666
|
+
if (isLegacySyntax) {
|
|
652
667
|
return false;
|
|
653
668
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
// @ts-ignore
|
|
659
|
-
if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
|
|
660
|
-
return false;
|
|
661
|
-
}
|
|
669
|
+
// @ts-ignore
|
|
670
|
+
if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
|
|
671
|
+
return false;
|
|
662
672
|
}
|
|
663
|
-
continue;
|
|
664
|
-
}
|
|
665
|
-
if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
|
|
666
|
-
continue;
|
|
667
|
-
}
|
|
668
|
-
if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
|
|
669
|
-
return false;
|
|
670
673
|
}
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
|
|
680
|
+
return false;
|
|
671
681
|
}
|
|
672
682
|
}
|
|
673
683
|
}
|
|
@@ -680,7 +690,7 @@ function parseColor(token) {
|
|
|
680
690
|
// @ts-ignore
|
|
681
691
|
token.typ = EnumToken.ColorTokenType;
|
|
682
692
|
// @ts-ignore
|
|
683
|
-
token.kin =
|
|
693
|
+
token.kin = ColorType[token.val.replaceAll('-', '_').toUpperCase()];
|
|
684
694
|
// @ts-ignore
|
|
685
695
|
if (token.chi[0].typ == EnumToken.IdenTokenType) {
|
|
686
696
|
// @ts-ignore
|
|
@@ -947,4 +957,4 @@ function isWhiteSpace(codepoint) {
|
|
|
947
957
|
codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
|
|
948
958
|
}
|
|
949
959
|
|
|
950
|
-
export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseColor, parseDimension, pseudoAliasMap, pseudoElements, transformFunctions, webkitExtensions, wildCardFuncs };
|
|
960
|
+
export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPercentageToken, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseColor, parseDimension, pseudoAliasMap, pseudoElements, transformFunctions, webkitExtensions, wildCardFuncs };
|