Dec 6, 2006

Colors and Hex: Back to the very basics

Today I've been doing a WinForms aplication at work and I want to reuse a color scheme for a web site. Since WinForms don't allow you to use css :) I've to convert the HTML colors (#006600) to System.Drawing.Colors.

You can do that with one of the thousands Color Calculators but I was expecting the Color class having a method like FromHTML. It was't there but I found something like this.

static Color FromArgb(int argb);

So, just writing FromArgb(0x006600) it should work... No way. The problem is that in this situation the alpha channel has the value 0, so the color is transparent.

All right, let's try FromArgb(0xFF006600). Doesn't compile! it's a uint instead of a int because is bigger than int.MaxInt, the compiler it's right, but the one who made this function should make a uint overload. It's the problem of sharing framework with VB people.

So... What can I do? Because int uses Two's_complement to represent negative values, we can do that: FromArgb(-0x01000000 0x006600) because -0x01000000 will be represented just as 0xFF000000.

Using the first day computer science lecture to pick a color!.

No comments: