I have a program I'm working on right now where the user settings for the app are done on a website. What I didn't realize was that with HTML colours being stored in RRGGBB format, you can't automatically use it in VB, because VB uses the &HBBGGRR format. Here's a little function to bring back the right colour.
Submitted On | |
By | Brandon McPherson |
Level | Beginner |
User Rating | 5.0 (10 globes from 2 users) |
Compatibility | VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script |
Category | Internet/ HTML |
World | Visual Basic |
Archive File |
Function MakeVBColour(hColor) As Long
' 20010509 BWM - Used to flip the
' #RRGGBB HTML colour format to the
' VB-style &HBBGGRR format
' Note: the variable 'RED' refers to 'BLUE'
' in HTML, and 'BLUE' refers to 'RED' in HTML.
' There's no standard.
Dim Red As Long
Dim Green As Long
Dim Blue As Long
Dim sRed As String
Dim sBlue As String
Dim sGreen As String
' Fill a long variable with the colour
hColor = CLng(hColor)
' Separate the colours into their own variables
Red = hColor And 255
Green = (hColor And 65280) \ 256
Blue = (hColor And 16711680) \ 65535
' Get the hex equivalents
sRed = Hex(Red)
sBlue = Hex(Blue)
sGreen = Hex(Green)
' Pad each colour, to make sure it's 2 chars
sRed = String(2 - Len(sRed), "0") & sRed
sBlue = String(2 - Len(sBlue), "0") & sBlue
sGreen = String(2 - Len(sGreen), "0") & sGreen
'reassemble' the colour
MakeVBColour = CLng("&H" & sRed & sGreen & sBlue)
End Function