-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (59 loc) · 1.68 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
/**
* Module dependencies.
*/
var parse = require('color-parser');
var hsb2rgb = require('hsb2rgb');
var functions = require('rework-plugin-function');
/**
* Provide color manipulation helpers.
*/
module.exports = function() {
return functions({
/**
* Converts RGBA(color, alpha) to the corrosponding RGBA(r, g, b, a) equivalent.
*
* background: rgba(#eee, .5)
* background: rgba(white, .2)
*
*/
rgba: function(color, alpha){
var args;
if (2 == arguments.length) {
var c = parse(color.trim());
args = [c.r, c.g, c.b, alpha];
} else {
args = [].slice.call(arguments);
}
return 'rgba(' + args.join(', ') + ')';
},
/**
* Converts HSV (HSB) color values to RGB.
* Saturation and brightness can be expressed as floats or percentages.
*
* color: hsb(220, 45%, .3);
* color: hsb(220deg, 0.45, 30%);
*
*/
hsb: function (hue, saturation, value) {
var rgb = hsb2rgb(hue, saturation, value);
return 'rgb(' + rgb.join(', ') + ')';
},
/**
* Converts HSV (HSB) color values with alpha specified to RGBa.
* Saturation, brightness and alpha can be expressed as floats or
* percentages.
*
* color: hsba(220, 45%, .3, .4);
* color: hsba(220deg, 0.45, 30%, 40%);
*
*/
hsba: function (hue, saturation, value, alpha) {
alpha = /%/.test(alpha)
? parseInt(alpha, 10) / 100
: parseFloat(alpha, 10);
alpha = String(alpha).replace(/^0+\./, '.');
var rgb = hsb2rgb(hue, saturation, value);
return 'rgba(' + rgb.join(', ') + ', ' + alpha + ')';
}
});
};