Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

RGB()

This function takes values for the red, green and blue components of a color and returns the unique number for that color. Because Visual FoxPro offers more than 16 million colors, RGB() is a lot more convenient than trying to remember individual color numbers.

Usage

nColorNumber = RGB( nRedValue, nGreenValue, nBlueValue )

Each of the three color values ranges from 0 to 255.

If you went to school (and we figure most of you did), you may wonder why we specify red, green and blue values. After all, the primary colors are red, yellow and blue.

The answer is that there are two different types of color around. The colors we all learned about in school are paint colors. With paint, mixing red, yellow and blue in appropriate proportions can make everything except white. Paint colors are subtractive—each color you add removes more of the visible color until you eventually end up with black, the absence of color.

Computers (and TVs and so forth) use light color, though. This is additive color in which each color you add increases the total color of the result until you end up with white (total color). In light colors, the primaries are red, green and blue.

The result of RGB() is a number between 0 and 16,777,215 (which is 256^3 less one). It sounds mysterious, but in fact, the formula used to compute the result is simply:

nBlueValue * (256^2) + nGreenValue * 256 + nRedValue

We can’t figure why they did it backwards, but we suspect it wasn’t the Visual FoxPro developers who made this choice.

The header file that ships with FoxPro, FoxPro.H, includes values for 16 colors—use those when you can. (COLOR_RED is a lot more readable than RGB(255,0,0) or 255.) When you need a color other than those 16, use RGB() unless you want the user to choose, in which case use GETCOLOR().

The SET COLOR OF SCHEME command uses a special version of RGB() that takes six numbers and converts them into a color pair—that’s a foreground and a background color.

Example

ThisForm.BackColor = RGB(128,0,64)

See Also

#Include, BackColor, FoxTools, ForeColor, GetColor(), RGBScheme(), Set Color Of Scheme