[Mono-list] System.Drawing.Color.FromArgb(), why int parameters?

Weeble clockworksaint at gmail.com
Thu Aug 25 04:50:01 EDT 2011


On Thu, Aug 25, 2011 at 7:15 AM, Stifu <stifu at free.fr> wrote:
> I have a quick question about the System.Drawing.Color.FromArgb() method
> (which also applies to other similar methods).
>
> http://msdn.microsoft.com/en-us/library/at1k42eh.aspx
>
> Does anyone know why it takes int parameters, rather than bytes, considering
> expected values should go from 0 to 255?

I might guess this is related to the lack of byte operators. If you're
going to do any arithmetic on your bytes before you feed them into
FromArgb, you'll find you keep needing to cast back to byte, because
all the arithmetic will use int. As I see it you have these options:

* Write the API with bytes, use slightly verbose (byte)(a+b) casts
during arithmetic. If you screw up and overflow you get weird results.
* Write the API with bytes, use very verbose checked((byte)(a+b))
casts. If you overflow you get a runtime exception.
* Write the API with ints. No verbose casts are required. If you go
out of range then FromArgb will throw a runtime exception.

So, if you're doing arithmetic to generate the arguments for FromArgb,
it seems slightly less painful and no more unsafe for the API to use
ints. Not a deeply compelling reason, but perhaps worth consideration.


More information about the Mono-list mailing list