Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Created August 1, 2014 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DomDomHaas/ea3c2b8f5cdb0c66ee91 to your computer and use it in GitHub Desktop.
Save DomDomHaas/ea3c2b8f5cdb0c66ee91 to your computer and use it in GitHub Desktop.
get the average RGB form a texture
private Color getAverageRGB (Texture2D tex)
{
Color[] allPixels = tex.GetPixels (0, 0, tex.width, tex.height);
float r = 0;
float g = 0;
float b = 0;
int interationSteps = 1;
for (int i = 0; i < allPixels.Length; i += interationSteps) {
Color pixColor = allPixels [i];
r += pixColor.r;
g += pixColor.g;
b += pixColor.b;
// for (int j = 0; j < tex.height; j += interationSteps) {
// }
}
//Calculate average
r /= allPixels.Length;
g /= allPixels.Length;
b /= allPixels.Length;
return new Color (r, g, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment