Tuesday, October 16, 2007

Hex to Dec (VbScript)

Convert hexidecimal color codes (ie #C0C0C0) to decimal RGB (ie 255,255,255) in vbscript

Public Function GiveDec(Hex)
  if Hex = "A" then
    Value = 10
  elseif Hex = "B" then
    Value = 11
  elseif Hex = "C" then
    Value = 12
  elseif Hex = "D" then
    Value = 13
  elseif Hex = "E" then
    Value = 14
  elseif Hex = "F" then
    Value = 15
  else
    Value = Hex
  end if
  GiveDec = Value
End Function

Public Function HexToDec(Hex)
  Dim a,b,c,d,e,f,x,y,z,fHex
  fHex = Replace(Hex,"#","")

  a = Left(fHex,1)
  a = GiveDec(a)
  b = Mid(fHex,2,1)
  b = GiveDec(b)
  c = Mid(fHex,3,1)
  c = GiveDec(c)
  d = Mid(fHex,4,1)
  d = GiveDec(d)
  e = Mid(fHex,5,1)
  e = GiveDec(e)
  f = Right(fHex,1)
  f = GiveDec(f)

  x = (a * 16) + b
  y = (c * 16) + d
  z = (e * 16) + f

  HexToDec = """" & x & "," & y & "," & z & """"
End Function

No comments: