How do I convert to Hex?

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

I have a function that returns an int that represents an rgb colour code
(e.g. '5731173').

How can I convert this to a hex value of the form #D2D2D2 so that I can use
it to color an object?

Thanks in advance,

JJ
 
JJ said:
I have a function that returns an int that represents an rgb colour code
(e.g. '5731173').

How can I convert this to a hex value of the form #D2D2D2 so that I can
use it to color an object?

Thanks in advance,

JJ


Not 100% what you're looking for, but this is how I do something very
similar:

string ColorToHex(Color pobjColor)
{
return "#" + string.Format("{0:X2}{1:X2}{2:X2}", pobjColor.R,
pobjColor.G, pobjColor.B).ToLower();
}
 
Thanks Mark.

I ended up doing it like this:

Got the r, g, b int values, then used:
theColour = System.Drawing.Color.FromArgb(r, g, b);

Which kind of avoids my problem of going from a string of the form '#D2D2D2'
to something that I can color a panel (for example) with - i.e. of type
System.Drawing.Color.

I _think_ it all works. I'm trying to duplicate colors generated from a
flash program, so I'll only know if they look the same when I get the thing
running properly....
If it doesn't work, I'll be revisiting your suggested approach (!)

Thanks,
JJ
 
You don't have to split the value into the colour components to turn it
into a Color value. Just use the integer value:

theColour = Color.FromArgb(theInteger);

If you want the html representation of the value:

string htmlColour = string.Format("#{0:x6}", theInteger);

--
Göran Andersson
_____
http://www.guffa.com

OK that didn't work.

Think I'll look at this again tomorrow.

JJ
 
Thanks - I didn't know that.
In fact thanks to you and Mark for helping. I 'm embarrassed to say that
when I looked at my code this morning I realised that the problem lied with
what I was feeding my function and not the function itself.

A new color is picked based on an int. I was feeding the function the wrong
int and couldn't work out why the colors weren't as expected.
What can I say, it was a long day....

However I am utilising what you and Mark have told be about in other areas,
and it also helped me find my error on this occassion, so thank you very
much.
JJ
 
Back
Top