Convert HTML Color to RGB


In this day and age you will find yourself at times needing to set a color in two different ways. If you are doing any sort of web development you will find that often you are referring to colors using a code that looks something like this '#0066FF'. On the other hand if you are doing development in Visual Basic it expects you to specify any colors by their Red, Green, and Blue values (hence RGB). Luckily you can write a simple code snippet to convert HTML colors into their RGB equivalents.

To do this creat a new Visual Basic application. Add a command button to it. Double click this button. Highlight everything in the code editor and delete it. Replace it with this code.

  1. Function HTMLtoRGB(HtmlCode As String) As String
  2.         If Left(HtmlCode, 1) = "#" Then
  3.                 HtmlCode = Right(HtmlCode, 6)
  4.         End If
  6.         RED = Left(HtmlCode, 2)
  7.         GREEN = Mid(HtmlCode, 3, 2)
  8.         BLUE = Right(HtmlCode, 2)
  9.         RgbHex = "&H00" + BLUE + GREEN + RED
  10.         HTMLtoRGB = "&" & Val(RgbHex)
  11. End Function
  13. Private Sub Command1_Click()
  14.     MsgBox HTMLtoRGB("0000FF")
  15. End Sub

Once you add the source code run the program and hit the command button. You will see your HTML color converted to a Red Green Blue equivalent.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

all about vb 6

please help me all about coding on vb 6