How can Color.FromArgb take Int32 as parameter?

The practical problem is that you want to enter a eight-digit hexadecimal number, but because the single-parameter version uses an int, rather than a uint, it is difficult to represent colours with an Alpha value above &7F. This is because an int uses one bit to represent the sign.

The easiest solution is to use the four-parameter version:

var whiteColor = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);

Your confusion lies in signage. Although Int32.MaxValue is equal to 2,147,483,647, that is signed.

If you look at UInt32.MaxValue, that is unsigned and as you can see, the maximum value is 4,294,967,295.

You see, signed numbers, in binary, use the left most bit to determine if its a positive or negative number. Unsigned numbers, in binary, don't have a signed bit and make use of that last bit, giving you essentially double the storage capacity.

i think part of the reason that the Color class uses Int32 instead of unsigned is because unsigned int's aren't CLS compliant, as stated in this SO Question


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.

http://msdn.microsoft.com/en-us/library/2zys7833(v=vs.110).aspx

It appears that the method breaks the int32 into 32 bits and converts that to AARRGGBB which is two nibbles (1 byte) for each parameter A, R, G, and B.

This works because each digit in FFFFFFFF in hexadecimal converts to a single nibble. Each space equals 4 bits specifically. So, this bit representation converts directly to 32 bits, which can be represented as a single int32.

To give just a little more detail:

The maximum value of a space in hexadecimal is F (or 15 in decimal).

The maximum value of 4 bits ( 1 nibble) is (8, 4, 2, 1) which is also 15.

So, FFFFFFFF = 1111 1111 1111 1111 1111 1111 1111 1111 which is then represented as an int32 .

AS @icemanind pointed out, the first bit is reserved for the sign (+ or -), and therefore limits the numeric value of the int32 to 2,147,483,647.

It's not the numeric value, but the bit values that are important for this method.


Unfortunately, since Color.FromArgb takes an int instead of a uint, you will need to use the unchecked keyword for colors that are greater than int.MaxValue.

var white = Color.FromArgb(unchecked((int)0xFFFFFFFF));

Tags:

C#

Colors

Argb