Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RGB Colors

Status
Not open for further replies.

sacsac

Programmer
Joined
Dec 10, 2000
Messages
182
Location
GB
Can anyone tell me how to 'reverse' the RGB function. I want to pick out the individual Red, Green, & Blue colors from a previously saved color (a Long data type saved using the RGB function). I'm sure that this must be easy, but I've been struggling for too long! Any help appreciated.
 
Here's two ways:
[tt]
Option Explicit

Private Type vbRGB
Red As Byte
Green As Byte
Blue As Byte
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()
Dim CurrColor As Long
Dim RedShade As Byte
Dim GreenShade As Byte
Dim BlueShade As Byte

CurrColor = RGB(140, 200, 112)
RedShade = (CurrColor And 255)
GreenShade = (CurrColor And 65280) / 256
BlueShade = (CurrColor And 16711680) / 65536

MsgBox RedShade & ", " & GreenShade & ", " & BlueShade
End Sub

Private Sub Command2_Click()
Dim myRGB As vbRGB
Dim CurrColor As Long

CurrColor = RGB(140, 200, 112)
CopyMemory myRGB, ByVal VarPtr(CurrColor), 3

MsgBox myRGB.Red & ", " & myRGB.Green & ", " & myRGB.Blue
End Sub

 
Option Explicit

Private Sub Command1_Click()
MsgBox Long2RGB(1133)
End Sub

Public Function Long2RGB(LongColor As Long) As String
On Error Resume Next
' convert to hex using vb's hex function
' , then use the hex2rgb function
Long2RGB = Hex2RGB(Hex(LongColor))
End Function

Public Function Hex2RGB(strHexColor As String) As String
Dim HexColor As String
Dim i As Integer
Dim r As Byte
Dim g As Byte
Dim b As Byte

On Error Resume Next
' make sure the string is 6 characters long
' (it may have been given in &H###### format, we want ######)
strHexColor = Right((strHexColor), 6)
' however, it may also have been given as or #***** format, so add 0's in front
For i = 1 To (6 - Len(strHexColor))
HexColor = HexColor & "0"
Next

HexColor = HexColor & strHexColor
' convert each set of 2 characters into bytes, using vb's cbyte function
r = CByte("&H" & Left$(HexColor, 2))
g = CByte("&H" & Mid$(HexColor, 3, 2))
b = CByte("&H" & Right$(HexColor, 2))
Hex2RGB = r & "," & g & "," & b
End Function
 
Many thanks strongm & LPlates
 
This is the one I use:
Code:
Public Function TranslateRGB(ByVal RGB As Long) As OLE_COLOR
    
    '--- Parameter
    '     [In]
    '     RGB: an RGB colour value where the low order bytes
    '     are in red, green, blue order
    
    '--- Return value
    '     returns the equivalent OLE_COLOR value where the low
    '     order bytes are in blue, green, red order
    
    Dim pstrRed As String
    Dim pstrGreen As String
    Dim pstrBlue As String
    
    'Extract the three colour bytes
    pstrRed = Hex$((RGB And &HFF0000) / &H10000)
    pstrGreen = Hex$((RGB And &HFF00&) / &H100)
    pstrBlue = Hex$(RGB And &HFF&)
    
    TranslateRGB = "&H2" & pstrBlue & pstrGreen & pstrRed
    
End Function

Paul Bent
Northwind IT Systems
 
Many thanks Paul Bent.
 
The thread222-672031 might also be worth reading.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top