简体   繁体   English

比较颜色与公差

[英]Compare colors with toleration

Today I am trying to check if one color is similar to another in CSharp from BitMap. 今天,我尝试检查BitMap中CSharp中的一种颜色是否与另一种颜色相似。 This is code, what I am using: 这是代码,我正在使用:

Color blah = screenshot.GetPixel(x, y);
if (blah == Color.Red) {
...

The problem is, that I never get true , because the color has a little bit different shade. 问题是,我从来没有得到过true ,因为颜色的阴影有点不同。 Is there any way to compare this colors with some tolerance? 有什么办法可以比较这种颜色的公差吗?

Thanks! 谢谢!

You may check defince a tolarance value and check if their difference is less than that: 您可以检查定义容忍度值,并检查它们的差值是否小于此值:

Color blah = screenshot.GetPixel(x, y);
    if (Math.Abs(Color.Red.GetHue() - blah.GetHue()) <= tolorance)
    {
        // ...
    }

Maybe a better solution: 也许是更好的解决方案:

 public bool AreColorsSimilar(Color c1, Color c2, int tolerance)
 {
     return Math.Abs(c1.R - c2.R) < tolerance &&
            Math.Abs(c1.G - c2.G) < tolerance &&
            Math.Abs(c1.B - c2.B) < tolerance;
 }

Source: 资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM