hsla.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var utils = require('../utils')
  2. , nodes = require('../nodes');
  3. /**
  4. * Convert the given `color` to an `HSLA` node,
  5. * or h,s,l,a component values.
  6. *
  7. * Examples:
  8. *
  9. * hsla(10deg, 50%, 30%, 0.5)
  10. * // => HSLA
  11. *
  12. * hsla(#ffcc00)
  13. * // => HSLA
  14. *
  15. * @param {RGBA|HSLA|Unit} hue
  16. * @param {Unit} saturation
  17. * @param {Unit} lightness
  18. * @param {Unit} alpha
  19. * @return {HSLA}
  20. * @api public
  21. */
  22. function hsla(hue, saturation, lightness, alpha){
  23. switch (arguments.length) {
  24. case 1:
  25. utils.assertColor(hue);
  26. return hue.hsla;
  27. case 2:
  28. utils.assertColor(hue);
  29. var color = hue.hsla;
  30. utils.assertType(saturation, 'unit', 'alpha');
  31. var alpha = saturation.clone();
  32. if ('%' == alpha.type) alpha.val /= 100;
  33. return new nodes.HSLA(
  34. color.h
  35. , color.s
  36. , color.l
  37. , alpha.val);
  38. default:
  39. utils.assertType(hue, 'unit', 'hue');
  40. utils.assertType(saturation, 'unit', 'saturation');
  41. utils.assertType(lightness, 'unit', 'lightness');
  42. utils.assertType(alpha, 'unit', 'alpha');
  43. var alpha = alpha.clone();
  44. if (alpha && '%' == alpha.type) alpha.val /= 100;
  45. return new nodes.HSLA(
  46. hue.val
  47. , saturation.val
  48. , lightness.val
  49. , alpha.val);
  50. }
  51. };
  52. hsla.params = ['hue', 'saturation', 'lightness', 'alpha'];
  53. module.exports = hsla;