Guest User

Untitled

a guest
Feb 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. /**
  2. * Determine the mean value of two CSS color, where mean is by channel
  3. * @param {string} color1 string of the first color
  4. * @param {string} color2 string of the second color
  5. * @requires {string} string of the mean color
  6. */
  7. var average_color = function(color1, color2) {
  8. // Assumption: "color1" and "color2" need not to be validated
  9.  
  10. var r1 = parseInt(color1.substr(0, 2), 16),
  11. g1 = parseInt(color1.substr(2, 2), 16),
  12. b1 = parseInt(color1.substr(4, 2), 16);
  13. var r2 = parseInt(color2.substr(0, 2), 16),
  14. g2 = parseInt(color2.substr(2, 2), 16),
  15. b2 = parseInt(color2.substr(4, 2), 16);
  16. var r3 = Math.round(0.5 * (r1 + r2)),
  17. g3 = Math.round(0.5 * (g1 + g2)),
  18. b3 = Math.round(0.5 * (b1 + b2));
  19. return (r3 * 65536 + g3 * 256 + b3).toString(16);
  20. }; // var average_color = function(color1, color2) { ... };
  21.  
  22. console.log(average_color('135246', 'acfbe0'));
Add Comment
Please, Sign In to add comment