Today I am trying to check if one color is similar to another in CSharp from BitMap. 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. 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.