Example: Color Cycling

The Rainbow program demonstrates color cycling within a 24-color rainbow (the rainbow uses colors 10 to 33) shown here:

The key to the Rainbow program lies in the RGBvalues array, which is large enough to hold two sets of 24 RGB triples. After setting up the device context, the WM_CREATE handler calls a function FillColorPalette() to fill the first half of this array with the initial color components for colors 10 to 33. The FillColorPalette() function then duplicates these same values in the second half of the array. WM_CREATE then creates a logical palette for colors 10 to 33 using the RGB values in the first half of RGBarray. Following this, WM_CREATE uses fg_ellipsef() to draw a rainbow of consecutive-color bands, with color 10 at the outside and color 33 the innermost color. The last thing WM_CREATE does is set the global variable Start to zero; we'll see why shortly.

The actual color cycling occurs with the help of a Windows timer. For C and C++, we set this up at the end of the WM_CREATE handler with the SetTimer() Windows API function. The SetTimer() parameters specify that the timer "goes off" every 50 milliseconds, at which time Windows will generate a WM_TIMER message (you cannot rely on this being an exact time, but for our purposes it will be more than adequate). Our WindowProc() procedure includes a WM_TIMER handler to process these messages. For C++Builder, Delphi, and Visual Basic, we implement this through a timer control, also with a 50-millisecond interval.

The WM_TIMER handler first uses the Windows API function GetActiveWindow() to insure we perform the color cycling only when the Rainbow program is the active window. If it is the active window, we point Start to the next RGB triple in RGBvalues, pointing back to the start of the array if needed. We then define new values for colors 10 to 33 beginning with the RGB triple pointed to by Start. For all but color 10, this makes fg_setdacs() use some color values from the second half of the RGBvalues array, effectively simulating a "wrap" to the start of RGBvalues that would otherwise require two fg_setdacs() calls.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.