@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.
Files changed (101) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +4195 -3375
  4. package/dist/index.cjs +5468 -4648
  5. package/dist/index.d.ts +42 -31
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +37 -35
  8. package/dist/lib/ast/features/inlinecssvariables.js +25 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +184 -159
  13. package/dist/lib/ast/math/math.js +22 -20
  14. package/dist/lib/ast/minify.js +249 -200
  15. package/dist/lib/ast/transform/compute.js +48 -38
  16. package/dist/lib/ast/transform/matrix.js +6 -5
  17. package/dist/lib/ast/transform/minify.js +31 -34
  18. package/dist/lib/ast/transform/utils.js +76 -16
  19. package/dist/lib/ast/types.js +32 -1
  20. package/dist/lib/fs/resolve.js +1 -14
  21. package/dist/lib/parser/declaration/list.js +1 -1
  22. package/dist/lib/parser/declaration/map.js +1 -1
  23. package/dist/lib/parser/declaration/set.js +1 -1
  24. package/dist/lib/parser/parse.js +19 -95
  25. package/dist/lib/parser/tokenize.js +1 -13
  26. package/dist/lib/parser/utils/declaration.js +1 -1
  27. package/dist/lib/parser/utils/type.js +1 -1
  28. package/dist/lib/renderer/render.js +44 -168
  29. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  30. package/dist/lib/syntax/color/cmyk.js +104 -0
  31. package/dist/lib/{renderer → syntax}/color/color-mix.js +20 -21
  32. package/dist/lib/syntax/color/color.js +581 -0
  33. package/dist/lib/syntax/color/hex.js +179 -0
  34. package/dist/lib/syntax/color/hsl.js +201 -0
  35. package/dist/lib/syntax/color/hwb.js +185 -0
  36. package/dist/lib/syntax/color/lab.js +262 -0
  37. package/dist/lib/syntax/color/lch.js +194 -0
  38. package/dist/lib/syntax/color/oklab.js +237 -0
  39. package/dist/lib/syntax/color/oklch.js +166 -0
  40. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  41. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  42. package/dist/lib/{renderer → syntax}/color/relativecolor.js +53 -40
  43. package/dist/lib/syntax/color/rgb.js +140 -0
  44. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  45. package/dist/lib/{renderer → syntax}/color/utils/components.js +7 -7
  46. package/dist/lib/{renderer → syntax}/color/utils/constants.js +7 -34
  47. package/dist/lib/syntax/color/utils/distance.js +30 -0
  48. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  49. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  50. package/dist/lib/syntax/syntax.js +77 -67
  51. package/dist/lib/syntax/utils.js +70 -0
  52. package/dist/lib/validation/at-rules/container.js +1 -1
  53. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  54. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  55. package/dist/lib/validation/at-rules/document.js +1 -1
  56. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  57. package/dist/lib/validation/at-rules/import.js +1 -1
  58. package/dist/lib/validation/at-rules/keyframes.js +14 -13
  59. package/dist/lib/validation/at-rules/layer.js +1 -1
  60. package/dist/lib/validation/at-rules/media.js +1 -1
  61. package/dist/lib/validation/at-rules/namespace.js +1 -1
  62. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  63. package/dist/lib/validation/at-rules/page.js +1 -1
  64. package/dist/lib/validation/at-rules/supports.js +1 -1
  65. package/dist/lib/validation/at-rules/when.js +1 -1
  66. package/dist/lib/validation/atrule.js +2 -2
  67. package/dist/lib/validation/config.js +4 -3
  68. package/dist/lib/validation/config.json.js +1 -1
  69. package/dist/lib/validation/parser/parse.js +12 -7
  70. package/dist/lib/validation/selector.js +9 -8
  71. package/dist/lib/validation/syntax.js +171 -132
  72. package/dist/lib/validation/syntaxes/complex-selector-list.js +13 -19
  73. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  74. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  75. package/dist/lib/validation/syntaxes/family-name.js +36 -39
  76. package/dist/lib/validation/syntaxes/keyframe-selector.js +14 -22
  77. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  78. package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -25
  79. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  80. package/dist/lib/validation/syntaxes/url.js +35 -33
  81. package/dist/lib/validation/utils/list.js +10 -9
  82. package/dist/lib/validation/utils/whitespace.js +1 -1
  83. package/dist/node/index.js +4 -2
  84. package/dist/web/index.js +4 -2
  85. package/package.json +4 -4
  86. package/.editorconfig +0 -484
  87. package/dist/lib/ast/transform/convert.js +0 -33
  88. package/dist/lib/ast/utils/utils.js +0 -104
  89. package/dist/lib/renderer/color/color.js +0 -654
  90. package/dist/lib/renderer/color/hex.js +0 -105
  91. package/dist/lib/renderer/color/hsl.js +0 -125
  92. package/dist/lib/renderer/color/hwb.js +0 -103
  93. package/dist/lib/renderer/color/lab.js +0 -148
  94. package/dist/lib/renderer/color/lch.js +0 -90
  95. package/dist/lib/renderer/color/oklab.js +0 -131
  96. package/dist/lib/renderer/color/oklch.js +0 -75
  97. package/dist/lib/renderer/color/rgb.js +0 -50
  98. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  99. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  100. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  101. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -1,105 +0,0 @@
1
- import { 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
- import { minmax, getNumber } from './color.js';
8
- import { hsl2rgb, hwb2rgb, cmyk2rgb, oklab2rgb, oklch2rgb, lab2rgb, lch2rgb } from './rgb.js';
9
- import { NAMES_COLORS } from './utils/constants.js';
10
- import { getComponents } from './utils/components.js';
11
- import '../sourcemap/lib/encode.js';
12
-
13
- function toHexString(acc, value) {
14
- return acc + value.toString(16).padStart(2, '0');
15
- }
16
- function reduceHexValue(value) {
17
- const named_color = NAMES_COLORS[expandHexValue(value)];
18
- if (value.length == 7) {
19
- if (value[1] == value[2] &&
20
- value[3] == value[4] &&
21
- value[5] == value[6]) {
22
- value = `#${value[1]}${value[3]}${value[5]}`;
23
- }
24
- }
25
- else if (value.length == 9) {
26
- if (value[1] == value[2] &&
27
- value[3] == value[4] &&
28
- value[5] == value[6] &&
29
- value[7] == value[8]) {
30
- value = `#${value[1]}${value[3]}${value[5]}${value[7]}`;
31
- }
32
- }
33
- return named_color != null && named_color.length <= value.length ? named_color : value;
34
- }
35
- function expandHexValue(value) {
36
- if (value.length == 4) {
37
- return `#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}`;
38
- }
39
- if (value.length == 5) {
40
- return `#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}${value[4]}${value[4]}`;
41
- }
42
- return value;
43
- }
44
- function rgb2hex(token) {
45
- let value = '#';
46
- let t;
47
- // @ts-ignore
48
- const components = getComponents(token);
49
- // @ts-ignore
50
- for (let i = 0; i < 3; i++) {
51
- // @ts-ignore
52
- t = components[i];
53
- // @ts-ignore
54
- value += (t.typ == EnumToken.Iden && t.val == 'none' ? '0' : Math.round(getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 255 : 1))).toString(16).padStart(2, '0');
55
- }
56
- // @ts-ignore
57
- if (components.length == 4) {
58
- // @ts-ignore
59
- t = components[3];
60
- // @ts-ignore
61
- const v = (t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
62
- // @ts-ignore
63
- if (v < 1) {
64
- // @ts-ignore
65
- value += Math.round(255 * getNumber(t)).toString(16).padStart(2, '0');
66
- }
67
- }
68
- return value;
69
- }
70
- function hsl2hex(token) {
71
- const t = hsl2rgb(token);
72
- return t == null ? null : `${t.reduce(toHexString, '#')}`;
73
- }
74
- function hwb2hex(token) {
75
- const t = hwb2rgb(token);
76
- return t == null ? null : `${t.reduce(toHexString, '#')}`;
77
- }
78
- function cmyk2hex(token) {
79
- const t = cmyk2rgb(token);
80
- return t == null ? null : `#${t.reduce(toHexString, '')}`;
81
- }
82
- function oklab2hex(token) {
83
- const t = oklab2rgb(token);
84
- return t == null ? null : `${t.reduce(toHexString, '#')}`;
85
- }
86
- function oklch2hex(token) {
87
- const value = oklch2rgb(token);
88
- if (value == null) {
89
- return null;
90
- }
91
- return `${value.reduce(toHexString, '#')}`;
92
- }
93
- function lab2hex(token) {
94
- const t = lab2rgb(token);
95
- return t == null ? null : `${t.reduce(toHexString, '#')}`;
96
- }
97
- function lch2hex(token) {
98
- const t = lch2rgb(token);
99
- return t == null ? null : `${t.reduce(toHexString, '#')}`;
100
- }
101
- function srgb2hexvalues(r, g, b, alpha) {
102
- return [r, g, b].concat(alpha == null || alpha == 1 ? [] : [alpha]).reduce((acc, value) => acc + minmax(Math.round(255 * value), 0, 255).toString(16).padStart(2, '0'), '#');
103
- }
104
-
105
- export { cmyk2hex, expandHexValue, hsl2hex, hwb2hex, lab2hex, lch2hex, oklab2hex, oklch2hex, reduceHexValue, rgb2hex, srgb2hexvalues };
@@ -1,125 +0,0 @@
1
- import { hwb2hsv } from './hsv.js';
2
- import { getNumber } from './color.js';
3
- import { lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hex2rgb } from './rgb.js';
4
- import './utils/constants.js';
5
- import { getComponents } from './utils/components.js';
6
- import { EnumToken } from '../../ast/types.js';
7
- import '../../ast/minify.js';
8
- import '../../ast/walk.js';
9
- import '../../parser/parse.js';
10
- import '../../parser/tokenize.js';
11
- import '../../parser/utils/config.js';
12
- import { hslvalues } from './srgb.js';
13
- import '../sourcemap/lib/encode.js';
14
-
15
- function hex2hsl(token) {
16
- // @ts-ignore
17
- return rgb2hslvalues(...hex2rgb(token));
18
- }
19
- function rgb2hsl(token) {
20
- const chi = getComponents(token);
21
- if (chi == null) {
22
- return null;
23
- }
24
- // @ts-ignore
25
- let t = chi[0];
26
- // @ts-ignore
27
- let r = getNumber(t);
28
- // @ts-ignore
29
- t = chi[1];
30
- // @ts-ignore
31
- let g = getNumber(t);
32
- // @ts-ignore
33
- t = chi[2];
34
- // @ts-ignore
35
- let b = getNumber(t);
36
- // @ts-ignore
37
- t = chi[3];
38
- // @ts-ignore
39
- let a = null;
40
- if (t != null && !(t.typ == EnumToken.IdenTokenType && t.val == 'none')) {
41
- // @ts-ignore
42
- a = getNumber(t) / 255;
43
- }
44
- const values = [r, g, b];
45
- if (a != null && a != 1) {
46
- values.push(a);
47
- }
48
- // @ts-ignore
49
- return rgb2hslvalues(...values);
50
- }
51
- // https://gist.github.com/defims/0ca2ef8832833186ed396a2f8a204117#file-annotated-js
52
- function hsv2hsl(h, s, v, a) {
53
- const result = [
54
- //[hue, saturation, lightness]
55
- //Range should be between 0 - 1
56
- h, //Hue stays the same
57
- //Saturation is very different between the two color spaces
58
- //If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
59
- //Otherwise sat*val/(2-(2-sat)*val)
60
- //Conditional is not operating with hue, it is reassigned!
61
- s * v / ((h = (2 - s) * v) < 1 ? h : 2 - h),
62
- h / 2, //Lightness is (2-sat)*val/2
63
- ];
64
- if (a != null) {
65
- result.push(a);
66
- }
67
- return result;
68
- }
69
- function hwb2hsl(token) {
70
- // @ts-ignore
71
- return hsv2hsl(...hwb2hsv(...Object.values(hslvalues(token))));
72
- }
73
- function lab2hsl(token) {
74
- // @ts-ignore
75
- return rgb2hslvalues(...lab2rgb(token));
76
- }
77
- function lch2hsl(token) {
78
- // @ts-ignore
79
- return rgb2hslvalues(...lch2rgb(token));
80
- }
81
- function oklab2hsl(token) {
82
- const t = oklab2rgb(token);
83
- // @ts-ignore
84
- return t == null ? null : rgb2hslvalues(...t);
85
- }
86
- function oklch2hsl(token) {
87
- const t = oklch2rgb(token);
88
- // @ts-ignore
89
- return t == null ? null : rgb2hslvalues(...t);
90
- }
91
- function rgb2hslvalues(r, g, b, a = null) {
92
- return srgb2hsl(r / 255, g / 255, b / 255, a);
93
- }
94
- function srgb2hsl(r, g, b, a = null) {
95
- let max = Math.max(r, g, b);
96
- let min = Math.min(r, g, b);
97
- let h = 0;
98
- let s = 0;
99
- let l = (max + min) / 2;
100
- if (max != min) {
101
- let d = max - min;
102
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
103
- switch (max) {
104
- case r:
105
- h = (g - b) / d + (g < b ? 6 : 0);
106
- break;
107
- case g:
108
- h = (b - r) / d + 2;
109
- break;
110
- case b:
111
- h = (r - g) / d + 4;
112
- break;
113
- }
114
- h /= 6;
115
- }
116
- const hsl = [h, s, l];
117
- if (a != null && a < 1) {
118
- // @ts-ignore
119
- return hsl.concat([a]);
120
- }
121
- // @ts-ignore
122
- return hsl;
123
- }
124
-
125
- export { hex2hsl, hsv2hsl, hwb2hsl, lab2hsl, lch2hsl, oklab2hsl, oklch2hsl, rgb2hsl, rgb2hslvalues, srgb2hsl };
@@ -1,103 +0,0 @@
1
- import { hsl2hsv } from './hsv.js';
2
- import './utils/constants.js';
3
- import { getComponents } from './utils/components.js';
4
- import { getAngle, getNumber } from './color.js';
5
- import { EnumToken } from '../../ast/types.js';
6
- import '../../ast/minify.js';
7
- import '../../ast/walk.js';
8
- import '../../parser/parse.js';
9
- import '../../parser/tokenize.js';
10
- import '../../parser/utils/config.js';
11
- import { lch2srgb, lab2srgb, oklch2srgb, oklab2srgb } from './srgb.js';
12
- import '../sourcemap/lib/encode.js';
13
-
14
- function rgb2hwb(token) {
15
- // @ts-ignore
16
- return srgb2hwb(...getComponents(token).map((t, index) => {
17
- if (index == 3 && t.typ == EnumToken.IdenTokenType && t.val == 'none') {
18
- return 1;
19
- }
20
- return getNumber(t) / 255;
21
- }));
22
- }
23
- function hsl2hwb(token) {
24
- // @ts-ignore
25
- return hsl2hwbvalues(...getComponents(token).map((t, index) => {
26
- if (index == 3 && (t.typ == EnumToken.IdenTokenType && t.val == 'none')) {
27
- return 1;
28
- }
29
- if (index == 0) {
30
- return getAngle(t);
31
- }
32
- return getNumber(t);
33
- }));
34
- }
35
- function lab2hwb(token) {
36
- // @ts-ignore
37
- return srgb2hwb(...lab2srgb(token));
38
- }
39
- function lch2hwb(token) {
40
- // @ts-ignore
41
- return srgb2hwb(...lch2srgb(token));
42
- }
43
- function oklab2hwb(token) {
44
- // @ts-ignore
45
- return srgb2hwb(...oklab2srgb(token));
46
- }
47
- function oklch2hwb(token) {
48
- // @ts-ignore
49
- return srgb2hwb(...oklch2srgb(token));
50
- }
51
- function rgb2hue(r, g, b, fallback = 0) {
52
- let value = rgb2value(r, g, b);
53
- let whiteness = rgb2whiteness(r, g, b);
54
- let delta = value - whiteness;
55
- if (delta > 0) {
56
- // calculate segment
57
- let segment = value === r ? (g - b) / delta : (value === g
58
- ? (b - r) / delta
59
- : (r - g) / delta);
60
- // calculate shift
61
- let shift = value === r ? segment < 0
62
- ? 360 / 60
63
- : 0 / 60 : (value === g
64
- ? 120 / 60
65
- : 240 / 60);
66
- // calculate hue
67
- return (segment + shift) * 60;
68
- }
69
- return fallback;
70
- }
71
- function rgb2value(r, g, b) {
72
- return Math.max(r, g, b);
73
- }
74
- function rgb2whiteness(r, g, b) {
75
- return Math.min(r, g, b);
76
- }
77
- function srgb2hwb(r, g, b, a = null, fallback = 0) {
78
- r *= 100;
79
- g *= 100;
80
- b *= 100;
81
- let hue = rgb2hue(r, g, b, fallback);
82
- let whiteness = rgb2whiteness(r, g, b);
83
- let value = Math.round(rgb2value(r, g, b));
84
- let blackness = 100 - value;
85
- const result = [hue / 360, whiteness / 100, blackness / 100];
86
- if (a != null) {
87
- result.push(a);
88
- }
89
- return result;
90
- }
91
- function hsv2hwb(h, s, v, a = null) {
92
- const result = [h, (1 - s) * v, 1 - v];
93
- if (a != null) {
94
- result.push(a);
95
- }
96
- return result;
97
- }
98
- function hsl2hwbvalues(h, s, l, a = null) {
99
- // @ts-ignore
100
- return hsv2hwb(...hsl2hsv(h, s, l, a));
101
- }
102
-
103
- export { hsl2hwb, hsl2hwbvalues, hsv2hwb, lab2hwb, lch2hwb, oklab2hwb, oklch2hwb, rgb2hwb, srgb2hwb };
@@ -1,148 +0,0 @@
1
- import { e, k, D50 } from './utils/constants.js';
2
- import { getComponents } from './utils/components.js';
3
- import { srgb2xyz } from './xyz.js';
4
- import { oklch2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb } from './srgb.js';
5
- import { getLCHComponents } from './lch.js';
6
- import { OKLab_to_XYZ, getOKLABComponents } from './oklab.js';
7
- import { getNumber } from './color.js';
8
- import { EnumToken } from '../../ast/types.js';
9
- import '../../ast/minify.js';
10
- import '../../ast/walk.js';
11
- import '../../parser/parse.js';
12
- import '../../parser/tokenize.js';
13
- import '../../parser/utils/config.js';
14
- import { xyzd502srgb } from './xyzd50.js';
15
- import '../sourcemap/lib/encode.js';
16
-
17
- // L: 0% = 0.0, 100% = 100.0
18
- // for a and b: -100% = -125, 100% = 125
19
- function hex2lab(token) {
20
- // @ts-ignore
21
- return srgb2lab(...hex2srgb(token));
22
- }
23
- function rgb2lab(token) {
24
- // @ts-ignore
25
- return srgb2lab(...rgb2srgb(token));
26
- }
27
- function hsl2lab(token) {
28
- // @ts-ignore
29
- return srgb2lab(...hsl2srgb(token));
30
- }
31
- function hwb2lab(token) {
32
- // @ts-ignore
33
- return srgb2lab(...hwb2srgb(token));
34
- }
35
- function lch2lab(token) {
36
- // @ts-ignore
37
- return lch2labvalues(...getLCHComponents(token));
38
- }
39
- function oklab2lab(token) {
40
- // @ts-ignore
41
- return xyz2lab(...OKLab_to_XYZ(...getOKLABComponents(token)));
42
- }
43
- function oklch2lab(token) {
44
- // @ts-ignore
45
- return srgb2lab(...oklch2srgb(token));
46
- }
47
- function srgb2lab(r, g, b, a) {
48
- // @ts-ignore */
49
- const result = xyz2lab(...srgb2xyz(r, g, b));
50
- // Fixes achromatic RGB colors having a _slight_ chroma due to floating-point errors
51
- // and approximated computations in sRGB <-> CIELab.
52
- // See: https://github.com/d3/d3-color/pull/46
53
- if (r === b && b === g) {
54
- result[1] = result[2] = 0;
55
- }
56
- if (a != null) {
57
- result.push(a);
58
- }
59
- return result;
60
- }
61
- function xyz2lab(x, y, z, a = null) {
62
- // Assuming XYZ is relative to D50, convert to CIE Lab
63
- // from CIE standard, which now defines these as a rational fraction
64
- // var e = 216/24389; // 6^3/29^3
65
- // var k = 24389/27; // 29^3/3^3
66
- // compute xyz, which is XYZ scaled relative to reference white
67
- const xyz = [x, y, z].map((value, i) => value / D50[i]);
68
- // now compute f
69
- const f = xyz.map((value) => value > e ? Math.cbrt(value) : (k * value + 16) / 116);
70
- const result = [
71
- (116 * f[1]) - 16, // L
72
- 500 * (f[0] - f[1]), // a
73
- 200 * (f[1] - f[2]) // b
74
- ];
75
- // L in range [0,100]. For use in CSS, add a percent
76
- if (a != null && a != 1) {
77
- result.push(a);
78
- }
79
- return result;
80
- }
81
- function lch2labvalues(l, c, h, a = null) {
82
- // l, c * Math.cos(360 * h * Math.PI / 180), c * Math.sin(360 * h * Math.PI / 180
83
- const result = [l, c * Math.cos(h * Math.PI / 180), c * Math.sin(h * Math.PI / 180)];
84
- if (a != null) {
85
- result.push(a);
86
- }
87
- return result;
88
- }
89
- function getLABComponents(token) {
90
- const components = getComponents(token);
91
- if (components == null) {
92
- return null;
93
- }
94
- for (let i = 0; i < components.length; i++) {
95
- if (![EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.IdenTokenType].includes(components[i].typ)) {
96
- return [];
97
- }
98
- }
99
- // @ts-ignore
100
- let t = components[0];
101
- // @ts-ignore
102
- const l = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 100 : 1);
103
- // @ts-ignore
104
- t = components[1];
105
- // @ts-ignore
106
- const a = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 125 : 1);
107
- // @ts-ignore
108
- t = components[2];
109
- // @ts-ignore
110
- const b = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 125 : 1);
111
- // @ts-ignore
112
- t = components[3];
113
- // @ts-ignore
114
- const alpha = t == null ? 1 : getNumber(t);
115
- const result = [l, a, b];
116
- if (alpha != null && alpha != 1) {
117
- result.push(alpha);
118
- }
119
- return result;
120
- }
121
- // from https://www.w3.org/TR/css-color-4/#color-conversion-code
122
- // D50 LAB
123
- function Lab_to_sRGB(l, a, b) {
124
- // @ts-ignore
125
- return xyzd502srgb(...Lab_to_XYZ(l, a, b));
126
- }
127
- // from https://www.w3.org/TR/css-color-4/#color-conversion-code
128
- function Lab_to_XYZ(l, a, b) {
129
- // Convert Lab to D50-adapted XYZ
130
- // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
131
- const k = 24389 / 27; // 29^3/3^3
132
- const e = 216 / 24389; // 6^3/29^3
133
- const f = [];
134
- // compute f, starting with the luminance-related term
135
- f[1] = (l + 16) / 116;
136
- f[0] = a / 500 + f[1];
137
- f[2] = f[1] - b / 200;
138
- // compute xyz
139
- const xyz = [
140
- Math.pow(f[0], 3) > e ? Math.pow(f[0], 3) : (116 * f[0] - 16) / k,
141
- l > k * e ? Math.pow((l + 16) / 116, 3) : l / k,
142
- Math.pow(f[2], 3) > e ? Math.pow(f[2], 3) : (116 * f[2] - 16) / k
143
- ];
144
- // Compute XYZ by scaling xyz by reference white
145
- return xyz.map((value, i) => value * D50[i]);
146
- }
147
-
148
- export { Lab_to_XYZ, Lab_to_sRGB, getLABComponents, hex2lab, hsl2lab, hwb2lab, lch2lab, lch2labvalues, oklab2lab, oklch2lab, rgb2lab, srgb2lab, xyz2lab };
@@ -1,90 +0,0 @@
1
- import './utils/constants.js';
2
- import { getComponents } from './utils/components.js';
3
- import { getNumber, getAngle } from './color.js';
4
- import { EnumToken } from '../../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
- import { srgb2lab, xyz2lab, oklch2lab, oklab2lab, getLABComponents, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
11
- import '../sourcemap/lib/encode.js';
12
-
13
- function hex2lch(token) {
14
- // @ts-ignore
15
- return lab2lchvalues(...hex2lab(token));
16
- }
17
- function rgb2lch(token) {
18
- // @ts-ignore
19
- return lab2lchvalues(...rgb2lab(token));
20
- }
21
- function hsl2lch(token) {
22
- // @ts-ignore
23
- return lab2lchvalues(...hsl2lab(token));
24
- }
25
- function hwb2lch(token) {
26
- // @ts-ignore
27
- return lab2lchvalues(...hwb2lab(token));
28
- }
29
- function lab2lch(token) {
30
- // @ts-ignore
31
- return lab2lchvalues(...getLABComponents(token));
32
- }
33
- function srgb2lch(r, g, blue, alpha) {
34
- // @ts-ignore
35
- return lab2lchvalues(...srgb2lab(r, g, blue, alpha));
36
- }
37
- function oklab2lch(token) {
38
- // @ts-ignore
39
- return lab2lchvalues(...oklab2lab(token));
40
- }
41
- function oklch2lch(token) {
42
- // @ts-ignore
43
- return lab2lchvalues(...oklch2lab(token));
44
- }
45
- function lab2lchvalues(l, a, b, alpha = null) {
46
- let c = Math.sqrt(a * a + b * b);
47
- let h = Math.atan2(b, a) * 180 / Math.PI;
48
- if (h < 0) {
49
- h += 360;
50
- }
51
- if (c < .0001) {
52
- c = h = 0;
53
- }
54
- return alpha == null ? [l, c, h] : [l, c, h, alpha];
55
- }
56
- function xyz2lchvalues(x, y, z, alpha) {
57
- // @ts-ignore(
58
- const lch = lab2lchvalues(...xyz2lab(x, y, z));
59
- return alpha == null || alpha == 1 ? lch : lch.concat(alpha);
60
- }
61
- function getLCHComponents(token) {
62
- const components = getComponents(token);
63
- if (components == null) {
64
- return null;
65
- }
66
- for (let i = 0; i < components.length; i++) {
67
- if (![EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.IdenTokenType].includes(components[i].typ)) {
68
- return null;
69
- }
70
- }
71
- // @ts-ignore
72
- let t = components[0];
73
- // @ts-ignore
74
- const l = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 100 : 1);
75
- // @ts-ignore
76
- t = components[1];
77
- // @ts-ignore
78
- const c = getNumber(t) * (t.typ == EnumToken.PercentageTokenType ? 150 : 1);
79
- // @ts-ignore
80
- t = components[2];
81
- // @ts-ignore
82
- const h = getAngle(t) * 360;
83
- // @ts-ignore
84
- t = components[3];
85
- // @ts-ignore
86
- const alpha = t == null ? 1 : getNumber(t);
87
- return alpha == null ? [l, c, h] : [l, c, h, alpha];
88
- }
89
-
90
- export { getLCHComponents, hex2lch, hsl2lch, hwb2lch, lab2lch, lab2lchvalues, oklab2lch, oklch2lch, rgb2lch, srgb2lch, xyz2lchvalues };