systemautorizin,

Nice little program but the OP (tripes) asked how to break down a selected color into the rgb triplet values. What tripes did not tell us is how they are getting those value and off the top of my head I can think of a couple of ways. GetPixel API, Point, and the common dialog show color method. Each of these methods returns a long value and the following shows how to break down that long into each of the rgb triplet values.

Dim MyColor As Long, R As Integer, G As Integer, B As Integer, Temp As Integer
MyColor = RGB(124, 45, 36)

Temp = MyColor Mod 65536
MyColor = MyColor - Temp
B = MyColor / 65536
MyColor = Temp
Temp = MyColor Mod 256
MyColor = MyColor - Temp
G = MyColor / 256
R = Temp
Debug.Print R, G, B

Good Luck

Option Explicit
Private Const RGB_R = 1
Private Const RGB_G = 2
Private Const RGB_B = 3

Private Sub cmdFindColor_Click()
     Dim lngColor as Long, intRGB as Integer, lngRGBValue as Long
     ' Code to find RGB value
     ' Blah, Blah, Blah
     ' intRGB would be RGB_R, RGB_B, etc.
     ' lngRGBValue would be the RGB Color value
     ' lngColor would be the Red, Green, or Blue Value you wish to find
     lngColor = Return_RGB_VALUE(lngRGBValue, intRGB)
End Sub

Private Function Return_RGB_VALUE(RGB_Long As Long, RGB_Color As Integer) As Long
If (RGB_Long > 0) And (RGB_Long < 16777216) Then
     ' Return the Red, Green, or Blue value based on the RGB Value and
     ' the Color requested.
     Return_RGB_VALUE = (RGB_Long / 256) ^ ((RGB_Color - 1) And 255)
     ' Convert the RGB value into an 8 bit value and take that value
     ' the exponential value of the bitwise comparison of ((Color - 1) And 255)
     ' The And operator in this case returns the bitwise comparison of 2 numbers.
   Else
     ' Not a valid RGB value
     Return_RGB_VALUE = -1
   End If

End Function

Visual Basic really doesn't have any operators that deal with bit shifting directly like C (<< or >>).

But in the C language the COLORREF data type contains the RGB value with a high order byte of zero and it's in a 32 bit value with the following format: 0x00bbggrr. And C has three special color macros: GetRValue, GetGValue, and GetBValue which do not exist in VB6: C uses these three macros to extract the red, green, and the blue values. C uses bit shift operators in these macros.

But the COLORREF data type is a hexadecimal number which is formed from the RGB value and the high order byte &H00.
There are 4 bytes in that number: the low order red byte (rr or red), the second byte (gg or green), the third byte (bb or green), and the high order byte must always be zero.

Visual Basic only gives us the first 3 bytes. But that's all we need anyway.

The highest RGB Value would be from the values of 255, 255, 255

lngValue = RGB(255, 255, 255)

The lngValue would be 16777215 Hex$(16777215) would yield FFFFFF

Red would be &HFF, Blue &HFF, Green &HFF

However, if you had a small value like 25 for the RGB VALUE, that would pose problems for finding the hex values: hex$(25) would yield the string "19." You have no values for the green or the blue.

So, one way to get around this problem is to just add (maximum value + 1) to the RGB value.

The max hex value for the RGB value is &HFFFFFF.

That value plus one is &H1000000

Thus, if you just add the hex value of the RGB (hex(RGB(Red, Green, Blue)), to the max value you would always be able to have a hex string that you could work with to extract the red green and Blue values.

&H1000000  +
&H25
-----------------
&H1000025

Just convert that Hex number to a HEX$ and use some VB string functions to extract the 'rr', the 'gg', and the 'bb' from that hexadecimal string.

dim strColorRef as String
Dim strColorRed as String, strColorBlue as String, strColorGreen as String
Dim lngRGB as long
Dim lngColorReg as long
Dim lngRed as long, lngGreen as long, lngBlue as long

lngRGB = 67
' Since &H1000000 = 16777216
' just add 16777216 to lngRGB
lngColorRef = 16777216 + lngRGB
' convert that value to a hexadecimal string
strColorRef = hex$(lngColorRef)
' strColorRef here would be "1000043"

' Red
strColorRed = Right(strColorRef,2)
lngRed = clng("&H" & strColorRed)

' Green
strColorGreen = left(strColorRef, 5)
strColorGreen = Right(strColorGreen, 2)
lngGreen = clng("&H" & strColorGreen)

' Blue
strColorBlue = left(strColorRef, 3)
strColorBlue = Right(strColorBlue, 2)
lngBlue = clng("&H" & strColorBlue)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.