开发者

Range of .ToARGB() return values

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开发者_Python百科 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 0x00000000 to 0xFFFFFFFF, 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.


quoting from MSDN

The byte-ordering of the 32-bit ARGB value is AARRGGBB. The most significant byte (MSB), represented by AA, is the alpha component value. The second, third, and fourth bytes, represented by RR, GG, and BB, respectively, are the color components red, green, and blue, respectively

It is a 32-bit field. first 8 bits are for alpha channel. When the MSB is 1, it will always give you back a negative value. When it becomes 0, then you can get positive values. I hope you know about how values are stored and how sign bits affect it?

when AA drops down to 0xxxxxxxb from 11111111b, then expect to get positive values.

IMHO Nullable fields are a way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜