rgba.js 1.5 KB

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