Palette-Animation
What you should know before reading this:
You should posses a general knowledge about bitmaps (especially about 256-color
bitmaps) and you should know how Windows handles palettes.
Introduction:
"Normal" animations (.gif-files for example) use many single bitmaps. The clip that you see is just one bitmap after
another. These animations can consume a lot of disk space. Whereas palette-animations use just one bitmap but many
different palettes. Actually a palette-animation does nothing more than changing the colors of a bitmap in real-time.
The bitmap is displayed during the whole process, so it looks like a real animation. This saves a lot of disk space,
however the things palette-animations can do are very limited: You can only use it for animations if you do not need to
modify the displayed bitmap itself and, of course, it works only with images that use palettes (it doesn't work with truecolor
(24-Bit) images). With this technique you can simulate for example flowing water or fade outs. The Windows 95 startup logo
(details) uses a palette-animation (the blue bar at the bottom), but of course you can use a
palette-animation for every bitmap. You just have to write the code that performs the palette-animation.
Colorcycling:
This is a special and probably the most used form of palette-animation. You don't use new colors with every change, but
rotate the existing ones. For example: You have got a palette with three entries: #0 red; #1 green; #2 blue. With every
change the entries are moved to the next lower entry and the first entry becomes the last. After the first change the
palette would look like this: #0 green; #1 blue; #2 red. After the second change: #0 blue; #1 red; #2 green. After the third
change the palette would look like it did at the beginning. Of course you can do it the other way round too (#0 red; #1
green; #2 blue >> #0 blue; #1 red; #2 green >> #0 green; #1 blue; #2 red >> etc.)
View a Colorcycling example
AnimatePalette:
Palette-animation uses the following API function: void AnimatePalette(hpal, iStart, cEntries, lppe)
- HPALETTE hpal
- is the handle of the logical palette that contains the entries to be animated.
- UINT iStart
- specifies the first entry in the palette to be animated.
- UINT cEntries
- specifies the number of entries to be animated.
- const PALETTEENTRY FAR* lppe
- points to the first member of an array of PALETTEENTRY-structures that contains the colors which will replace the
palette entries specified by iStart and cEntries.
AnimatePalette does not change the colors in the logical Palette but changes the system palette directly so the
changes become visible immediatly. If you would change the logical palette instead, you would
have to map the new entries into the system palette after every change (using RealizePalette).
Creating a palette-animation:
The first thing you have to do is to create a 256-color image to animate. You will need an image-editing program that
allows you to change the palette manually. You should place all colors you want to use for the animation one after
another and remember their indexes. Note that the first color in the palette has the index 0. Then you should decide
where to store the other colors/palettes which will replace the old colors. If you use Colorcycling you don't have to
save other colors at all. If you want to write a program that performs a fade-out, you don't need other colors as well
(you can calculate them). But in other cases you will have to save the new colors either in an own file or include it
directly in the source code.
The code that performs the palette-animation:
The animation itself should be organised in some kind of timer-event function. That means that the same function is
called again after a certain span of time has passed. The implementation of the timer itself is different in every
programming language. What you need is a way to make your program execute the same code every 400 milliseconds
for example. The function itself does nothing more than either (Colorcycling) rotating the colors in the
PALETTEENTRY-array or (Fade-outs) calculates a whole new palette or (others) loads new colors into the array and then
calls AnimatePalette to replace the colors in the system palette.
Back to the main page
Copyright 1998 Stefan Hetzl. If you have questions or comments or have discovered an error,
send mail to [email protected]. You may forward this document or publish it on
your webpage as long as you don't change it and leave this notice at the end.