forked from adam-h/postcss-sass-color-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (104 loc) · 3.33 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
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
var postcss = require("postcss");
var balanced = require("balanced-match");
var Color = require("color");
var helpers = require("postcss-message-helpers");
var functions = {
mix: function(one, two, weight) {
weight = weight || 0.5;
if(typeof weight == "string" && weight.indexOf("%") > 0) {
weight = parseInt(weight, 10) / 100;
}
return Color(toSimpleColor(one)).mix(Color(toSimpleColor(two)), weight).rgbString();
},
rgba: function() {
if(arguments.length > 2) {
// It's the actual rgba function, not the sass version
return "rgba("+[].slice.call(arguments).join(",")+")";
}
return Color(toSimpleColor(arguments[0])).alpha(arguments[1]).rgbString();
},
darken: function(color, amount) {
if(typeof amount == "string" && amount.indexOf("%") > 0) {
amount = parseInt(amount, 10) / 100;
}
return Color(toSimpleColor(color)).darken(amount).rgbString();
},
lighten: function(color, amount) {
if(typeof amount == "string" && amount.indexOf("%") > 0) {
amount = parseInt(amount, 10) / 100;
}
return Color(toSimpleColor(color)).lighten(amount).rgbString();
},
tint: function(color, amount) {
return functions['mix']("white", color, amount);
},
shade: function(color, amount) {
return functions['mix']("black", color, amount);
},
transparentize: function(color, amount) {
color = Color(toSimpleColor(color))
amount = parseFloat(amount.trim())
var alpha = Math.round((color.alpha() - amount) * 100) / 100;
return color.alpha(alpha).rgbString();
},
opacify: function(color, amount) {
color = Color(toSimpleColor(color))
amount = parseFloat(amount.trim())
var alpha = Math.round((color.alpha() + amount) * 100) / 100;
return color.alpha(alpha).rgbString();
}
};
function toSimpleColor(color) {
if(functionsRegex.test(color)) {
return handleFunction(color);
} else {
return color.trim();
}
}
var functionsRegex = new RegExp(
"(^|[^\\w\\-])" +
"(" +
Object.keys(functions).reduce(function(prev, curr) {
return (prev ? prev + "|" : "") + curr
}, false) +
")\\(");
/**
* PostCSS plugin to transform color()
*/
module.exports = postcss.plugin("postcss-sass-color-functions", function() {
return function(style) {
style.walkDecls(function transformDecl(decl) {
if (!decl.value || !functionsRegex.test(decl.value)) {
return;
}
decl.value = helpers.try(function() {
return handleFunction(decl.value, decl.source);
}, decl.source)
})
}
});
/**
* Transform FUNCTION() to rgba?()
*
* @param {String} string declaration value
* @return {String} converted declaration value to rgba?()
*/
function handleFunction(string, source) {
var match = functionsRegex.exec(string);
if(!match) {
return string
}
var index = match.index;
var sassFn = match[2];
// NOTE: regexp search beginning of line of non char symbol before `FUNCTION(`.
// Offset used for second case.
index = index === 0 ? index : index + 1
var fn = string.slice(index)
var balancedMatches = balanced("(", ")", fn)
if (!balancedMatches) {
throw new Error("Missing closing parentheses in '" + string + "'", source)
}
return string.slice(0, index)
+ functions[sassFn].apply( null, balancedMatches.body.split(/,(?![^\(]*\))/) )
+ handleFunction(balancedMatches.post, source)
}