-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
executable file
·62 lines (56 loc) · 1.54 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*!
* postcss-color-gray | MIT (c) Shinnosuke Watanabe
* https://github.com/postcss/postcss-color-gray
*/
'use strict';
var color = require('color');
var postcss = require('postcss');
var helpers = require('postcss-message-helpers');
var reduceFunctionCall = require('reduce-function-call');
var pluginName = 'postcss-color-gray';
var errorContext = {plugin: pluginName};
function parseAlpha(alpha) {
if (alpha) {
var 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', function(body) {
if (/^,/.test(body) || /,$/.test(body)) {
throw decl.error(
'Unable to parse color from string "gray(' + body + ')"',
errorContext
);
}
var fn = 'rgb';
var args = postcss.list.comma(body);
var lightness = args[0];
var rgb = [lightness, lightness, lightness];
var alpha = parseAlpha(args[1]);
if (alpha) {
fn += 'a';
rgb.push(alpha);
}
try {
return color(fn + '(' + rgb + ')').rgb().string();
} catch (err) {
throw decl.error(
'Unable to parse color from string "gray(' + args + ')"',
errorContext
);
}
});
}
module.exports = postcss.plugin(pluginName, function() {
return function(root) {
root.walkDecls(function(decl) {
if (decl.value && decl.value.indexOf('gray(') !== -1) {
decl.value = helpers.try(parseGray.bind(this, decl), decl.source);
}
});
};
});