Wiki Home

VFP Function RGB


Namespace: WIN_COM_API
The RGB( nRed, nGreen, nBlue) function returns an integer corresponding to a color. This integer is used in, among other places, the Fore Color and BackColor properties.


The integer generated by RGB(x, y, z) is built as follows

RGB(x, y, z) = x + y(256) + z(256)^2
One can also invert the calculation to obtain the R, G, and B values from an nColor number;

lnColor = Some color number
lnBlue =  INT(lnColor / (256^2))
lnGreen = INT((lnColor - lnBlue*256^2) / 256)
lnRed =   INT(lnColor - (lnBlue*256^2 + lnGreen * 256))
* A perhaps more readable version:
lnColor = 0xfba93d
lnRed  = Mod(lnColor, 256)
lnGreen = Mod(BitRShift(lnColor, 8), 256)
lnBlue = Mod(BitRShift(lnColor, 16), 256)

Foxtools.fll contains a function RGBComp() that will do this faster and in one line of code.
Not that some other languages (notably Delphi ... Is this the Win32 convention?) store RGB color values in the opposite byte order. ie:
RGB(x, y, z) = z + y(256) + x(256)^2
Category VFP Functions
( Topic last updated: 2004.11.12 09:40:19 AM )