-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (36 loc) · 1.48 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
/**
* POSTCSS RGB
* A postcss plugin to use rgb and rgba with hex values
* version 1.0.0
* author Arpad Hegedus <[email protected]>
*/
// load dependencies
let postcss = require('postcss'),
util = require('postcss-plugin-utilities');
// export plugin
module.exports = postcss.plugin('postcss-rgb', options => {
return css => {
css.walkDecls(decl => {
if(decl.value.indexOf('rgb(') >= 0 || decl.value.indexOf('rgba(') >= 0) {
// parse rgba values
decl.value = decl.value.replace(/rgba\(([^\,\)]+)(\,([\s]+)?[^\)\,]+)\)/ig, (str, clr, alpha = null) => {
clr = clr.trim();
clrName = util.nameToHex(clr);
if (clrName) { clr = clrName; }
clrHex = util.hexToRGB(clr);
clr = (clrHex)? clrHex : {r: '204', g: '204', b: '204'};
return `rgba(${clr.r}, ${clr.g}, ${clr.b}${alpha})`;
});
// parse rgb values
decl.value = decl.value.replace(/rgb\(([^\,\)]+)\)/ig, (str, clr) => {
clr = clr.trim();
clrName = util.nameToHex(clr);
if (clrName) { clr = clrName; }
clrHex = util.hexToRGB(clr);
clr = (clrHex)? clrHex : {r: '204', g: '204', b: '204'};
return `rgb(${clr.r}, ${clr.g}, ${clr.b})`;
});
}
});
}
});