Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Determine the mean value of two CSS color, where mean is by channel
- * @param {string} color1 string of the first color
- * @param {string} color2 string of the second color
- * @requires {string} string of the mean color
- */
- var average_color = function(color1, color2) {
- // Assumption: "color1" and "color2" need not to be validated
- var r1 = parseInt(color1.substr(0, 2), 16),
- g1 = parseInt(color1.substr(2, 2), 16),
- b1 = parseInt(color1.substr(4, 2), 16);
- var r2 = parseInt(color2.substr(0, 2), 16),
- g2 = parseInt(color2.substr(2, 2), 16),
- b2 = parseInt(color2.substr(4, 2), 16);
- var r3 = Math.round(0.5 * (r1 + r2)),
- g3 = Math.round(0.5 * (g1 + g2)),
- b3 = Math.round(0.5 * (b1 + b2));
- return (r3 * 65536 + g3 * 256 + b3).toString(16);
- }; // var average_color = function(color1, color2) { ... };
- console.log(average_color('135246', 'acfbe0'));
Add Comment
Please, Sign In to add comment