conversion of color to hex string

Discussion in 'ASP .Net' started by expertware, Sep 29, 2005.

  1. expertware

    expertware Guest

    To conver a color to its hexadecimal string I have devised
    this way:

    Function HexStringFromColor(ByVal Colore As Color) As String
    Return "#" & Colore.FromArgb(Colore.A,
    Colore).Name.Substring(2).ToUpper
    End Function

    but it seems a little bit ackward. I would like to ask if there
    is a better way (colortranslator does not seem to have this function,
    but I may be missing it).

    -Pamela
     
    expertware, Sep 29, 2005
    #1
    1. Advertisements

  2. Not bad. Perhaps:

    Function HexStringFromColor(ByVal Colore As Color) As String
    Return "#" & Colore.ToArgb().ToString("X").Substring(2)
    End Function

    The ToArgb() method returns a 32-bit number containing the A,R,G, and B
    components of the color. The Int32.ToString(string) method converts an Int32
    to a string using the format string provided. The "X" format string converts
    the Int32 to a string representation of hexadecimal notation for the number.
    And the Substring() removes the Alpha hex value from the string.

    But it isn't much more succinct than yours. I don't think it can be.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Big things are made up of
    lots of little things.
     
    Kevin Spencer, Sep 29, 2005
    #2
    1. Advertisements

  3. expertware

    Phillip Ian Guest

    ColorTranslator.ToHtml should do the trick.
     
    Phillip Ian, Sep 29, 2005
    #3
  4. Darn! I should have thought of that!

    --
    :p,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Big things are made up of
    lots of little things.
     
    Kevin Spencer, Sep 29, 2005
    #4
  5. expertware

    expertware Guest

    This one also nice, but still feel ackward.

    I can't believe there isn't a straight way to do such
    a common task.

    -Pamela

    PS. no would be too easy! ColorTranslator.ToHtml
    is NOT the same . It yelds the color name if it exists

    * I always want the HEX *

    eehi mpv !
     
    expertware, Sep 29, 2005
    #5
  6. Good point!

    --

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Big things are made up of
    lots of little things.
     
    Kevin Spencer, Sep 29, 2005
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.