How do I convert to Hex?

Discussion in 'ASP .Net' started by JJ, May 17, 2007.

  1. JJ

    JJ Guest

    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, May 17, 2007
    #1
    1. Advertising

  2. JJ

    Mark Rae Guest

    "JJ" <> wrote in message
    news:%...
    >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
     
    Mark Rae, May 17, 2007
    #2
    1. Advertising

  3. JJ

    JJ Guest

    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" <> wrote in message
    news:OR%...
    > "JJ" <> wrote in message
    > news:%...
    >>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
     
    JJ, May 17, 2007
    #3
  4. JJ

    JJ Guest

    OK that didn't work.

    Think I'll look at this again tomorrow.

    JJ





    "JJ" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:OR%...
    >> "JJ" <> wrote in message
    >> news:%...
    >>>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

    >
    >
     
    JJ, May 17, 2007
    #4
  5. 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


    JJ wrote:
    > OK that didn't work.
    >
    > Think I'll look at this again tomorrow.
    >
    > JJ
    >
    >
    >
    >
    >
    > "JJ" <> wrote in message
    > news:...
    >> 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" <> wrote in message
    >> news:OR%...
    >>> "JJ" <> wrote in message
    >>> news:%...
    >>>> 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

    >>
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 18, 2007
    #5
  6. JJ

    JJ Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    > JJ wrote:
    >> OK that didn't work.
    >>
    >> Think I'll look at this again tomorrow.
    >>
    >> JJ
    >>
    >>
    >>
    >>
    >>
    >> "JJ" <> wrote in message
    >> news:...
    >>> 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" <> wrote in message
    >>> news:OR%...
    >>>> "JJ" <> wrote in message
    >>>> news:%...
    >>>>> 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
    >>>
     
    JJ, May 18, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,396
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    527
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    616
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,131
  5. chirs
    Replies:
    3
    Views:
    191
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page