RGB function

A

asc4john

I'm using the RGB function which converts (R,G,B) to long so how do I
get from long to RGB?
 
T

Terry Kreft

Quick way without bitmasks, this returns a 3 element array the first element
is the red value the second is the green and the third is the blue.

Function RevRGB(RGBIn As Long) As Variant
Dim strHex As String
Dim intRed As Integer
Dim intGreen As Integer
Dim intBlue As Integer

Const PAD_ZERO = "000000"

strHex = Right(PAD_ZERO & Hex(RGBIn), 6)

intBlue = Val("&H" & Mid(strHex, 1, 2))
intGreen = Val("&H" & Mid(strHex, 3, 2))
intRed = Val("&H" & Mid(strHex, 5, 2))

RevRGB = Array(intRed, intGreen, intBlue)
End Function

sample call
Sub TestRevRGB()
Dim varRet As Variant
Dim lngColor As Long

lngColor = RGB(255, 254, 253)
varRet = RevRGB(lngColor)
Debug.Print lngColor; "= "; varRet(0), varRet(1), varRet(2)

lngColor = RGB(0, 1, 2)
varRet = RevRGB(lngColor)
Debug.Print lngColor; "= "; varRet(0), varRet(1), varRet(2)
End Sub
 
T

Terry Kreft

.... oh if you want a bitmasking version then you can use

Function RevRGB(RGBIn As Long) As Variant
Dim intRed As Integer
Dim intGreen As Integer
Dim intBlue As Integer

intBlue = (RGBIn And Not &HFFFF&) / &H10000
intGreen = (RGBIn And Not &HFF00FF) / &H100
intRed = (RGBIn And Not &HFFFF00)

RevRGB = Array(intRed, intGreen, intBlue)
End Function

Which can of course go to a oneliner

Function RevRGB(RGBIn As Long) As Variant
RevRGB = Array((RGBIn And Not &HFFFF00), (RGBIn And Not &HFF00FF) / &H100,
(RGBIn And Not &HFFFF&) / &H10000)
End Function

BTW you should only apply any of these methods in the range returned by the
RGB function (i.e. 0 to &HFFFFFF)
 
A

asc4john

I have been playing around with this and have found a different
problem. I'm using Xpdfview I set a highlight with addRegion
page,x0,y0,x1,y1, RGB(255,0,0) .... which gives a red highlight. When
I retrieve the region info later the number I get back does not match
the RGB(255,0,0) value. Do PDFs use a different colour scheme? Also if
I use the retrieved value the colour is different.
RGB(255,0,0) long value is 255 but the retrieved value is 197,687,864.
Which is kinda purple.
 
M

Marshall Barton

asc4john said:
I'm using the RGB function which converts (R,G,B) to long so how do I
get from long to RGB?


If x is the RGB value, an arithmetic method is:

intRed = x Mod 256
intGreen = (x \ 256) Mod 256
intBlue = x \ 256 \ 256
 
T

Terry Kreft

The best person to ask about pdfs and their colour schemes would be Stephen
Lebans.
 
A

asc4john

Thanks but I finally got it all working there was a little error in my,
aaah, thought process.
 

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