Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Color objects

This is VS2010 and .NET 4.0. I'm trying to compare two System.Drawing.Color objects.

The value of mStartColor.ToArgb() is 16777215.

The value of Color.Transparent.ToArgb() is 16777215.

The value of mStartColor <> Color.Transparent is True.

How is equality implemented for Color objects?

EDIT

Thanks everyone. I got my answer, though it doesn't make much sense to me (see my comments to Tim's and Dave's answers below). I'll mark Tim's post as the answer and he was the first to reply, but Dave's answer is equally informative.

like image 244
dotNET Avatar asked Jan 08 '14 11:01

dotNET


People also ask

How do you compare two colors?

The most common method would be a visual color comparison by looking at two physical color samples side by side under a light source. Color is very relative, so you can compare colors in terms of the other color across dimensions such as hue, lightness and saturation (brightness).

How can I compare two colors in C#?

Equals or Color- == is used everywhere. So even if a color look like another it's not the same if it hasn't for example the same name or the same KnownColor .


1 Answers

Always read the documentation first:

"To compare colors based solely on their ARGB values, you should use the ToArgb method. This is because the Equals and Equality members determine equivalency using more than just the ARGB value of the colors. For example, Black and FromArgb(0,0,0) are not considered equal, since Black is a named color and FromArgb(0,0,0) is not"

like image 182
Tim Schmelter Avatar answered Oct 18 '22 08:10

Tim Schmelter