-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
48 lines (39 loc) · 1.28 KB
/
index.js
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
'use strict';
const color = require('color');
const postcss = require('postcss');
const helpers = require('postcss-message-helpers');
const reduceFunctionCall = require('reduce-function-call');
const pluginName = 'postcss-color-gray';
const errorContext = {plugin: pluginName};
function parseAlpha(alpha) {
if (alpha) {
const match = alpha.match(/^\d(\d|\.)+?%$/);
if (match && match[0] === alpha) {
return parseFloat(alpha) * 0.01;
}
}
return alpha;
}
function parseGray(decl) {
return reduceFunctionCall(decl.value, 'gray', body => {
if (body.startsWith(',') || body.endsWith(',')) {
throw decl.error(`Unable to parse color from string "gray(${body})"`, errorContext);
}
const args = postcss.list.comma(body);
const lightness = args[0];
const alpha = parseAlpha(args[1]);
const rgb = `${lightness},${lightness},${lightness}`;
try {
return color(alpha ? `rgba(${rgb},${alpha})` : `rgb(${rgb})`).rgb().string();
} catch (err) {
throw decl.error(`Unable to parse color from string "gray(${args})"`, errorContext);
}
});
}
module.exports = postcss.plugin(pluginName, () => function(root) {
root.walkDecls(function(decl) {
if (decl.value && decl.value.includes('gray(')) {
decl.value = helpers.try(parseGray.bind(this, decl), decl.source);
}
});
});