Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare a Color by the GetPixel Method and a Color passed in a method like Color.Black?

I have a loop that gets pixelcolors from an image and try to see if they are the same as the Color I passed into the method as parameter.

I tried the Equals method but it doesn't work. I also tried the ToKnown method. It looks like that match doesn't work beacuse the values that synthesize the two colors don't match.

Example:

With GetPixel:

{Name=ff000000, ARGB=(255, 0, 0, 0)}

Color.Black:

{Name=Black, ARGB=(255, 0, 0, 0)}
if (pixelColor.ToArgb().Equals(startingOffsetColor.ToArgb())) { }

The code above works, but I still want to know if there is any better method or any method that can reduce any CPU overhead, because I'm using this inside a loop statement.

like image 472
Relok Avatar asked Dec 03 '22 07:12

Relok


1 Answers

According to MSDN, the Color.Equality Operator...

...compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method

So the method that you are using is correct for comparing the raw values

EDIT

.ToArgb() returns an int so you can just use == for comparison, you don't need to use .Equals() if you find it too verbose.

like image 78
Chris Haas Avatar answered Dec 28 '22 23:12

Chris Haas