1

Can you please advise me on how to convert hex to RGB in ASP Classic/VBScript. I have tried to search all over the Internet and tried out many suggested solutions but none point to what I want to achieve.

I have tried the following functions but none of them work: Convert hex color string to RGB color

After converting to RBB, I wish to set the text color based on the background color. So basically my background color code is in hex.

Hanz Cheah
  • 761
  • 5
  • 15
  • 44

3 Answers3

1

First, you convert your hex code into decimal using "&h" representation and parsing the result. After that, it's a matter of doing basic bitwise operators to extract the RGB values from the number.

Dim hexval : hexval = "fdfeff"

Dim rgbval : rgbval = CLng("&h" & hexval)
Dim r : r = (rgbval And &hff0000&) / 65536
Dim g : g = (rgbval And &h00ff00&) / 256
Dim b : b = (rgbval And &h0000ff&)

wscript.echo Join(Array(hexval, rgbval, r, g, b), vbcrlf)

This produces the following output:

fefeff
16645887
253
254
255
BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
0

Here is the function I use :

Function HexToRGB(HexColor)
    'PURPOSE: Convert Hex Code To RGB Code In Cells

    'Remove # (if applicable)
        Dim RGBColor : RGBColor = Replace(HexColor, "#", "")
    
    'Ensure 6 Characters
        RGBColor = Right("000000" & RGBColor, 6)
    
    'Extract RGB Codes
        RGBColor = CLng("&h" & RGBColor)
        
        Dim r : r = (RGBColor And &hff0000&) / 65536
        Dim g : g = (RGBColor And &h00ff00&) / 256
        Dim b : b = (RGBColor And &h0000ff&)
    
    'Format rrrgggbbb
        'RGBColor = r & g & b
    'or rrr,ggg,bbb
        RGBColor = r & "," & g & "," & b

    'Return the value
        HexToRGB = RGBColor
End Function

You can change the returned value at the bottom based on your needs.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Vaka
  • 1
  • 3
-1

This is a little long and I am sure the base 16 function could be more efficient but it works.

' Convert Hex to RGB 
Function ConvertHexToRBG(theHexColor)
  Color = Replace(theHexColor, "#", "") 
  Red = (Mid(Color, 1, 2)) 
  Green = (Mid(Color, 3, 2))
  Blue = (Mid(Color, 5, 2))

  ConvertHexToRBG =  "RGB("&ConvertHexToInt(red)&","&ConvertHexToInt(green)&","&ConvertHexToInt(blue)&",1)"
End Function

Function ConvertHexToInt(theStr)
  If theStr <> "" Then 
  SELECT Case Left(theStr,1)
    Case "F"
        T = 15
    Case "E"
        T = 14
    Case "D"
        T = 13
    Case "C"
        T = 12
    Case "B"
        T = 11
    Case "A"
        T = 10
    Case "9"
        T = 9
    Case "8"
        T = 8
    Case "7"
        T = 7
    Case "6"
        T = 6
    Case "5"
        T = 5
    Case "4"
        T = 4
    Case "3"
        T = 3
    Case "2"
        T = 2
    Case "1" 
        T = 1
    CASE "0"
        T = 0
    Case Else
        T = Left(theStr,1)
  End SELECT
  SELECT Case Right(theStr,1)
    Case "F"
        D = 15
    Case "E"
        D = 14
    Case "D"
        D = 13
    Case "C"
        D = 12
    Case "B"
        D = 11
    Case "A"
        D = 10
    Case "9"
        D = 9
    Case "8"
        D = 8
    Case "7"
        D = 7
    Case "6"
        D = 6
    Case "5"
        D = 5
    Case "4"
        D = 4
    Case "3"
        D = 3
    Case "2"
        D = 2
    Case "1" 
        D = 1
    CASE "0"
        D = 0
    Case Else
        D = Right(theStr,1)
  End SELECT
  ConvertHexToInt = CInt(T*16)+CInt(D)
Else
  ConvertHexToInt =  theStr
End If