Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How do I convert to Hex?

Reply
Thread Tools

How do I convert to Hex?

 
 
JJ
Guest
Posts: n/a
 
      05-17-2007
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


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      05-17-2007
"JJ" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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();
}



--
http://www.markrae.net

 
Reply With Quote
 
 
 
 
JJ
Guest
Posts: n/a
 
      05-17-2007
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


"Mark Rae" <(E-Mail Removed)> wrote in message
news:OR%(E-Mail Removed)...
> "JJ" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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();
> }
>
>
>
> --
> http://www.markrae.net



 
Reply With Quote
 
JJ
Guest
Posts: n/a
 
      05-17-2007
OK that didn't work.

Think I'll look at this again tomorrow.

JJ





"JJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Mark Rae" <(E-Mail Removed)> wrote in message
> news:OR%(E-Mail Removed)...
>> "JJ" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>>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();
>> }
>>
>>
>>
>> --
>> http://www.markrae.net

>
>



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      05-18-2007
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("#{06}", theInteger);

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


JJ wrote:
> OK that didn't work.
>
> Think I'll look at this again tomorrow.
>
> JJ
>
>
>
>
>
> "JJ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>> "Mark Rae" <(E-Mail Removed)> wrote in message
>> news:OR%(E-Mail Removed)...
>>> "JJ" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>>> 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();
>>> }
>>>
>>>
>>>
>>> --
>>> http://www.markrae.net

>>

 
Reply With Quote
 
JJ
Guest
Posts: n/a
 
      05-18-2007
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

"Göran Andersson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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("#{06}", theInteger);
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
>
>
> JJ wrote:
>> OK that didn't work.
>>
>> Think I'll look at this again tomorrow.
>>
>> JJ
>>
>>
>>
>>
>>
>> "JJ" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>
>>>
>>> "Mark Rae" <(E-Mail Removed)> wrote in message
>>> news:OR%(E-Mail Removed)...
>>>> "JJ" <(E-Mail Removed)> wrote in message
>>>> news:%(E-Mail Removed)...
>>>>> 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();
>>>> }
>>>>
>>>>
>>>>
>>>> --
>>>> http://www.markrae.net
>>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments