How do I convert to Hex?

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
 
M

Mark Rae

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();
}
 
J

JJ

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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
 
J

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top