Example: Palette Fades

The Fade program demonstrates both the fade in and fade out palette fades with a 320x200 PCX file. The FadeIn and FadeOut selections on the top-level menu are mutually exclusive. That is, when one is enabled, the other is grayed out. After the program's WM_CREATE handler sets up the device context and default logical palette, it creates a 320x200 virtual buffer and loads the PCX file into it. We then use fg_getdacs() to retrieve the original image colors into the Original array. Note that we're only interested in the 236 non-system colors (colors 10 to 245).

Clicking on FadeOut calls the FadeOutClick() function. This function gradually turns the image to black with a "fade out" palette fade. It starts by copying the contents of the Original array to the Current array. The function then begins a loop that decrements each nonzero color component in Current. Once we've checked all 236 non-system colors, we call fg_setdacs() to update the logical palette with the color values from the Current array. If we're using a 16-color or 256-color display, this updates the colors in the client area, but if we're using a high color or true color display, we must also call fg_vbscale() to display the pixels in their new colors. This produces one step of the palette fade. We continue this process until all color components are zero, at which time the image is invisible and the palette fade is done. Finally, we disable the FadeOut menu item and enable the FadeIn item, and in the C/C++ version, then use DrawMenuBar() so the menu bar reflects these changes. Note that when the fade out completes, the pixels comprising the image are not color 0. We've just changed the logical palette colors so all non-system colors are black.

Clicking on FadeIn calls the FadeInClick() function to restore the original image colors through a "fade in" palette fade. It is very similar to the FadeOutClick() function, but it starts by setting all elements in the Current array to zero. The loop increments each color component in Current until they've reached their original values, which are stored in the Original array.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.