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