Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to find Gray color percentage from ARGB value?

Actually I want to set color according to Gray color percentage in an image.

I wrote following code but it is not working so I thought if I get percentage of colour then it could be better option.
C#
Color compareClr = Color.FromArgb(75, 75, 75);
Color compareClr2 = Color.FromArgb(90, 90, 90);
Color compareClr3 = Color.FromArgb(105, 105, 105);
Color compareClr4 = Color.FromArgb(150, 150, 150);
for (int y = 0; y < lockBitmap.Height; y++)
{
    for (int x = 0; x < lockBitmap.Width; x++)
    {
        double d= Color.LightGray.GetHue();
        Console.WriteLine("GetHue " + d);
        //Console.WriteLine((System.Drawing.Color.LightGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " LightGray");
        //Console.WriteLine((System.Drawing.Color.Gray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " Gray");
        //Console.WriteLine((System.Drawing.Color.DarkGray.ToArgb() == lockBitmap.GetPixel(x, y).ToArgb()) + " DarkGray");
        if (lockBitmap.GetPixel(x, y).ToArgb() == compareClr.ToArgb())
            lockBitmap.SetPixel(x, y, Color.Cyan);
        else if (lockBitmap.GetPixel(x, y).ToArgb() == compareClr2.ToArgb())
            lockBitmap.SetPixel(x, y, Color.Green);
        else if (lockBitmap.GetPixel(x, y).ToArgb() == compareClr3.ToArgb())
            lockBitmap.SetPixel(x, y, Color.Yellow);
        else
            lockBitmap.SetPixel(x, y, Color.Blue);
    }
}
Posted
Updated 19-Mar-13 2:06am
v3
Comments
CPallini 19-Mar-13 10:42am    
"Actually I want to set color according to Gray color percentage in an image."
What do you mean, exactly?

The FromArgb methods assume 32-bits-per-pixel pixel format (only), 8 bits per each color component and A (opacity).
It means that the maximum color component value is byte.MaximumValue == 0xFF == 255, which corresponds to 100%. And minimum value is 0, corresponds to 0%. Can do the math?

—SA
 
Share this answer
 
From your description I would assume you want to calculate the luminance of a pixel and depending in which range it is set an overlay color.

C#
for (int y = 0; y < lockBitmap.Height; y++)
{
   for (int x = 0; x < lockBitmap.Width; x++)
   {
      // get the color of the pixel
      Color pix = lockBitmap.GetPixel(x, y);
      // calculate luminance from the components
      // result will be in the range [0.0, 255.0]
      double lum = (double)pix.R*0.3+(double)pix.G*0.59+(double)pix.B*0.11;
      // color according to the luminance ranges
      if(lum < 75.0) {
        lockBitmap.SetPixel(x, y, Color.Cyan);
      } else if (lum < 90.0) {
        lockBitmap.SetPixel(x, y, Color.Green);
      } else if (lum < 105.0) {
        lockBitmap.SetPixel(x, y, Color.Yellow);
      } else {
        lockBitmap.SetPixel(x, y, Color.Blue);
      }
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900