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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Red, Green and Blue Values from a colour ?

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
I know I can use the RGB function to return the colour code. I.e RGB(red, green, blue) as long however if I know the colour code is there a function that will tell me the individual Red, Green and blue values that make that colour.

Cheers in advance.
Neemi
 


Hi,

Code:
vbRed
vbGreen
vbBlue
.....


Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Here's the function I wrote a while back:

Code:
Function RGBRev(ColorValue As Long) As String
   RGBRev = CStr(ColorValue And 255) & "R " & CStr(ColorValue \ 256 And 255) & "G " & CStr(ColorValue \ 65536 And 255) & "B"
End Function

If you wanted separate components, you could do something like

Code:
' Put this declaration at the top of a standard module
Public Type ColorTriplet
   Red As Integer
   Green As Integer
   Blue As Integer
End Type

Function RGBRevTriplet(ColorValue As Long) As ColorTriplet
   With RGBRevTriplet
      .Red = ColorValue And 255
      .Green = ColorValue \ 256 And 255
      .Blue = ColorValue \ 65536 And 255
   End With
End Function
 
Hi ESquared, when I use your function and test it in the imediate window it keeps returning the actual Colour Value

Code:
?redval 4752349

returns
Code:
4752349
 
Try this:
the colour code is made up of the RGB values added together
R * 256^0 + G * 256^1 + B * 256^2
(if I remember correctly) so you should be able to reverse it using Mod and integer division



[η][β][π]
 
redval? Which function?

To use the RGBRevTriplet function, you have to do like this:

RGBRevTriplet(4752349).Red

or,
Dim t as ColorTriplet
t = RGBRevTriplet(4752349)
Debug.Print t.Red, t.Green, t.Blue

Plus, because you are missing parentheses, I wonder how the function is working at all... perhaps it's not even calling anything and just printing out your number.
 
PJStephenson,

Anding with a bitmask is probably more efficient than using modulus when the divisor is a power of two:

Value And 255
vs.
Value Mod 256
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top