Comparing structs: System.Drawing.Color

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

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
 
Zytan said:
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!
 
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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top