Color.FromArgb(int argb)

  • Thread starter Thread starter Olivier Gaumond
  • Start date Start date
O

Olivier Gaumond

I think the following overload of Color.FromArgb should have a uint
parameter instead of int.

----
From MSDN:
Creates a Color structure from a 32-bit ARGB value.

public static Color FromArgb(
int argb
);

Parameters
argb
A value specifying the 32-bit ARGB value.
----

What if I want to get a color without an alpha component, I would have
to make this call for green.
Color color = Color.FromArgb(0xFF00FF00);

However 0xFF00FF00 is greater than Int32.MaxValue then I get an
OverflowException et runtime or a compile time error (Argument '1':
cannot convert form 'uint' to 'int')

I know I can use another overload but I still think the API is
incorrect.

Any workaround around this?

Olivier
 
Olivier Gaumond said:
I think the following overload of Color.FromArgb should have a uint
parameter instead of int.

That would make it CLS-non-compliant though, which is unlikely to be a
good thing.

I would suggest an overload so that there *was* a version with a uint
parameter *as well as* the version with an int parameter.
----
From MSDN:
Creates a Color structure from a 32-bit ARGB value.

public static Color FromArgb(
int argb
);

Parameters
argb
A value specifying the 32-bit ARGB value.
----

What if I want to get a color without an alpha component, I would have
to make this call for green.
Color color = Color.FromArgb(0xFF00FF00);

However 0xFF00FF00 is greater than Int32.MaxValue then I get an
OverflowException et runtime or a compile time error (Argument '1':
cannot convert form 'uint' to 'int')

I know I can use another overload but I still think the API is
incorrect.

Any workaround around this?

Well, you could make the conversion unchecked at compile-time:

Color color = Color.FromArgb(unchecked((int)0xFF00FF00));
 
Back
Top