The Color.FromArgb method takes Int32
as a parameter. The value of Color.White is #FFFFFFFF
as ARGB, which is 4.294.967.295
as decimal (way over int.MaxValue
). What am I not understanding here? How can the method take int
as a parameter if valid ARGB values are above the maximum value of an int
?

- 32,158
- 14
- 82
- 96

- 1,853
- 6
- 28
- 44
-
I guess it boils down to `int` being more convenient/common than `uint` and .NET is being easy on everybody, here.. – TaW Sep 11 '14 at 15:47
-
They just needed 32 bits to encode four 8-bit values. What name it has wasn't that important, as long as any language can use it. – Hans Passant Sep 11 '14 at 21:42
-
You are absolutely right, they screwed up. It should've been uint. And By the way, language does not come into the picture here. It should be fine to `Color.FromArgb(0x80ffffff)`, but the conversion will see overflow. Now you have to use `unchecked`. – Kay Zed Mar 25 '23 at 07:54
8 Answers
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));

- 692
- 4
- 11
-
3No, you can feed it the value as int. You can't feed it as a constant, though, as the compiler jumps on that but the runtime is ok with it.. This works: `long l = 0xffffffff; Color c = Color.FromArgb( (int)l );` this doesn't: `const long l = 0xffffffff; Color c = Color.FromArgb( (int)l );` – TaW Sep 11 '14 at 16:02
-
3I have found that `Color.FromArgb(255, Color.FromArgb(0x992E11));` works. – refeniz Nov 07 '18 at 04:49
-
@TaW Technically, `unchecked` is still required even during runtime. It's just that most C# code is compiled with unchecked as the implicit default. If you configure your project to build with overflow checks, your code would fail during runtime. I agree that it would be very evil if anyone ever actually sets that compiler flag, but I also believe that you should clearly state your intent and put all operations that are expected to overflow into an explicit unchecked context. – relatively_random Jan 18 '22 at 09:07
-
If it's a fully opaque color (i.e. Alpha = 255), you can also use: `Color color = Color.FromArgb(0xFF, 0x60, 0x60);` If the color has transparency, add a new parameter in the beginning like this: `Color color = Color.FromArgb(128, 0xFF, 0x60, 0x60)`. – Pavel Vladov Mar 13 '23 at 15:49
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.

- 321
- 1
- 9
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);

- 4,807
- 1
- 23
- 44
According to the MSDN page for Color.FromArgb Method (Int32), you don't pass in the actual int
value for the color. For example, to get red you would call Color.FromArgb(0x78FF0000)
. So, for white, you should just need to call Color.FromArgb(0xFFFFFFFF)
.

- 2,283
- 1
- 21
- 26
A Color
is made up of four important fields, A
(alpha), R
(red), G
(green), and B
(blue). Each of these is eight bits. four eight-bit values fit into an int32. Although the MSB may be the sign bit, that is ignored.
0xFFFFFFFF
may be a negative number when expressed as an int
, but it's white as a color.

- 3,711
- 21
- 28
It doesn't matter.
#FFFFFFFF is 11111111111111111111111111111111 in binary.
In decimal it is 4.294.967.295 if you're using unsigned ints. If you're using signed ints, it is interpreted as -1.
But the actual decimal value doesn't matter; what matters is the value of the individual bits.
A signed int can still store 4.294.967.295 values, just half of them are negative. The bits are the same.

- 8,751
- 23
- 29
You can wright 0x00FFFFFF-0x01000000 compiler will work correct
-
You need to add some more explanation regarding your answer. For help on crafting good answers, checkout this [link](https://stackoverflow.com/help/how-to-answer). – technogeek1995 Jul 18 '19 at 18:38