Get Average Colour From Texture?


Is it possible to get grab every pixel colour from a texture within the scene (applied to a quad), and find the average colour that is being used?

I ask because I want to know how ‘bright’ the texture is, which will in turn will affect the intensity of a spotlight. Eg: if the texture is fairly bright, and over exposed on average, then the spotlight intensity will be pretty bright and strong. And vise versa.


To calculate the brightness of a color you could try this method and then calculate the average brightness of all returned values (not tested):

private int Brightness(Color c)
    return (int)Mathf.Sqrt(
       c.R * c.R * .241 + 
       c.G * c.G * .691 + 
       c.B * c.B * .068);


To get the pixels of a Texture2D just call GetPixels() or GetPixels32()

Texture2D destTex = new Texture2D(width, height);
Color[] pixels = destTex.GetPixels();