Last active
August 29, 2015 13:56
-
-
Save Calvein/9124938 to your computer and use it in GitHub Desktop.
Get the average of colors (#123456)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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