Convert color values to RGB
2005-03-01 Other 0 464
With the procedure below you can convert a color value to its separate RGB counterparts:
Sub Color2RGB(lngColor As Long, intR As Integer, intG As Integer, intB As Integer)
' converts a color to its RGB counterparts
intR = lngColor And 255
intG = lngColor \ 256 And 255
intB = lngColor \ 256 ^ 2 And 255
End Sub
' example:
Color2RGB ActiveCell.Interior.Color, intRed, intGreen, intBlue
The function below can be used to convert a color value to a 6 character HTML color code:
Function Color2HTMLRGB(lngColor As Long, Optional blnIncludeGrid As Boolean = True) As String
Dim intR As Integer, intG As Integer, intB As Integer
Dim r As String, g As String, b As String, strResult As String
intR = lngColor And 255
intG = lngColor \ 256 And 255
intB = lngColor \ 256 ^ 2 And 255
r = Hex(intR)
g = Hex(intG)
b = Hex(intB)
If Len(r) < 2 Then r = "0" & r
If Len(g) < 2 Then g = "0" & g
If Len(b) < 2 Then b = "0" & b
strResult = UCase(r & g & b)
If blnIncludeGrid Then
strResult = "#" & strResult
End If
Color2HTMLRGB = strResult
End Function
' examples:
strColorCode = Color2HTMLRGB(Range("A1").Interior.Color)
strColorCode = Color2HTMLRGB(Range("A1").Font.Color)