Convert RGB to Red/Green/Blue component

B

Barb Reinhardt

I've seen this somewhere, but can't find it now. Can someone direct me to
the formulas to convert to R/G/B components.

Thanks,
Barb Reinhardt
 
C

Chip Pearson

Barb,

You can adapt the following code:

Dim R As Long
Dim G As Long
Dim B As Long
Dim RGBLong As Long
R = &H10
G = &H20
B = &H30
RGBLong = RGB(R, G, B)
Debug.Print Hex(RGBLong)
R = RGBLong And &HFF
G = (RGBLong And &HFF00&) \ &H100&
B = (RGBLong And &HFF0000) \ &H10000
Debug.Print Hex(R), Hex(G), Hex(B)

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
E

egun

Here's one version:

Public Function LongToRGB(theColor As Long, iRed As Integer, iGreen As
Integer, iBlue As Integer) As Boolean
'
Dim lColor As Long
lColor = theColor 'work long
iRed = lColor Mod 256 'get red component
iGreen = (lColor \ 256) Mod 256 'get green component
iBlue = (lColor \ 256 \ 256) Mod 256 'get blue component
'
LongToRGB = True
End Function


HTH,

Eric
 
R

Rick Rothstein

Here is one more method...

Dim Red As Long
Dim Green As Long
Dim Blue As Long
Dim HexVal As String

HexVal = Hex(rgbValue)
Red = CLng("&H" & Right(HexVal, 2))
Green = CLng("&H" & Mid(HexVal, 3, 2))
Blue = CLng("&H" & Left(HexVal, 2))

Debug.Print Red, Green, Blue
 
D

David

Note that when using HEX values that Microsoft handles the HEX values in
reverse order.

All others (HTML, VB RGB Functions, etc.) = R,G,B

Microsoft Hex value is ---> B,G,R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top