Why Color.FromArgb(255, 255, 255, 255) != Color.White?
Why Color.FromArgb(255, 255, 255, 255) is not equal to 开发者_运维技巧Color.White ? Is there any built-in way to compare only A,R,G,B values and not color names?
Thanks.
See http://msdn.microsoft.com/en-us/library/e03x8ct2(VS.85).aspx
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.
To add to Nick's (correct) answer: if you really wanted, you could write your own IEqualityComparer<Color>
implementation and use that in, e.g., any algorithms you may be writing that deal with colors, where you want flexibility when it comes to color equality determination.
You know, something like:
public class ColorComparer : IEqualityComparer<Color>
{
public bool Equals(Color x, Color y)
{
return x.ToArgb() == y.ToArgb();
}
public int GetHashCode(Color color)
{
return color.ToArgb();
}
}
精彩评论