Neil said:
The RGB function returns a colour number. I need a function that will
take a colour number as input and return the equivalent RGB
specification.
I need to use custom colours within a Word document and corresponding
colours in menu icons. I only have colour numbers (e.g. 683250 and
1900708) for the colours, but the software available to me for
creating icons requires I input in RGB format.
Is there a way to do this in VBA or is there software that will
convert for me?
You can do it in VBA.
First, an explanation of how it works: Going from RGB to a single number,
the three numbers from the RGB representation are converted to hexadecimal
and then stuck together, with the R number in the least significant
position, two hex digits each. That 6-digit hex number is then converted
back to decimal. For example, if the RGB colour is 242 red, 108 green, 10
blue then in hex you have
R = 242 decimal = F2 hex
G = 108 decimal = 6C hex
B = 10 decimal = 0A hex
colour number = 0A6CF2 hex = 683250 decimal
To reverse the procedure, take the decimal colour number and convert it to a
6-digit hex number; take each 2-digit piece of the hex number and convert it
to the corresponding decimal number.
Here's the VBA code:
Sub ColourToRGB()
Dim strColour As String
Dim hexColour As String
Dim nColour As Long
Dim nR As Long, nB As Long, nG As Long
strColour = InputBox("Enter decimal colour number:")
If Len(strColour) = 0 Then Exit Sub
nColour = Val(strColour) ' convert string to decimal number
hexColour = Hex(nColour) ' convert decimal number to hex string
While Len(hexColour) < 6 ' pad on left to 6 hex digits
hexColour = "0" & hexColour
Wend
nB = CLng("&H" & Mid(hexColour, 1, 2))
nG = CLng("&H" & Mid(hexColour, 3, 2))
nR = CLng("&H" & Mid(hexColour, 5, 2))
MsgBox strColour & " = R " & nR & ", G " & nG & ", B " & nB
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.