Runtime: System.Drawing.Color equality is comparing more than the color

Created on 10 Jul 2019  路  3Comments  路  Source: dotnet/runtime

I figure this was done as originally designed and probably has a whole lot of legacy code tied to it to be this particular way but it doesn't really make sense to me.

Long story short:

Color.Red == Color.FromArgb(255, 255, 0, 0); //False
Color.Red.ToArgb() == Color.FromArgb(255, 255, 0, 0).ToArgb(); //True

So while Color.Red is made up of Alpha 255, Red 255, Green 0 and Blue 0, it isn't the same as creating the color via FromArgb due to the equality check actually checking whether it is a known color and checking the name of the color.

https://github.com/dotnet/corefx/blob/aef8ed681c53f0e04733878e240c072036dd6679/src/System.Drawing.Primitives/src/System/Drawing/Color.cs#L563-L567

What I am thinking is FromArgb to actually look up the corresponding known color and name information and set it. As best as I can tell, all the information needed to do it exists in the KnownColorTable but the lookup is reversed.

Thoughts? Is this just a dumb idea and I should use the value of ToArgb to compare colours?

area-System.Drawing

Most helpful comment

While that makes some sense in terms of SystemColors, it seems like a misnomer that the class is Color where two colours that are visually the same are not actually equal to each other. There's got to be a better way to handle this...

All 3 comments

This is intentional. The known colors are also used for the system colors. Specifically, SystemColors.Control is not supposed to be equal to Color.Silver even if the ARGB value matches at the moment. Named color are just weird special case that is not very intuitive.

While that makes some sense in terms of SystemColors, it seems like a misnomer that the class is Color where two colours that are visually the same are not actually equal to each other. There's got to be a better way to handle this...

This issue may become irrelevant if System.Drawing.Color is replaced. https://github.com/dotnet/runtime/issues/14825

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jchannon picture jchannon  路  3Comments

yahorsi picture yahorsi  路  3Comments

omariom picture omariom  路  3Comments

Timovzl picture Timovzl  路  3Comments

matty-hall picture matty-hall  路  3Comments