Skip to content

Instantly share code, notes, and snippets.

@jancassio
Last active September 25, 2019 23:02
Show Gist options
  • Save jancassio/f0d84a69f31b654b10b717d8922b3034 to your computer and use it in GitHub Desktop.
Save jancassio/f0d84a69f31b654b10b717d8922b3034 to your computer and use it in GitHub Desktop.
Get average color of an image
color averageColor(PImage image) {
float r = 0;
float g = 0;
float b = 0;
int count = image.pixels.length;
int[] pixels = image.pixels;
for(int i = 0; i < count; i++) {
r += red(pixels[i]);
g += green(pixels[i]);
b += blue(pixels[i]);
}
r = floor(r / (count / 4));
g = floor(g / (count / 4));
b = floor(b / (count / 4));
return color(r, g, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment