by Brandon McPherson (2 Submissions)
Category: Internet/HTML
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
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.
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