Skip to content

Instantly share code, notes, and snippets.

@Calvein
Last active August 29, 2015 13:56
Show Gist options
  • Save Calvein/9124938 to your computer and use it in GitHub Desktop.
Save Calvein/9124938 to your computer and use it in GitHub Desktop.
Get the average of colors (#123456)
averaveColor = (colors) ->
d2h = (d) -> d.toString(16)
h2d = (h) -> parseInt(h, 16)
r = g = b = 0
for color in colors
r += h2d(color.slice(1, 3))
g += h2d(color.slice(3, 5))
b += h2d(color.slice(5))
r = r // colors.length
g = g // colors.length
b = b // colors.length
color = '#'
color += d2h(r)
color += d2h(g)
color += d2h(b)
return color
var averaveColor;
averaveColor = function(colors) {
var b, color, d2h, g, h2d, r, _i, _len;
d2h = function(d) {
return d.toString(16);
};
h2d = function(h) {
return parseInt(h, 16);
};
r = g = b = 0;
for (_i = 0, _len = colors.length; _i < _len; _i++) {
color = colors[_i];
r += h2d(color.slice(1, 3));
g += h2d(color.slice(3, 5));
b += h2d(color.slice(5));
}
r = Math.floor(r / colors.length);
g = Math.floor(g / colors.length);
b = Math.floor(b / colors.length);
color = '#';
color += d2h(r);
color += d2h(g);
color += d2h(b);
return color;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment