1 | import postcss from 'postcss';
|
---|
2 | import parser from 'postcss-values-parser';
|
---|
3 | import { lab2rgb } from '@csstools/convert-colors';
|
---|
4 |
|
---|
5 | function _slicedToArray(arr, i) {
|
---|
6 | return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
|
---|
7 | }
|
---|
8 |
|
---|
9 | function _arrayWithHoles(arr) {
|
---|
10 | if (Array.isArray(arr)) return arr;
|
---|
11 | }
|
---|
12 |
|
---|
13 | function _iterableToArrayLimit(arr, i) {
|
---|
14 | var _arr = [];
|
---|
15 | var _n = true;
|
---|
16 | var _d = false;
|
---|
17 | var _e = undefined;
|
---|
18 |
|
---|
19 | try {
|
---|
20 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
---|
21 | _arr.push(_s.value);
|
---|
22 |
|
---|
23 | if (i && _arr.length === i) break;
|
---|
24 | }
|
---|
25 | } catch (err) {
|
---|
26 | _d = true;
|
---|
27 | _e = err;
|
---|
28 | } finally {
|
---|
29 | try {
|
---|
30 | if (!_n && _i["return"] != null) _i["return"]();
|
---|
31 | } finally {
|
---|
32 | if (_d) throw _e;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | return _arr;
|
---|
37 | }
|
---|
38 |
|
---|
39 | function _nonIterableRest() {
|
---|
40 | throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
---|
41 | }
|
---|
42 |
|
---|
43 | var index = postcss.plugin('postcss-color-gray', opts => root => {
|
---|
44 | // walk all declarations likely containing a gray() function
|
---|
45 | root.walkDecls(decl => {
|
---|
46 | if (hasGrayFunction(decl)) {
|
---|
47 | const originalValue = decl.value; // parse the declaration value
|
---|
48 |
|
---|
49 | const ast = parser(originalValue).parse(); // walk every node in the value that contains a gray() function
|
---|
50 |
|
---|
51 | ast.walk(node => {
|
---|
52 | const _getFunctionGrayArgs = getFunctionGrayArgs(node),
|
---|
53 | _getFunctionGrayArgs2 = _slicedToArray(_getFunctionGrayArgs, 2),
|
---|
54 | lightness = _getFunctionGrayArgs2[0],
|
---|
55 | alpha = _getFunctionGrayArgs2[1];
|
---|
56 |
|
---|
57 | if (lightness !== undefined) {
|
---|
58 | // rename the gray() function to rgb()
|
---|
59 | node.value = 'rgb'; // convert the lab gray lightness into rgb
|
---|
60 |
|
---|
61 | const _lab2rgb$map = lab2rgb(lightness, 0, 0).map(channel => Math.max(Math.min(Math.round(channel * 2.55), 255), 0)),
|
---|
62 | _lab2rgb$map2 = _slicedToArray(_lab2rgb$map, 3),
|
---|
63 | r = _lab2rgb$map2[0],
|
---|
64 | g = _lab2rgb$map2[1],
|
---|
65 | b = _lab2rgb$map2[2]; // preserve the slash nodes within rgb()
|
---|
66 |
|
---|
67 |
|
---|
68 | const openingSlash = node.first;
|
---|
69 | const closingSlash = node.last;
|
---|
70 | node.removeAll() // replace the contents of rgb with `(r,g,b`
|
---|
71 | .append(openingSlash).append(parser.number({
|
---|
72 | value: r
|
---|
73 | })).append(parser.comma({
|
---|
74 | value: ','
|
---|
75 | })).append(parser.number({
|
---|
76 | value: g
|
---|
77 | })).append(parser.comma({
|
---|
78 | value: ','
|
---|
79 | })).append(parser.number({
|
---|
80 | value: b
|
---|
81 | })); // if an alpha channel was defined
|
---|
82 |
|
---|
83 | if (alpha < 1) {
|
---|
84 | // rename the rgb() function to rgba()
|
---|
85 | node.value += 'a';
|
---|
86 | node // append the contents of rgba with `,a`
|
---|
87 | .append(parser.comma({
|
---|
88 | value: ','
|
---|
89 | })).append(parser.number({
|
---|
90 | value: alpha
|
---|
91 | }));
|
---|
92 | } // append the contents of rgb/rgba with `)`
|
---|
93 |
|
---|
94 |
|
---|
95 | node.append(closingSlash);
|
---|
96 | }
|
---|
97 | });
|
---|
98 | const modifiedValue = ast.toString(); // if the modified value has changed from the original value
|
---|
99 |
|
---|
100 | if (originalValue !== modifiedValue) {
|
---|
101 | // if the original gray() color is to be preserved
|
---|
102 | if (Object(opts).preserve) {
|
---|
103 | // insert the declaration value with the fallback before the current declaration
|
---|
104 | decl.cloneBefore({
|
---|
105 | value: modifiedValue
|
---|
106 | });
|
---|
107 | } else {
|
---|
108 | // otherwise, overwrite the declaration value with the fallback
|
---|
109 | decl.value = modifiedValue;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | });
|
---|
114 | }); // return whether a string contains a gray() function
|
---|
115 |
|
---|
116 | const hasGrayFunctionRegExp = /(^|[^\w-])gray\(/i;
|
---|
117 |
|
---|
118 | const hasGrayFunction = decl => hasGrayFunctionRegExp.test(Object(decl).value); // return whether a node matches a specific type
|
---|
119 |
|
---|
120 |
|
---|
121 | const isNumber = node => Object(node).type === 'number';
|
---|
122 |
|
---|
123 | const isOperator = node => Object(node).type === 'operator';
|
---|
124 |
|
---|
125 | const isFunction = node => Object(node).type === 'func';
|
---|
126 |
|
---|
127 | const isCalcRegExp = /^calc$/i;
|
---|
128 |
|
---|
129 | const isFunctionCalc = node => isFunction(node) && isCalcRegExp.test(node.value);
|
---|
130 |
|
---|
131 | const isGrayRegExp = /^gray$/i;
|
---|
132 |
|
---|
133 | const isFunctionGrayWithArgs = node => isFunction(node) && isGrayRegExp.test(node.value) && node.nodes && node.nodes.length;
|
---|
134 |
|
---|
135 | const isNumberPercentage = node => isNumber(node) && node.unit === '%';
|
---|
136 |
|
---|
137 | const isNumberUnitless = node => isNumber(node) && node.unit === '';
|
---|
138 |
|
---|
139 | const isOperatorSlash = node => isOperator(node) && node.value === '/'; // return valid values from a node, otherwise undefined
|
---|
140 |
|
---|
141 |
|
---|
142 | const getNumberUnitless = node => isNumberUnitless(node) ? Number(node.value) : undefined;
|
---|
143 |
|
---|
144 | const getOperatorSlash = node => isOperatorSlash(node) ? null : undefined;
|
---|
145 |
|
---|
146 | const getAlpha = node => isFunctionCalc(node) ? String(node) : isNumberUnitless(node) ? Number(node.value) : isNumberPercentage(node) ? Number(node.value) / 100 : undefined; // return valid arguments from a gray() function
|
---|
147 |
|
---|
148 |
|
---|
149 | const functionalGrayArgs = [getNumberUnitless, getOperatorSlash, getAlpha];
|
---|
150 |
|
---|
151 | const getFunctionGrayArgs = node => {
|
---|
152 | const validArgs = []; // if the node is a gray() function with arguments
|
---|
153 |
|
---|
154 | if (isFunctionGrayWithArgs(node)) {
|
---|
155 | // get all the gray() function arguments between `(` and `)`
|
---|
156 | const nodes = node.nodes.slice(1, -1); // validate each argument
|
---|
157 |
|
---|
158 | for (const index in nodes) {
|
---|
159 | const arg = typeof functionalGrayArgs[index] === 'function' ? functionalGrayArgs[index](nodes[index]) : undefined; // if the argument was validated
|
---|
160 |
|
---|
161 | if (arg !== undefined) {
|
---|
162 | // push any non-null argument to the valid arguments array
|
---|
163 | if (arg !== null) {
|
---|
164 | validArgs.push(arg);
|
---|
165 | }
|
---|
166 | } else {
|
---|
167 | // otherwise, return an empty array
|
---|
168 | return [];
|
---|
169 | }
|
---|
170 | } // return the valid arguments array
|
---|
171 |
|
---|
172 |
|
---|
173 | return validArgs;
|
---|
174 | } else {
|
---|
175 | // otherwise, return an empty array
|
---|
176 | return [];
|
---|
177 | }
|
---|
178 | };
|
---|
179 |
|
---|
180 | export default index;
|
---|