Palette tricks

Palettes can provide some amusing graphics techniques for a very small performance cost. It’s much faster to change colors in the hardware palette than to change pixels on the screen, and sometimes the effect can be just as dra­-
ma­tic. For example, you can gradually fade a bitmap on to or off the screen, or you can rotate palette colors to create an illusion of movement.

Visual Basic won’t help you with these tricks, and in many cases it will stand in your way as it manages palettes behind the scenes. To get palette management to work seamlessly, you need to push Visual Basic out of the way and take over. Here are some hints about how to do it.

There’s a lot more to handling palettes at the API level—too much to describe in detail, so here’s the executive summary. The first step in manipulating a palette (in the form of a palette handle) is to get its size with the multipurpose GetObject function (or the GetObjectPaletteEntries alias from the Windows API type library). Next you read the palette colors into an array with the GetPaletteEntries API function. Add the flags necessary for palette manipulation and call Set­PaletteEntries to put your changes back into the palette. When you’re ready to roll, call SelectPalette to select your palette into the device context of the window that will display the palette, and call RealizePalette to map your logical palette onto the system palette. Most palette tricks involve some sort of periodic reordering of the palette colors. For each change, you rearrange the
palette colors in the array and call AnimatePalette to update the new colors in the palette. If all goes well, the new colors will appear on the screen in place of the old colors.

The TPALETTE project on the CD illustrates the details of palette management. There’s no point in showing a screen dump because the paper pages of this book don’t support palettes.