百分比到Argb颜色值 [英] percent to Argb color value

查看:97
本文介绍了百分比到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);
    }
}

推荐答案

FromArgb 方法假设每像素32位像素格式(仅),每个颜色分量8位和A(不透明度)。

这意味着最大颜色分量值是 byte.MaximumValue == 0xFF == 255 ,相当于100%。最小值为0,对应0%。可以做数学吗?



-SA
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


从你的描述中我会假设你想要计算一个像素的亮度,并根据它设置一个叠加颜色的范围。



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.

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颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆