Connecting Tech Pros Worldwide Forums | Help | Site Map

Comparing structs: System.Drawing.Color

Zytan
Guest
 
Posts: n/a
#1: Apr 10 '07
I wanted to do something simple like checking my own Color variable
against Color.Black, and == returned false, even though their RGB
values were all 0! Of course, there's the A (alpha) value, but that's
0, too (I assume). I guess I naively thought that == would be
overloaded to work in this case, just dealing with RGB. I guess I
have to compare R, G, and B separately.

Is this the standard way to deal with this? I know some structs just
can't be compared, but this is a color. I could even see that the
alpha channel needs to be the same, but I would think it is always 0,
unless otherwise needed.

Zytan


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Apr 10 '07

re: Comparing structs: System.Drawing.Color


Zytan <zytanlithium@gmail.comwrote:
Quote:
I wanted to do something simple like checking my own Color variable
against Color.Black, and == returned false, even though their RGB
values were all 0! Of course, there's the A (alpha) value, but that's
0, too (I assume). I guess I naively thought that == would be
overloaded to work in this case, just dealing with RGB. I guess I
have to compare R, G, and B separately.
>
Is this the standard way to deal with this? I know some structs just
can't be compared, but this is a color. I could even see that the
alpha channel needs to be the same, but I would think it is always 0,
unless otherwise needed.
From the docs:

<quote>
This structure only does comparisons with other Color structures. To
compare colors based solely on their ARGB values, you should use the
ToArgb method. This is because the Equals and op_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.
</quote>

Not exactly friendly, but at least it explains it!

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Zytan
Guest
 
Posts: n/a
#3: Apr 11 '07

re: Comparing structs: System.Drawing.Color


From the docs:
Quote:
>
<quote>
This structure only does comparisons with other Color structures. To
compare colors based solely on their ARGB values, you should use the
ToArgb method. This is because the Equals and op_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.
</quote>
>
Not exactly friendly, but at least it explains it!
Ah, yes, not friendly, at all, but yes, it does explain it, which is
nice. I had a feeling what it said was the case. Well, the evidenece
suggests it, but now I know why -- one is a named color, and the other
isn't. I was trying to find the default zero'ed value for Color, and
I assumed it would be Black or FromArgb(0,0,0).

Thanks, Jon!

Zytan

Closed Thread