@tbela99/css-parser 1.4.2 → 1.4.3
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/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.repl_history +4 -0
- package/CHANGELOG.md +164 -4
- package/README.md +43 -0
- package/badges/coverage.svg +20 -0
- package/deno.lock +2861 -0
- package/dist/config.json.js +33 -1
- package/dist/index-umd-web.js +24382 -18476
- package/dist/index.cjs +24522 -18618
- package/dist/index.d.ts +1396 -929
- package/dist/lib/ast/clone.d.ts +10 -0
- package/dist/lib/ast/clone.js +45 -0
- package/dist/lib/ast/expand.d.ts +14 -0
- package/dist/lib/ast/expand.js +89 -64
- package/dist/lib/ast/features/calc.d.ts +10 -0
- package/dist/lib/ast/features/calc.js +62 -24
- package/dist/lib/ast/features/if.d.ts +10 -0
- package/dist/lib/ast/features/if.js +215 -0
- package/dist/lib/ast/features/index.d.ts +6 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
- package/dist/lib/ast/features/inlinecssvariables.js +32 -27
- package/dist/lib/ast/features/prefix.d.ts +8 -0
- package/dist/lib/ast/features/prefix.js +68 -43
- package/dist/lib/ast/features/shorthand.d.ts +12 -0
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.d.ts +10 -0
- package/dist/lib/ast/features/transform.js +9 -13
- package/dist/lib/ast/features/type.d.ts +15 -0
- package/dist/lib/ast/find.d.ts +165 -0
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.d.ts +18 -0
- package/dist/lib/ast/math/expression.js +140 -98
- package/dist/lib/ast/math/math.d.ts +6 -0
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.d.ts +19 -0
- package/dist/lib/ast/minify.js +541 -217
- package/dist/lib/ast/transform/compute.d.ts +8 -0
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.d.ts +22 -0
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.d.ts +5 -0
- package/dist/lib/ast/transform/minify.js +20 -20
- package/dist/lib/ast/transform/perspective.d.ts +3 -0
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.d.ts +12 -0
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.d.ts +6 -0
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.d.ts +4 -0
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.d.ts +6 -0
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.d.ts +9 -0
- package/dist/lib/ast/types.d.ts +903 -0
- package/dist/lib/ast/types.js +277 -23
- package/dist/lib/ast/walk.d.ts +162 -0
- package/dist/lib/ast/walk.js +116 -60
- package/dist/lib/fs/resolve.d.ts +20 -0
- package/dist/lib/fs/resolve.js +37 -45
- package/dist/lib/parser/declaration/list.d.ts +16 -0
- package/dist/lib/parser/declaration/list.js +26 -24
- package/dist/lib/parser/declaration/map.d.ts +15 -0
- package/dist/lib/parser/declaration/map.js +140 -95
- package/dist/lib/parser/declaration/set.d.ts +9 -0
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/node.d.ts +7 -0
- package/dist/lib/parser/parse.d.ts +107 -0
- package/dist/lib/parser/parse.js +1454 -1445
- package/dist/lib/parser/tokenize.d.ts +57 -0
- package/dist/lib/parser/tokenize.js +557 -404
- package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-page.js +28 -0
- package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-support.js +366 -0
- package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.d.ts +13 -0
- package/dist/lib/parser/utils/at-rule.js +37 -0
- package/dist/lib/parser/utils/cache.d.ts +6 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.d.ts +2 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
- package/dist/lib/parser/utils/declaration.d.ts +18 -0
- package/dist/lib/parser/utils/declaration.js +569 -91
- package/dist/lib/parser/utils/eq.d.ts +1 -0
- package/dist/lib/parser/utils/hash.d.ts +21 -0
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.d.ts +5 -0
- package/dist/lib/parser/utils/selector.js +476 -0
- package/dist/lib/parser/utils/text.d.ts +3 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.d.ts +14 -0
- package/dist/lib/parser/utils/token.js +102 -0
- package/dist/lib/parser/utils/type.d.ts +2 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.d.ts +28 -0
- package/dist/lib/renderer/render.js +421 -262
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/cmyk.d.ts +10 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.d.ts +2 -0
- package/dist/lib/syntax/color/color-mix.js +88 -77
- package/dist/lib/syntax/color/color.d.ts +42 -0
- package/dist/lib/syntax/color/color.js +65 -68
- package/dist/lib/syntax/color/hex.d.ts +16 -0
- package/dist/lib/syntax/color/hex.js +27 -31
- package/dist/lib/syntax/color/hsl.d.ts +20 -0
- package/dist/lib/syntax/color/hsl.js +5 -12
- package/dist/lib/syntax/color/hsv.d.ts +2 -0
- package/dist/lib/syntax/color/hwb.d.ts +21 -0
- package/dist/lib/syntax/color/hwb.js +8 -21
- package/dist/lib/syntax/color/lab.d.ts +25 -0
- package/dist/lib/syntax/color/lab.js +20 -21
- package/dist/lib/syntax/color/lch.d.ts +23 -0
- package/dist/lib/syntax/color/lch.js +13 -15
- package/dist/lib/syntax/color/oklab.d.ts +22 -0
- package/dist/lib/syntax/color/oklab.js +20 -39
- package/dist/lib/syntax/color/oklch.d.ts +20 -0
- package/dist/lib/syntax/color/oklch.js +14 -16
- package/dist/lib/syntax/color/p3.d.ts +6 -0
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
- package/dist/lib/syntax/color/relativecolor.js +68 -41
- package/dist/lib/syntax/color/rgb.d.ts +20 -0
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.d.ts +23 -0
- package/dist/lib/syntax/color/srgb.js +27 -26
- package/dist/lib/syntax/color/utils/components.d.ts +2 -0
- package/dist/lib/syntax/color/utils/components.js +30 -14
- package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
- package/dist/lib/syntax/color/utils/distance.js +1 -8
- package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
- package/dist/lib/syntax/color/xyz.d.ts +5 -0
- package/dist/lib/syntax/color/xyz.js +8 -20
- package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.d.ts +67 -0
- package/dist/lib/syntax/constants.js +436 -0
- package/dist/lib/syntax/syntax.d.ts +38 -0
- package/dist/lib/syntax/syntax.js +533 -568
- package/dist/lib/validation/config.d.ts +14 -0
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1159 -74
- package/dist/lib/validation/json.d.ts +2 -0
- package/dist/lib/validation/match.d.ts +38 -0
- package/dist/lib/validation/match.js +2985 -0
- package/dist/lib/validation/parser/parse.d.ts +8 -0
- package/dist/lib/validation/parser/parse.js +684 -935
- package/dist/lib/validation/parser/typedef.d.ts +95 -0
- package/dist/lib/validation/parser/typedef.js +100 -0
- package/dist/lib/validation/utils/list.d.ts +4 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.d.ts +2 -0
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.d.ts +207 -0
- package/dist/node.js +42 -39
- package/dist/web.d.ts +169 -0
- package/dist/web.js +38 -33
- package/package.json +15 -12
- package/playground/index.html +1328 -0
- package/playground/sw.js +55 -0
- package/playground/tree.js +176 -0
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function encode(value: number | number[]): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Location, SourceMapObject } from "../../../@types/index.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Source map class
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class SourceMap {
|
|
7
|
+
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Last location
|
|
10
|
+
*/
|
|
11
|
+
lastLocation: Location | null;
|
|
12
|
+
/**
|
|
13
|
+
* Add a location
|
|
14
|
+
* @param source
|
|
15
|
+
* @param original
|
|
16
|
+
*/
|
|
17
|
+
add(source: Location, original: Location): void;
|
|
18
|
+
/**
|
|
19
|
+
* Convert to URL encoded string
|
|
20
|
+
*/
|
|
21
|
+
toUrl(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Convert to JSON object
|
|
24
|
+
*/
|
|
25
|
+
toJSON(): SourceMapObject;
|
|
26
|
+
}
|
|
@@ -23,7 +23,7 @@ class SourceMap {
|
|
|
23
23
|
* Map
|
|
24
24
|
* @private
|
|
25
25
|
*/
|
|
26
|
-
#map = new Map;
|
|
26
|
+
#map = new Map();
|
|
27
27
|
/**
|
|
28
28
|
* Line
|
|
29
29
|
* @private
|
|
@@ -35,7 +35,7 @@ class SourceMap {
|
|
|
35
35
|
* @param original
|
|
36
36
|
*/
|
|
37
37
|
add(source, original) {
|
|
38
|
-
if (original.src !==
|
|
38
|
+
if (original.src !== "") {
|
|
39
39
|
if (!this.#sources.includes(original.src)) {
|
|
40
40
|
this.#sources.push(original.src);
|
|
41
41
|
}
|
|
@@ -45,12 +45,22 @@ class SourceMap {
|
|
|
45
45
|
this.#line = line;
|
|
46
46
|
}
|
|
47
47
|
if (!this.#map.has(line)) {
|
|
48
|
-
record = [
|
|
48
|
+
record = [
|
|
49
|
+
Math.max(0, source.sta.col - 1),
|
|
50
|
+
this.#sources.indexOf(original.src),
|
|
51
|
+
original.sta.lin - 1,
|
|
52
|
+
original.sta.col - 1,
|
|
53
|
+
];
|
|
49
54
|
this.#map.set(line, [record]);
|
|
50
55
|
}
|
|
51
56
|
else {
|
|
52
57
|
const arr = this.#map.get(line);
|
|
53
|
-
record = [
|
|
58
|
+
record = [
|
|
59
|
+
Math.max(0, source.sta.col - 1 - arr[0][0]),
|
|
60
|
+
this.#sources.indexOf(original.src) - arr[0][1],
|
|
61
|
+
original.sta.lin - 1,
|
|
62
|
+
original.sta.col - 1,
|
|
63
|
+
];
|
|
54
64
|
arr.push(record);
|
|
55
65
|
}
|
|
56
66
|
if (this.lastLocation != null) {
|
|
@@ -75,16 +85,16 @@ class SourceMap {
|
|
|
75
85
|
let i = 0;
|
|
76
86
|
for (; i <= this.#line; i++) {
|
|
77
87
|
if (!this.#map.has(i)) {
|
|
78
|
-
mappings.push(
|
|
88
|
+
mappings.push("");
|
|
79
89
|
}
|
|
80
90
|
else {
|
|
81
|
-
mappings.push(this.#map.get(i).reduce((acc, curr) => acc + (acc ===
|
|
91
|
+
mappings.push(this.#map.get(i).reduce((acc, curr) => acc + (acc === "" ? "" : ",") + encode(curr), ""));
|
|
82
92
|
}
|
|
83
93
|
}
|
|
84
94
|
return {
|
|
85
95
|
version: this.#version,
|
|
86
96
|
sources: this.#sources.slice(),
|
|
87
|
-
mappings: mappings.join(
|
|
97
|
+
mappings: mappings.join(";"),
|
|
88
98
|
};
|
|
89
99
|
}
|
|
90
100
|
}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { xyz2srgb } from './srgb.js';
|
|
2
2
|
import { multiplyMatrices } from './utils/matrix.js';
|
|
3
|
-
import './utils/constants.js';
|
|
4
|
-
import '../../ast/types.js';
|
|
5
|
-
import '../../ast/minify.js';
|
|
6
|
-
import '../../ast/walk.js';
|
|
7
|
-
import '../../parser/parse.js';
|
|
8
|
-
import '../../parser/tokenize.js';
|
|
9
|
-
import '../../parser/utils/config.js';
|
|
10
3
|
import { srgb2xyz } from './xyz.js';
|
|
11
|
-
import '../../renderer/sourcemap/lib/encode.js';
|
|
12
4
|
|
|
13
5
|
function a98rgb2srgbvalues(r, g, b, a = null) {
|
|
14
6
|
// @ts-ignore
|
|
@@ -23,21 +15,25 @@ function a98rgb2la98(r, g, b, a = null) {
|
|
|
23
15
|
// convert an array of a98-rgb values in the range 0.0 - 1.0
|
|
24
16
|
// to linear light (un-companded) form.
|
|
25
17
|
// negative values are also now accepted
|
|
26
|
-
return [r, g, b]
|
|
18
|
+
return [r, g, b]
|
|
19
|
+
.map(function (val) {
|
|
27
20
|
let sign = val < 0 ? -1 : 1;
|
|
28
21
|
let abs = Math.abs(val);
|
|
29
22
|
return sign * Math.pow(abs, 563 / 256);
|
|
30
|
-
})
|
|
23
|
+
})
|
|
24
|
+
.concat(a == null || a == 1 ? [] : [a]);
|
|
31
25
|
}
|
|
32
26
|
function la98rgb2a98rgb(r, g, b, a = null) {
|
|
33
27
|
// convert an array of linear-light a98-rgb in the range 0.0-1.0
|
|
34
28
|
// to gamma corrected form
|
|
35
29
|
// negative values are also now accepted
|
|
36
|
-
return [r, b, g]
|
|
30
|
+
return [r, b, g]
|
|
31
|
+
.map(function (val) {
|
|
37
32
|
let sign = val < 0 ? -1 : 1;
|
|
38
33
|
let abs = Math.abs(val);
|
|
39
34
|
return sign * Math.pow(abs, 256 / 563);
|
|
40
|
-
})
|
|
35
|
+
})
|
|
36
|
+
.concat(a == null || a == 1 ? [] : [a]);
|
|
41
37
|
}
|
|
42
38
|
function la98rgb2xyz(r, g, b, a = null) {
|
|
43
39
|
// convert an array of linear-light a98-rgb values to CIE XYZ
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ColorToken } from "../../../@types/token.d.ts";
|
|
2
|
+
export declare function rgb2cmykToken(token: ColorToken): ColorToken | null;
|
|
3
|
+
export declare function hsl2cmykToken(token: ColorToken): ColorToken | null;
|
|
4
|
+
export declare function hwb2cmykToken(token: ColorToken): ColorToken | null;
|
|
5
|
+
export declare function lab2cmykToken(token: ColorToken): ColorToken | null;
|
|
6
|
+
export declare function lch2cmykToken(token: ColorToken): ColorToken | null;
|
|
7
|
+
export declare function oklab2cmyk(token: ColorToken): ColorToken | null;
|
|
8
|
+
export declare function oklch2cmykToken(token: ColorToken): ColorToken | null;
|
|
9
|
+
export declare function color2cmykToken(token: ColorToken): ColorToken | null;
|
|
10
|
+
export declare function srgb2cmykvalues(r: number, g: number, b: number, a?: number | null): number[];
|
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
|
-
import '../../ast/minify.js';
|
|
3
|
-
import '../../ast/walk.js';
|
|
4
|
-
import '../../parser/parse.js';
|
|
5
|
-
import '../../parser/tokenize.js';
|
|
6
|
-
import '../../parser/utils/config.js';
|
|
7
2
|
import { color2srgbvalues, toPrecisionValue } from './color.js';
|
|
8
|
-
import { hsl2srgbvalues } from './rgb.js';
|
|
9
|
-
import './utils/constants.js';
|
|
10
3
|
import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb2srgbvalues, rgb2srgbvalues } from './srgb.js';
|
|
11
|
-
import '
|
|
4
|
+
import { hsl2srgbvalues } from './rgb.js';
|
|
12
5
|
|
|
13
6
|
function rgb2cmykToken(token) {
|
|
14
7
|
const components = rgb2srgbvalues(token);
|
|
@@ -88,19 +81,28 @@ function srgb2cmykvalues(r, g, b, a = null) {
|
|
|
88
81
|
function cmyktoken(values) {
|
|
89
82
|
return {
|
|
90
83
|
typ: EnumToken.ColorTokenType,
|
|
91
|
-
val:
|
|
92
|
-
chi: values.reduce((acc, curr, index) => index < 4
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
84
|
+
val: "device-cmyk",
|
|
85
|
+
chi: values.reduce((acc, curr, index) => index < 4
|
|
86
|
+
? [
|
|
87
|
+
...acc,
|
|
88
|
+
{
|
|
89
|
+
typ: EnumToken.PercentageTokenType,
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
val: toPrecisionValue(curr) * 100,
|
|
92
|
+
},
|
|
93
|
+
]
|
|
94
|
+
: [
|
|
95
|
+
...acc,
|
|
96
|
+
{
|
|
97
|
+
typ: EnumToken.LiteralTokenType,
|
|
98
|
+
val: "/",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
typ: EnumToken.PercentageTokenType,
|
|
102
|
+
val: toPrecisionValue(curr) * 100,
|
|
103
|
+
},
|
|
104
|
+
], []),
|
|
105
|
+
kin: ColorType.DEVICE_CMYK,
|
|
104
106
|
};
|
|
105
107
|
}
|
|
106
108
|
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { ColorToken, IdentToken, NumberToken, PercentageToken } from "../../../@types/index.d.ts";
|
|
2
|
+
export declare function colorMix(colorSpace: IdentToken, hueInterpolationMethod: IdentToken | null, color1: ColorToken, percentage1: PercentageToken | NumberToken | null, color2: ColorToken, percentage2: PercentageToken | NumberToken | null): ColorToken | null;
|
|
@@ -1,31 +1,24 @@
|
|
|
1
1
|
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
2
|
-
import '../../ast/minify.js';
|
|
3
|
-
import '../../ast/walk.js';
|
|
4
|
-
import '../../parser/parse.js';
|
|
5
|
-
import '../../parser/tokenize.js';
|
|
6
|
-
import '../../parser/utils/config.js';
|
|
7
|
-
import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../syntax.js';
|
|
8
2
|
import { getNumber } from './color.js';
|
|
9
|
-
import { srgb2rgb } from './rgb.js';
|
|
10
|
-
import './utils/constants.js';
|
|
11
|
-
import { getComponents } from './utils/components.js';
|
|
12
|
-
import { srgb2oklab } from './oklab.js';
|
|
13
3
|
import { srgbvalues, srgb2lsrgbvalues } from './srgb.js';
|
|
14
|
-
import { srgb2hwb } from './hwb.js';
|
|
15
|
-
import { srgb2hslvalues } from './hsl.js';
|
|
16
4
|
import { srgb2lch, xyz2lchvalues } from './lch.js';
|
|
5
|
+
import { srgb2rgb } from './rgb.js';
|
|
6
|
+
import { srgb2hslvalues } from './hsl.js';
|
|
7
|
+
import { srgb2hwb } from './hwb.js';
|
|
17
8
|
import { srgb2labvalues } from './lab.js';
|
|
18
9
|
import { srgb2p3values } from './p3.js';
|
|
10
|
+
import { getComponents } from './utils/components.js';
|
|
19
11
|
import { srgb2oklch } from './oklch.js';
|
|
12
|
+
import { srgb2oklab } from './oklab.js';
|
|
20
13
|
import { srgb2prophotorgbvalues } from './prophotorgb.js';
|
|
21
14
|
import { srgb2xyz_d50 } from './xyz.js';
|
|
22
15
|
import { XYZ_D65_to_D50, xyzd502lch } from './xyzd50.js';
|
|
23
16
|
import { srgb2rec2020values } from './rec2020.js';
|
|
24
|
-
import '
|
|
17
|
+
import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../syntax.js';
|
|
25
18
|
|
|
26
19
|
function interpolateHue(interpolationMethod, h1, h2) {
|
|
27
20
|
switch (interpolationMethod.val) {
|
|
28
|
-
case
|
|
21
|
+
case "longer":
|
|
29
22
|
if (h2 - h1 < 180 && h2 - h1 > 0) {
|
|
30
23
|
h1 += 360;
|
|
31
24
|
}
|
|
@@ -33,17 +26,17 @@ function interpolateHue(interpolationMethod, h1, h2) {
|
|
|
33
26
|
h2 += 360;
|
|
34
27
|
}
|
|
35
28
|
break;
|
|
36
|
-
case
|
|
29
|
+
case "increasing":
|
|
37
30
|
if (h2 < h1) {
|
|
38
31
|
h2 += 360;
|
|
39
32
|
}
|
|
40
33
|
break;
|
|
41
|
-
case
|
|
34
|
+
case "decreasing":
|
|
42
35
|
if (h2 > h1) {
|
|
43
36
|
h1 += 360;
|
|
44
37
|
}
|
|
45
38
|
break;
|
|
46
|
-
case
|
|
39
|
+
case "shorter":
|
|
47
40
|
default:
|
|
48
41
|
// shorter
|
|
49
42
|
if (h2 - h1 > 180) {
|
|
@@ -57,21 +50,21 @@ function interpolateHue(interpolationMethod, h1, h2) {
|
|
|
57
50
|
return [h1, h2];
|
|
58
51
|
}
|
|
59
52
|
function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color2, percentage2) {
|
|
60
|
-
if (color1.val.toLowerCase() ==
|
|
53
|
+
if (color1.val.toLowerCase() == "currentcolor" || color2.val == "currentcolor".toLowerCase()) {
|
|
61
54
|
return null;
|
|
62
55
|
}
|
|
63
56
|
if (hueInterpolationMethod != null && isRectangularOrthogonalColorspace(colorSpace)) {
|
|
64
57
|
return null;
|
|
65
58
|
}
|
|
66
59
|
if (isPolarColorspace(colorSpace) && hueInterpolationMethod == null) {
|
|
67
|
-
hueInterpolationMethod = { typ: EnumToken.IdenTokenType, val:
|
|
60
|
+
hueInterpolationMethod = { typ: EnumToken.IdenTokenType, val: "shorter" };
|
|
68
61
|
}
|
|
69
62
|
if (percentage1 == null) {
|
|
70
63
|
if (percentage2 == null) {
|
|
71
64
|
// @ts-ignore
|
|
72
|
-
percentage1 = { typ: EnumToken.NumberTokenType, val: .5 };
|
|
65
|
+
percentage1 = { typ: EnumToken.NumberTokenType, val: 0.5 };
|
|
73
66
|
// @ts-ignore
|
|
74
|
-
percentage2 = { typ: EnumToken.NumberTokenType, val: .5 };
|
|
67
|
+
percentage2 = { typ: EnumToken.NumberTokenType, val: 0.5 };
|
|
75
68
|
}
|
|
76
69
|
else {
|
|
77
70
|
if (+percentage2.val <= 0) {
|
|
@@ -115,10 +108,16 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
115
108
|
if (components1 == null || components2 == null) {
|
|
116
109
|
return null;
|
|
117
110
|
}
|
|
118
|
-
if (
|
|
111
|
+
if (components1[3] != null &&
|
|
112
|
+
components1[3].typ == EnumToken.IdenTokenType &&
|
|
113
|
+
components1[3].val == "none" &&
|
|
114
|
+
values2.length == 4) {
|
|
119
115
|
values1[3] = values2[3];
|
|
120
116
|
}
|
|
121
|
-
if (
|
|
117
|
+
if (components2[3] != null &&
|
|
118
|
+
components2[3].typ == EnumToken.IdenTokenType &&
|
|
119
|
+
components2[3].val == "none" &&
|
|
120
|
+
values1.length == 4) {
|
|
122
121
|
values2[3] = values1[3];
|
|
123
122
|
}
|
|
124
123
|
const p1 = getNumber(percentage1);
|
|
@@ -126,98 +125,107 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
126
125
|
const mul1 = values1.length == 4 ? values1.pop() : 1;
|
|
127
126
|
const mul2 = values2.length == 4 ? values2.pop() : 1;
|
|
128
127
|
const mul = mul1 * p1 + mul2 * p2;
|
|
128
|
+
const calculate = () => [colorSpace].concat(
|
|
129
129
|
// @ts-ignore
|
|
130
|
-
|
|
130
|
+
values1
|
|
131
|
+
.map((v1, i) => {
|
|
131
132
|
return {
|
|
132
|
-
typ: EnumToken.NumberTokenType,
|
|
133
|
+
typ: EnumToken.NumberTokenType,
|
|
134
|
+
val: (mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul,
|
|
133
135
|
};
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
|
|
136
|
+
})
|
|
137
|
+
.concat(mul == 1
|
|
138
|
+
? []
|
|
139
|
+
: [
|
|
140
|
+
{
|
|
141
|
+
typ: EnumToken.NumberTokenType,
|
|
142
|
+
val: mul,
|
|
143
|
+
},
|
|
144
|
+
]));
|
|
137
145
|
switch (colorSpace.val) {
|
|
138
|
-
case
|
|
146
|
+
case "srgb":
|
|
139
147
|
break;
|
|
140
|
-
case
|
|
148
|
+
case "display-p3":
|
|
141
149
|
// @ts-ignore
|
|
142
150
|
values1 = srgb2p3values(...values1);
|
|
143
151
|
// @ts-ignore
|
|
144
152
|
values2 = srgb2p3values(...values2);
|
|
145
153
|
break;
|
|
146
|
-
case
|
|
154
|
+
case "a98-rgb":
|
|
147
155
|
// @ts-ignore
|
|
148
156
|
values1 = srgb2a98values(...values1);
|
|
149
157
|
// @ts-ignore
|
|
150
158
|
values2 = srgb2a98values(...values2);
|
|
151
159
|
break;
|
|
152
|
-
case
|
|
160
|
+
case "prophoto-rgb":
|
|
153
161
|
// @ts-ignore
|
|
154
162
|
values1 = srgb2prophotorgbvalues(...values1);
|
|
155
163
|
// @ts-ignore
|
|
156
164
|
values2 = srgb2prophotorgbvalues(...values2);
|
|
157
165
|
break;
|
|
158
|
-
case
|
|
166
|
+
case "srgb-linear":
|
|
159
167
|
// @ts-ignore
|
|
160
168
|
values1 = srgb2lsrgbvalues(...values1);
|
|
161
169
|
// @ts-ignore
|
|
162
170
|
values2 = srgb2lsrgbvalues(...values2);
|
|
163
171
|
break;
|
|
164
|
-
case
|
|
172
|
+
case "rec2020":
|
|
165
173
|
// @ts-ignore
|
|
166
174
|
values1 = srgb2rec2020values(...values1);
|
|
167
175
|
// @ts-ignore
|
|
168
176
|
values2 = srgb2rec2020values(...values2);
|
|
169
177
|
break;
|
|
170
|
-
case
|
|
171
|
-
case
|
|
172
|
-
case
|
|
178
|
+
case "xyz":
|
|
179
|
+
case "xyz-d65":
|
|
180
|
+
case "xyz-d50":
|
|
173
181
|
// @ts-ignore
|
|
174
182
|
values1 = srgb2xyz_d50(...values1);
|
|
175
183
|
// @ts-ignore
|
|
176
184
|
values2 = srgb2xyz_d50(...values2);
|
|
177
|
-
if (colorSpace.val ==
|
|
185
|
+
if (colorSpace.val == "xyz-d50") {
|
|
178
186
|
// @ts-ignore
|
|
179
187
|
values1 = XYZ_D65_to_D50(...values1);
|
|
180
188
|
// @ts-ignore
|
|
181
189
|
values2 = XYZ_D65_to_D50(...values2);
|
|
182
190
|
}
|
|
183
191
|
break;
|
|
184
|
-
case
|
|
192
|
+
case "rgb":
|
|
185
193
|
// @ts-ignore
|
|
186
194
|
values1 = srgb2rgb(...values1);
|
|
187
195
|
// @ts-ignore
|
|
188
196
|
values2 = srgb2rgb(...values2);
|
|
189
197
|
break;
|
|
190
|
-
case
|
|
198
|
+
case "hsl":
|
|
191
199
|
// @ts-ignore
|
|
192
200
|
values1 = srgb2hslvalues(...values1);
|
|
193
201
|
// @ts-ignore
|
|
194
202
|
values2 = srgb2hslvalues(...values2);
|
|
195
203
|
break;
|
|
196
|
-
case
|
|
204
|
+
case "hwb":
|
|
197
205
|
// @ts-ignore
|
|
198
206
|
values1 = srgb2hwb(...values1);
|
|
199
207
|
// @ts-ignore
|
|
200
208
|
values2 = srgb2hwb(...values2);
|
|
201
209
|
break;
|
|
202
|
-
case
|
|
210
|
+
case "lab":
|
|
203
211
|
// @ts-ignore
|
|
204
212
|
values1 = srgb2labvalues(...values1);
|
|
205
213
|
// @ts-ignore
|
|
206
214
|
values2 = srgb2labvalues(...values2);
|
|
207
215
|
break;
|
|
208
|
-
case
|
|
216
|
+
case "lch":
|
|
209
217
|
// @ts-ignore
|
|
210
218
|
values1 = srgb2lch(...values1);
|
|
211
219
|
// @ts-ignore
|
|
212
220
|
values2 = srgb2lch(...values2);
|
|
213
221
|
break;
|
|
214
|
-
case
|
|
222
|
+
case "oklab":
|
|
215
223
|
// @ts-ignore
|
|
216
224
|
values1 = srgb2oklab(...values1);
|
|
217
225
|
// @ts-ignore
|
|
218
226
|
values2 = srgb2oklab(...values2);
|
|
219
227
|
break;
|
|
220
|
-
case
|
|
228
|
+
case "oklch":
|
|
221
229
|
// @ts-ignore
|
|
222
230
|
values1 = srgb2oklch(...values1);
|
|
223
231
|
// @ts-ignore
|
|
@@ -226,25 +234,27 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
226
234
|
default:
|
|
227
235
|
return null;
|
|
228
236
|
}
|
|
229
|
-
const lchSpaces = [
|
|
230
|
-
const colorSpace1 = ColorType[color1.kin].toLowerCase().replaceAll(
|
|
231
|
-
const colorSpace2 = ColorType[color2.kin].toLowerCase().replaceAll(
|
|
237
|
+
const lchSpaces = ["lch", "oklch"];
|
|
238
|
+
const colorSpace1 = ColorType[color1.kin].toLowerCase().replaceAll("_", "-");
|
|
239
|
+
const colorSpace2 = ColorType[color2.kin].toLowerCase().replaceAll("_", "-");
|
|
232
240
|
// powerless
|
|
233
241
|
if (lchSpaces.includes(colorSpace1) || lchSpaces.includes(colorSpace.val)) {
|
|
234
|
-
if ((components1[2].typ == EnumToken.IdenTokenType && components1[2].val ==
|
|
242
|
+
if ((components1[2].typ == EnumToken.IdenTokenType && components1[2].val == "none") ||
|
|
243
|
+
values1[2] == 0) {
|
|
235
244
|
values1[2] = values2[2];
|
|
236
245
|
}
|
|
237
246
|
}
|
|
238
247
|
// powerless
|
|
239
248
|
if (lchSpaces.includes(colorSpace2) || lchSpaces.includes(colorSpace.val)) {
|
|
240
|
-
if ((components2[2].typ == EnumToken.IdenTokenType && components2[2].val ==
|
|
249
|
+
if ((components2[2].typ == EnumToken.IdenTokenType && components2[2].val == "none") ||
|
|
250
|
+
values2[2] == 0) {
|
|
241
251
|
values2[2] = values1[2];
|
|
242
252
|
}
|
|
243
253
|
}
|
|
244
254
|
if (hueInterpolationMethod != null) {
|
|
245
255
|
let hueIndex = 2;
|
|
246
256
|
let multiplier = 1;
|
|
247
|
-
if ([
|
|
257
|
+
if (["hwb", "hsl"].includes(colorSpace.val)) {
|
|
248
258
|
hueIndex = 0;
|
|
249
259
|
multiplier = 360;
|
|
250
260
|
}
|
|
@@ -253,12 +263,13 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
253
263
|
values2[hueIndex] = h2 / multiplier;
|
|
254
264
|
}
|
|
255
265
|
switch (colorSpace.val) {
|
|
256
|
-
case
|
|
257
|
-
case
|
|
258
|
-
case
|
|
259
|
-
let values = values1
|
|
266
|
+
case "xyz":
|
|
267
|
+
case "xyz-d65":
|
|
268
|
+
case "xyz-d50":
|
|
269
|
+
let values = values1
|
|
270
|
+
.map((v1, i) => (mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul)
|
|
260
271
|
.concat(mul == 1 ? [] : [mul]);
|
|
261
|
-
if (colorSpace.val ==
|
|
272
|
+
if (colorSpace.val == "xyz-d50") {
|
|
262
273
|
// @ts-ignore
|
|
263
274
|
values = xyzd502lch(...values);
|
|
264
275
|
}
|
|
@@ -269,35 +280,35 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
269
280
|
// @ts-ignore
|
|
270
281
|
return {
|
|
271
282
|
typ: EnumToken.ColorTokenType,
|
|
272
|
-
val:
|
|
273
|
-
chi: values.map(v => {
|
|
283
|
+
val: "lch",
|
|
284
|
+
chi: values.map((v) => {
|
|
274
285
|
return {
|
|
275
286
|
typ: EnumToken.NumberTokenType,
|
|
276
|
-
val: v
|
|
287
|
+
val: v,
|
|
277
288
|
};
|
|
278
289
|
}),
|
|
279
|
-
kin: ColorType.LCH
|
|
290
|
+
kin: ColorType.LCH,
|
|
280
291
|
};
|
|
281
|
-
case
|
|
282
|
-
case
|
|
283
|
-
case
|
|
284
|
-
case
|
|
292
|
+
case "srgb":
|
|
293
|
+
case "srgb-linear":
|
|
294
|
+
case "a98-rgb":
|
|
295
|
+
case "rec2020":
|
|
285
296
|
// @ts-ignore
|
|
286
297
|
return {
|
|
287
298
|
typ: EnumToken.ColorTokenType,
|
|
288
|
-
val:
|
|
299
|
+
val: "color",
|
|
289
300
|
chi: calculate(),
|
|
290
301
|
kin: ColorType.COLOR,
|
|
291
|
-
cal:
|
|
302
|
+
cal: "col",
|
|
292
303
|
};
|
|
293
|
-
case
|
|
294
|
-
case
|
|
295
|
-
case
|
|
296
|
-
case
|
|
297
|
-
case
|
|
298
|
-
case
|
|
299
|
-
case
|
|
300
|
-
if ([
|
|
304
|
+
case "rgb":
|
|
305
|
+
case "hsl":
|
|
306
|
+
case "hwb":
|
|
307
|
+
case "lab":
|
|
308
|
+
case "lch":
|
|
309
|
+
case "oklab":
|
|
310
|
+
case "oklch":
|
|
311
|
+
if (["hsl", "hwb"].includes(colorSpace.val)) {
|
|
301
312
|
// @ts-ignore
|
|
302
313
|
if (values1[2] < 0) {
|
|
303
314
|
// @ts-ignore
|
|
@@ -309,7 +320,7 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
309
320
|
values2[2] += 1;
|
|
310
321
|
}
|
|
311
322
|
}
|
|
312
|
-
else if ([
|
|
323
|
+
else if (["lch", "oklch"].includes(colorSpace.val)) {
|
|
313
324
|
// @ts-ignore
|
|
314
325
|
if (values1[2] < 0) {
|
|
315
326
|
// @ts-ignore
|
|
@@ -326,9 +337,9 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
|
|
|
326
337
|
typ: EnumToken.ColorTokenType,
|
|
327
338
|
val: colorSpace.val,
|
|
328
339
|
chi: calculate().slice(1),
|
|
329
|
-
kin: ColorType[colorSpace.val.toUpperCase().replaceAll(
|
|
340
|
+
kin: ColorType[colorSpace.val.toUpperCase().replaceAll("-", "_")],
|
|
330
341
|
};
|
|
331
|
-
if (colorSpace.val ==
|
|
342
|
+
if (colorSpace.val == "hsl" || colorSpace.val == "hwb") {
|
|
332
343
|
// @ts-ignore
|
|
333
344
|
result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val * 360 };
|
|
334
345
|
// @ts-ignore
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AngleToken, ColorToken, FractionToken, IdentToken, NumberToken, PercentageToken } from "../../../@types/index.d.ts";
|
|
2
|
+
import { ColorType } from "../../ast/types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Converts a color to another color space
|
|
5
|
+
* @param token
|
|
6
|
+
* @param to
|
|
7
|
+
*
|
|
8
|
+
* @private
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
*
|
|
12
|
+
* const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
|
|
13
|
+
* const result = convertColor(token, ColorType.LCH);
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function convertColor(token: ColorToken, to: ColorType): ColorToken | null;
|
|
18
|
+
export declare function hex2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
19
|
+
export declare function rgb2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
20
|
+
export declare function hsl2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
21
|
+
export declare function hwb2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
22
|
+
export declare function cmyk2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
23
|
+
export declare function lab2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
24
|
+
export declare function oklab2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
25
|
+
export declare function lch2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
26
|
+
export declare function oklch2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
27
|
+
export declare function color2colorToken(token: ColorToken, to: ColorType): ColorToken | null;
|
|
28
|
+
export declare function minmax(value: number, min: number, max: number): number;
|
|
29
|
+
export declare function color2srgbvalues(token: ColorToken): number[] | null;
|
|
30
|
+
/**
|
|
31
|
+
* clamp color values
|
|
32
|
+
* @param token
|
|
33
|
+
*/
|
|
34
|
+
export declare function clamp(token: ColorToken): ColorToken;
|
|
35
|
+
export declare function getNumber(token: NumberToken | PercentageToken | IdentToken | FractionToken): number;
|
|
36
|
+
/**
|
|
37
|
+
* convert angle to turn
|
|
38
|
+
* @param token
|
|
39
|
+
*/
|
|
40
|
+
export declare function getAngle(token: NumberToken | AngleToken | IdentToken): number;
|
|
41
|
+
export declare function toPrecisionValue(value: number): number;
|
|
42
|
+
export declare function toPrecisionAngle(angle: number): number;
|