I don't think there is a good reason. First, I thought that digging into the code would provide some insight, but all I can find is that there are checks to ensure that the values of alpha
, red
, green
and blue
are within the [0..255] range, throwing an exception if not. Internally, the MakeArgb
method is then called, which does use byte
:
/// <summary>
/// [...]
/// Although this method allows a 32-bit value
/// to be passed for each component, the value of each
/// component is limited to 8 bits.
/// </summary>
public static Color FromArgb(int alpha, int red, int green, int blue)
{
Color.CheckByte(alpha, "alpha");
Color.CheckByte(red, "red");
Color.CheckByte(green, "green");
Color.CheckByte(blue, "blue");
return new Color(Color.MakeArgb((byte)alpha, (byte)red, (byte)green, (byte)blue), Color.StateARGBValueValid, null, (KnownColor)0);
}
private static long MakeArgb(byte alpha, byte red, byte green, byte blue)
{
return (long)((ulong)((int)red << 16 | (int)green << 8 | (int)blue | (int)alpha << 24) & (ulong)-1);
}
private static void CheckByte(int value, string name)
{
if (value < 0 || value > 255)
{
throw new ArgumentException(SR.GetString("InvalidEx2BoundArgument",
name,
value,
0,
255));
}
}
I guess it just became that way in the early days (we're talking .NET 1.0 here) and then it just stuck.
Also, there is the FromArgb(int)
overload, which lets you set all 32 bits with one 32 bit value. Strangely, this is an int
and not a unsigned int
.