You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
var utils = require('../utils') |
|
, nodes = require('../nodes'); |
|
|
|
/** |
|
* Return a `RGBA` from the r,g,b,a channels. |
|
* |
|
* Examples: |
|
* |
|
* rgba(255,0,0,0.5) |
|
* // => rgba(255,0,0,0.5) |
|
* |
|
* rgba(255,0,0,1) |
|
* // => #ff0000 |
|
* |
|
* rgba(#ffcc00, 50%) |
|
* // rgba(255,204,0,0.5) |
|
* |
|
* @param {Unit|RGBA|HSLA} red |
|
* @param {Unit} green |
|
* @param {Unit} blue |
|
* @param {Unit} alpha |
|
* @return {RGBA} |
|
* @api public |
|
*/ |
|
|
|
module.exports = function rgba(red, green, blue, alpha){ |
|
switch (arguments.length) { |
|
case 1: |
|
utils.assertColor(red); |
|
return red.rgba; |
|
case 2: |
|
utils.assertColor(red); |
|
var color = red.rgba; |
|
utils.assertType(green, 'unit', 'alpha'); |
|
alpha = green.clone(); |
|
if ('%' == alpha.type) alpha.val /= 100; |
|
return new nodes.RGBA( |
|
color.r |
|
, color.g |
|
, color.b |
|
, alpha.val); |
|
default: |
|
utils.assertType(red, 'unit', 'red'); |
|
utils.assertType(green, 'unit', 'green'); |
|
utils.assertType(blue, 'unit', 'blue'); |
|
utils.assertType(alpha, 'unit', 'alpha'); |
|
var r = '%' == red.type ? Math.round(red.val * 2.55) : red.val |
|
, g = '%' == green.type ? Math.round(green.val * 2.55) : green.val |
|
, b = '%' == blue.type ? Math.round(blue.val * 2.55) : blue.val; |
|
|
|
alpha = alpha.clone(); |
|
if (alpha && '%' == alpha.type) alpha.val /= 100; |
|
return new nodes.RGBA( |
|
r |
|
, g |
|
, b |
|
, alpha.val); |
|
} |
|
};
|
|
|