Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Color.FromArgb(255, 255, 255, 255) != Color.White?

Tags:

.net

gdi+

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.

like image 433
abenci Avatar asked Nov 16 '10 14:11

abenci


2 Answers

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.

like image 126
Nick Avatar answered Nov 16 '22 03:11

Nick


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();
    }
}
like image 41
Dan Tao Avatar answered Nov 16 '22 03:11

Dan Tao