Convert Common Dialog Control Color to WEB Hex

By | 2002-06-01

This code takes the common dialog color, extracts the R, G, B values, and
converts each value to the correct HEX equivilant supported by HTML.

Original Author: Charlie Wilson

Inputs

Function requires a common dialog control color being selected and passed to the
function

Assumptions

You must add the common dialog control to your application. After the ShowOpen event
occurs, pass the color selected by the user to the function:
Dim sColHex as String
sColHex = HexRGB(cdlCont.color)

Returns

Function returns properly formatted HEX color value for HTML

API Declarations

None

Code

Private Function HexRGB(lCdlColor As Long)
  Dim lCol As Long
  Dim iRed, iGreen, iBlue As Integer
  Dim vHexR, vHexG, vHexB As Variant
  
  'Break out the R, G, B values from the common dialog color
  lCol = lCdlColor
  iRed = lCol Mod &H100
    lCol = lCol &H100
  iGreen = lCol Mod &H100
    lCol = lCol &H100
  iBlue = lCol Mod &H100
  
  'Determine Red Hex
  vHexR = Hex(iRed)
      If Len(vHexR) < 2 Then
         vHexR = "0" & vHexR
      End If
      
  'Determine Green Hex
  vHexG = Hex(iGreen)
      If Len(vHexG) < 2 Then
         vHexG = "0" & iGreen
      End If
      
  'Determine Blue Hex
  vHexB = Hex(iBlue)
      If Len(vHexB) < 2 Then
         vHexB = "0" & vHexB
      End If
  'Add it up, return the function value
  HexRGB = "#" & vHexR & vHexG & vHexB
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.