Results 1 to 5 of 5

Thread: HTML Hex Colours > VB Colours

  1. #1

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    HTML Hex Colours > VB Colours

    I found this code on the forum a little time ago. It converts VB colours to HTML Colours (some kind of Hexadecimal value). Unfortunately I don't really understand its functionality, and I need a code that does the opposite: convert HTML Colours to VB Colours. Does anyone have such a code?

    Thanks.

    VB Code:
    1. Public Function ForeColorHTML(Number As Long) As String
    2. Dim MyString As String, MyNum As Long, MyCalc As Long, MyRemainder As Long, X As Integer
    3. MyRemainder = Number
    4.     MyString = ""
    5.     For X = 2 To 0 Step -1
    6.         MyCalc = MyRemainder \ 256 ^ X
    7.         MyString = Format$(Hex(MyCalc), "00") & MyString
    8.         MyRemainder = MyRemainder - MyCalc * 256 ^ X
    9.     Next
    10.     txtForeColor.Text = MyString
    11. End Function

  2. #2
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278
    To convert HTML colors to VB colors is pretty straight forward VB has a function called RGB........ the hex value from HTML represents this.... for example #ffffff is white

    this represents Red = ff = (15*16) + 15 = 255
    Blue = ff = (15*16) + 15 = 255
    Green = ff = (15*16) + 15 = 255

    So RGB(255,255,255) = color code white in VB which can be used to set the color in VB.
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

  3. #3
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278
    An easyway to set the color is to use the following:

    Code:
    public function returnColorCode(byval HexColor as String) as long
         returnColorCode = val("&h" & HexColor)
    end function
    This seems to be what you are after
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

  4. #4

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Yeah, indeed. Thanks very much. I needed that code bad.

    By the way, HTML reads Hexadecimal colours from right to left instead of left to right. So I slightly changed the code to this:

    VB Code:
    1. Val("&h" & Mid(strColor, 5, 2) & Mid(strColor, 3, 2) & Mid(strColor, 1, 2))
    Last edited by TheVader; Apr 9th, 2003 at 08:10 AM.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    That code will not give you the correct result since on a webpage you put the hex rgb code in this manner: RRGGBB while Windows store colors in the following manner: BBGGRR so if you would use that code to convert FF0000 that would be red on a webpage it would be shown as blue in your app.
    VB Code:
    1. Public Function RGBconvert(ByVal sHexColor) As Long
    2.     'First make sure the hex color is 6 digit long
    3.     sHexColor = Right$("000000" & sHexColor, 6)
    4.     'Now switch the red and the blue values
    5.     sHexColor = Right$(sHexColor, 2) & Mid$(sHexColor, 3, 2) & Left$(sHexColor, 2)
    6.     'Convert to long
    7.     RGBconvert = CLng("&H" & sHexColor)
    8. End Function
    Best regards

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