Why does this evaluate to false
?
Color.FromArgb(255, 255, 255, 255) == Color.White
Update It's by design.
Here's a copy from the decompiled Equals
function in the Color
structure:
public override bool Equals(object obj)
{
//probably failure to convert from C++ source,
//the following line should be invalid in C#, nevermind
if (obj is Color)
{
Color color = (Color) obj;
if (((this.value == color.value) &&
(this.state == color.state)) &&
(this.knownColor == color.knownColor))
{
return ((this.name == color.name) ||
(((this.name != null) && (color.name != null)) &&
this.name.Equals(this.name)));
}
}
return false;
}
My question is why on earth would MSFT have me comparing to white the ugly way?!?!?
static bool AreEqual(Color a, Color b)
{
if (!a.Equals(b))
{
return
a.A == b.A &&
a.R == b.R &&
a.G == b.G &&
a.B == b.B;
}
return true;
}
Also, another thing I don't get why the 1-arg overload of the FromArgb
function in Color
takes an int
, it should be able to take a uint
(0xffffffff
)
This is just how colors in .NET work:
Edit: additional answers.
If you're comparing colors in such a way that you want to know whether they're exactly the same color down to the values of the ARGB components, then you should be comparing them in the "ugly" way. If you're comparing the colors in the same way that most .NET programmers use the
Color
structure, then you only want to know if some color isWhite
orRed
orChartreuse
, and the "pretty" way (using Equals and ==) is simple, easy to use, and very readable.Also, the "ugly" way isn't the method you posted, it's this:
which isn't really that ugly.
I would say it should not be able to take a
uint
, thanks to the inevitable confusion and color errors that would cause. It's easy to write a method to do this.