WebControl.BackColor

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
 
M

Martin Dechev

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
 
F

francois

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
 
M

Martin Dechev

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
 
M

Matthias Straka

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
 
F

francois

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top