Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Color.FromArgb take Int32 as parameter?

Tags:

c#

colors

argb

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?

like image 543
user1151923 Avatar asked Sep 11 '14 15:09

user1151923


2 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));
like image 176
Daws Avatar answered Oct 13 '22 22:10

Daws


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

like image 43
Icemanind Avatar answered Oct 13 '22 21:10

Icemanind