luminosity.js 821 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var utils = require('../utils')
  2. , nodes = require('../nodes');
  3. /**
  4. * Returns the relative luminance of the given `color`,
  5. * see http://www.w3.org/TR/WCAG20/#relativeluminancedef
  6. *
  7. * Examples:
  8. *
  9. * luminosity(white)
  10. * // => 1
  11. *
  12. * luminosity(#000)
  13. * // => 0
  14. *
  15. * luminosity(red)
  16. * // => 0.2126
  17. *
  18. * @param {RGBA|HSLA} color
  19. * @return {Unit}
  20. * @api public
  21. */
  22. module.exports = function luminosity(color){
  23. utils.assertColor(color);
  24. color = color.rgba;
  25. function processChannel(channel) {
  26. channel = channel / 255;
  27. return (0.03928 > channel)
  28. ? channel / 12.92
  29. : Math.pow(((channel + 0.055) / 1.055), 2.4);
  30. }
  31. return new nodes.Unit(
  32. 0.2126 * processChannel(color.r)
  33. + 0.7152 * processChannel(color.g)
  34. + 0.0722 * processChannel(color.b)
  35. );
  36. };