1

System.Drawing.Color has a private field int state which makes equality a bit more tricky than one would expect from a struct.

Anyone know what on earth it's for? Who, what and why sets and reads it?

Lars-Erik
  • 301
  • 3
  • 17
  • color just has a empty field.http://msdn.microsoft.com/en-us/library/14w97wkc.aspx – Prabhu Murthy Oct 31 '12 at 16:50
  • My understanding is that the Color structure just has an `empty` field. http://msdn.microsoft.com/en-us/library/system.drawing.color_fields.aspx. But in .NET 3.5 it appears to be used internally as CSharpie mentions. – Chimera Oct 31 '12 at 16:54

2 Answers2

4

As far as i understand, it is compared to theese values:

    private static short StateKnownColorValid   = 0x0001;
    private static short StateARGBValueValid    = 0x0002;
    private static short StateValueMask         = (short)(StateARGBValueValid);
    private static short StateNameValid         = 0x0008;
    private static long NotDefinedValue = 0;

http://reflector.webtropy.com/default.aspx/Net/Net/3@5@50727@3053/DEVDIV/depot/DevDiv/releases/whidbey/netfxsp/ndp/fx/src/CommonUI/System/Drawing/Color@cs/1/Color@cs

So my shot is that its used to dertermine if its a "System Color" or a user defined from e.g. ARGB values.

public bool IsKnownColor 
{
    get { return((state & StateKnownColorValid) != 0);}
}
CSharpie
  • 9,195
  • 4
  • 44
  • 71
  • Ah, thanks. :) Should've decompiled myself.. Have to dig around a bit more the next time I hit my head against ==.. – Lars-Erik Nov 09 '12 at 09:53
1

The Color struct overrides the Equals method and does therefore automatically do the right thing when colors are tested for equality.

The Equals method compares the value, state, knownColor and name fields.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188