本文介绍了百分比到Argb颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从ARGB值中找到灰色百分比?



实际上我想根据图像中的灰色百分比设置颜色。



我写了下面的代码,但它没有用,所以我想如果我得到颜色的百分比那么它可能是更好的选择。

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.

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);
    }
}

推荐答案


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);
      }
   }
}


这篇关于百分比到Argb颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:54