I want to be able to store a backgroundcolor for a panel in my application in the database.
The value can be either a concrete color or empty, indicating it has been inherited from the parent.
I wanted to store this in an int-field that is non-nullable, by calling the System.Drawing.Color.ToArgb() method.
The return value of this method seems to be -1 for white, and -16777216 for black…
So I was thinking that for indicating the no color has been selected, I could store a positive integer. This would offer a very simple way to check whether an explicit color has been stored or no color has been stored.
Now I was wondering whether there is a legimitate reason why System.Drawing.Color.ToArgb() could ever return positive values. (What’s the range of possible return values for this function).
I haven’t been tampering around with the alpha channel, so I don’t know whether that would have influence….
If I’m not able to store a positive integer to indicate the abscence of an explicit color, I guess I will just have to make the field nullable….
Well, you get a 32-bit integer containing Alpha, Red, Green and Blue, each of them filling 8 bits.
The alpha value is in the highest-order 8 bits so for every non-transparent color you have those at
0xFF(which automatically makes that integer negative, if it’s a signed integer).So if your alpha value drops below 128 (
0x80) you will get positive color values.The range of possible return values for that function is indeed
0x00000000to0xFFFFFFFF, in other words: every possible 32-bit integer. For a signed int this is −2147483648 to +2147483647.In your case I’d definitely go with allowing NULL values, since that’s actually what you want here. If you drop the alpha value altogether you have another 8 bits for information, though. So you could then implement it checking for negative/non-negative values.