Results 1 to 7 of 7

Thread: HEX , HTML & CommonDialog1.Color

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2001
    Posts
    55

    HEX , HTML & CommonDialog1.Color

    Code:
    txtcolor(0).text = "#" & Right("00000" & Hex(CommonDialog1.Color), 6)
    The code above is what I am using to convert commondialog1.color to html hex values. it works on most colors but like on yellow it gives #00FFFF and on darkblue it give #80400 anyone know of a way to make the conversion work all of the time?
    -Sensimillia

  2. #2
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    This works, and for your own reference, #00FFFF is yellow.

    HexColorString = "#" & Right(Format("000000" & CStr(Hex(CommonDialog1.Color)), "000000"), 6)

    The main prob you had was that Hex() doesn't give you the right RGB string. It was the CStr which fixed it up nicely.
    I have thoroughly tested this, but it gave the right answer for some standard colours.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2001
    Posts
    55
    if #00FFFF is yellow why do I get skyblue when using this code on a web page?
    -Sensimillia

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2001
    Posts
    55
    I just tried your code and did not fixe the problem I compared the color codes in 2 different graphic editing programs and not all of the colors get the right values. Any other Ideas?
    -Sensimillia

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    you must remember that RGB in VB has to be switched to BGR for html hex as it uses reverse notation. I have posted some conversions on other threads so u can do a search on RGB and my name and see some
    regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Here is some code
    If u print Hex(vbBlue) in the debug window u get the reverse notation of FF0000. For html u need #0000FF.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim MyString As String
    3.     Dim MyNum As Long
    4.     Dim MyCalc As Long
    5.     Dim MyRemainder As Long
    6.    
    7.     MyNum = vbBlue
    8.  
    9.     MyRemainder = MyNum
    10.     MyString = ""
    11.     For x = 2 To 0 Step -1
    12.         MyCalc = MyRemainder \ 256 ^ x
    13.         MyString = Format$(Hex(MyCalc), "00") & MyString
    14.         MyRemainder = MyRemainder - MyCalc * 256 ^ x
    15.     Next
    16.     MyString = "#" & MyString
    17.     Debug.Print MyString
    18. End Sub
    You could also do it by flipping the string if u wanted to eg
    VB Code:
    1. MyNum = vbYellow
    2.     MyString = Right$("000000" & Hex(MyNum), 6)
    3.     MyString = "#" & Right$(MyString, 2) & Mid$(MyString, 3, 2) & Left$(MyString, 2)
    4.     Debug.Print MyString
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    If everything Stuart says is true (as I am sure it is), then convert my previous answer to:

    HexColorString = "#" & Left(Format(CStr(Hex(CommonDialog1.Color) & "000000"), "000000"), 6)


    And see what happens.

    I am half-drunk right now, so this may not work at all...



    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width