WebControl.BackColor

  • Thread starter Thread starter francois
  • Start date Start date
F

francois

Hi all,

The WebControl.BackColor expect a Drawing.Color object which i find extremly
inconvenient to use as mainly in the web world we use things like "#CC1235"
to define a color, the hexadecimal code.

Then my question is:
How can I give a value to WebControl.BackColor if all I got is a string
containing the hexadecimal value like "#CC1235" ?

I assume I need to create and instance of Color object somehow but how? Also
I have seen the classes WebColorConverter but how to use it? Is it
something that I could use for my case?

Thanks a lot,

Francois
 
Hi, Francois,

It is not that hard:

public static Color ConvertToColor(string color)
{
int c = Convert.ToInt32(color.Replace("#", "0x"), 16);
return Color.FromArgb(
(c & 0xFF0000) >> 16,
(c & 0x00FF00) >> 8,
c & 0x0000FF);
}

I agree that it is not very web-oriented.

Hope this helps
Martin
 
Hi Martin,

Then you propose me to create my own utility class isn't it?
Quite amazing that such a basic need for web developer has not been thought
of before

Anyway thank you for your method, it seems it will do the job perfectly

Best ragards,

Francois
 
Hi, Francois,

Another way is to use the TypeDescriptor and it is in one row (sort of):

System.Drawing.Color color =
(System.Drawing.Color)System.ComponentModel.TypeDescriptor.GetConverter(type
of(System.Drawing.Color)).ConvertFrom("#CC1235");

Greetings
Martin
 
Hello!
How can I give a value to WebControl.BackColor if all I got is a string
containing the hexadecimal value like "#CC1235" ?

I assume I need to create and instance of Color object somehow but how? Also
I have seen the classes WebColorConverter but how to use it? Is it
something that I could use for my case?

why not use the ColorTranslator class found in System.Drawing?

e.g.
Color myColor=ColorTranslator.FromHtml("#CC1235");

That's what I like about .Net, there's everything you need, you only have to
know the framework.

matthias
 
Great !! Thanx a lot for that one. It was not easy to find out in my opinion
but maybe it is because I never try to went through all packages and
classes.

Francois
 
Back
Top