- Home /
Get Average Colour From Texture?
Hello,
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.
Thanks
Answer by robhuhn · Jan 09, 2014 at 03:55 PM
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);
}
@Edit
To get the pixels of a Texture2D just call GetPixels() or GetPixels32()
Texture2D destTex = new Texture2D(width, height);
Color[] pixels = destTex.GetPixels();
sources: http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixels.html
Okay, thanks - but how do I actually pull the pixel data to begin with?
Your answer
Follow this Question
Related Questions
How can I change all of a textures pixels to one color? 1 Answer
Get pixel from Rotated Texture2D 1 Answer
How to pixelate an image texture for faster pixel color recognition? 2 Answers
How can i avoid mouse clicking on alpha areas in my GUITexture? 1 Answer
Duplicated objects, different colors 1 Answer