SAMPLE: Palette Animation in Windows

Last reviewed: February 15, 1996
Article ID: Q74799
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

"Animation" of the Windows palette is the replacement of one set of colors in the palette by another set of colors. This article will discuss animation and how it is used in a Windows-based application.

MORE INFORMATION

For an example of animation, consider a chain of rectangles on the screen:

  +------+ +------+ +------+ +------+ +------+ +------+
  |      | |      | |      | |      | |      | |      |
  |idx 20| |idx 21| |idx 22| |idx 20| |idx 21| |idx 22| ...
  |      | |      | |      | |      | |      | |      |
  +------+ +------+ +------+ +------+ +------+ +------+

If index 20 is mapped to the color green in the palette, index 21 is mapped to red, and index 22 is mapped to blue, a "Broadway Lights" effect can be achieved by remapping the three colors to the different indexes. For example, mapping red to 20, blue to 21, and green to 22, the "lights" will appear to move to the left.

Changing the hardware registers (the system palette) is much faster than redrawing the rectangles, since no pixels need to be updated. Only a very small set of hardware registers need to be changed. This allows for very fast graphical effects.

With video systems capable of supporting 256 colors, these hardware registers are changed through palette animation, and the GDI call, AnimatePalette() was specifically designed for this purpose.

This article is meant to be a supplement to the information provided in the Windows Software Development Kit (SDK) manuals, not a replacement. This article will discuss the actual steps necessary to create and use a palette in an application from a programmer's standpoint.

Q: What is the difference between a logical palette and the

   lpPaletteColors as referenced in the AnimatePalette() function
   description?

A: The logical palette is the one made by CreatePalette(), while the
   lpPaletteColors is a pointer to a separate memory structure that
   holds the animation colors. For this article, the term "animation
   palette" will be used to refer to the memory structure pointed to
   by lpPaletteColors.


Q: What special considerations are necessary for animation?

A: There are two necessary considerations.

   1. The palette entry in the logical palette must have the
      PC_RESERVED flag. A typical line of code that will be placed
      into the application prior to the CreatePalette() call to set
      this flag will be:

         pLogPal->palPalEntry[PaletteIndex].peFlags = PC_RESERVED;

   2. For ease of programming and animation, sequential indexes for
      the animated colors in the logical palette should be used. This
      will allow the smallest possible array to be used to contain the
      animation palette, and will produce a one-to-one correspondence
      between the logical palette and the animation palette.


Q: How is palette animation actually done?

A: There are two steps to animating the Palette:

   1. Change the color values in the animation palette to create the
      desired effect. The following example shifts the colors in the
      animation palette down one index:

        PALETTEENTRY   palentries[210];  /* The animation palette */

        ...

        palentry.peRed   = palentries[209].peRed;
        palentry.peGreen = palentries[209].peGreen;
        palentry.peBlue  = palentries[209].peBlue;

        for (i = 209; i > 0; i--)
          {
          palentries[i].peRed   = palentries[i-1].peRed;
          palentries[i].peGreen = palentries[i-1].peGreen;
          palentries[i].peBlue  = palentries[i-1].peBlue;
          }

        palentries[0].peRed   = palentry.peRed;
        palentries[0].peGreen = palentry.peGreen;
        palentries[0].peBlue  = palentry.peBlue;


   2. Call AnimatePalette(). The following example will animate the
      210 entries starting at index number 20:

        hDC = GetDC(hWnd);

        hOldPal = SelectPalette(hDC, hPal, FALSE);

        // Call Animate Palette, and realize the colors
        AnimatePalette(hPal, 20, 210, (LPPALETTEENTRY)palentries);

        RealizePalette(hDC);

        // Housekeep
        SelectPalette(hDC, hOldPal, TRUE);
        ReleaseDC(hWnd, hDC);


A sample application to draw a color wheel and "spin" it using animation can be found in the Microsoft Software Library.

Download ANIMATE.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download ANIMATE.EXE (size: 21524 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get ANIMATE.EXE (size: 21524 bytes) 
    


Additional reference words: 3.00 3.10 softlib ANIMATE.EXE
KBCategory: kbprg kbfile
KBSubcategory: GdiPal


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.