1 | var utils = require('../utils')
|
---|
2 | , nodes = require('../nodes');
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Return a `RGBA` from the r,g,b,a channels.
|
---|
6 | *
|
---|
7 | * Examples:
|
---|
8 | *
|
---|
9 | * rgba(255,0,0,0.5)
|
---|
10 | * // => rgba(255,0,0,0.5)
|
---|
11 | *
|
---|
12 | * rgba(255,0,0,1)
|
---|
13 | * // => #ff0000
|
---|
14 | *
|
---|
15 | * rgba(#ffcc00, 50%)
|
---|
16 | * // rgba(255,204,0,0.5)
|
---|
17 | *
|
---|
18 | * @param {Unit|RGBA|HSLA} red
|
---|
19 | * @param {Unit} green
|
---|
20 | * @param {Unit} blue
|
---|
21 | * @param {Unit} alpha
|
---|
22 | * @return {RGBA}
|
---|
23 | * @api public
|
---|
24 | */
|
---|
25 |
|
---|
26 | function rgba(red, green, blue, alpha){
|
---|
27 | switch (arguments.length) {
|
---|
28 | case 1:
|
---|
29 | utils.assertColor(red);
|
---|
30 | return red.rgba;
|
---|
31 | case 2:
|
---|
32 | utils.assertColor(red);
|
---|
33 | var color = red.rgba;
|
---|
34 | utils.assertType(green, 'unit', 'alpha');
|
---|
35 | alpha = green.clone();
|
---|
36 | if ('%' == alpha.type) alpha.val /= 100;
|
---|
37 | return new nodes.RGBA(
|
---|
38 | color.r
|
---|
39 | , color.g
|
---|
40 | , color.b
|
---|
41 | , alpha.val);
|
---|
42 | default:
|
---|
43 | utils.assertType(red, 'unit', 'red');
|
---|
44 | utils.assertType(green, 'unit', 'green');
|
---|
45 | utils.assertType(blue, 'unit', 'blue');
|
---|
46 | utils.assertType(alpha, 'unit', 'alpha');
|
---|
47 | var r = '%' == red.type ? Math.round(red.val * 2.55) : red.val
|
---|
48 | , g = '%' == green.type ? Math.round(green.val * 2.55) : green.val
|
---|
49 | , b = '%' == blue.type ? Math.round(blue.val * 2.55) : blue.val;
|
---|
50 |
|
---|
51 | alpha = alpha.clone();
|
---|
52 | if (alpha && '%' == alpha.type) alpha.val /= 100;
|
---|
53 | return new nodes.RGBA(
|
---|
54 | r
|
---|
55 | , g
|
---|
56 | , b
|
---|
57 | , alpha.val);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | rgba.params = ['red', 'green', 'blue', 'alpha'];
|
---|
61 | module.exports = rgba;
|
---|