blend.js 937 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var utils = require('../utils')
  2. , nodes = require('../nodes');
  3. /**
  4. * Blend the `top` color over the `bottom`
  5. *
  6. * Examples:
  7. *
  8. * blend(rgba(#FFF, 0.5), #000)
  9. * // => #808080
  10. *
  11. * blend(rgba(#FFDE00,.42), #19C261)
  12. * // => #7ace38
  13. *
  14. * blend(rgba(lime, 0.5), rgba(red, 0.25))
  15. * // => rgba(128,128,0,0.625)
  16. *
  17. * @param {RGBA|HSLA} top
  18. * @param {RGBA|HSLA} [bottom=#fff]
  19. * @return {RGBA}
  20. * @api public
  21. */
  22. function blend(top, bottom){
  23. // TODO: different blend modes like overlay etc.
  24. utils.assertColor(top);
  25. top = top.rgba;
  26. bottom = bottom || new nodes.RGBA(255, 255, 255, 1);
  27. utils.assertColor(bottom);
  28. bottom = bottom.rgba;
  29. return new nodes.RGBA(
  30. top.r * top.a + bottom.r * (1 - top.a),
  31. top.g * top.a + bottom.g * (1 - top.a),
  32. top.b * top.a + bottom.b * (1 - top.a),
  33. top.a + bottom.a - top.a * bottom.a);
  34. };
  35. blend.params = ['top', 'bottom'];
  36. module.exports = blend;