Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Advanced Color Imaging on the Mac OS /
Chapter 1 - Palette Manager / Using the Palette Manager


Animating a Window With a Palette

The Palette Manager provides functions that allow you to create color animation effects. Listing 1-5 shows a simple means of creating animation in your application.

Note
Color animation requires color tables. Therefore, you can create color animation effects on indexed screens only. Screens with 16-bit and 32-bit pixel depth do not have color tables and therefore cannot support Palette Manager animation.
Listing 1-5 Animating with a palette

#define  clutID   150
#define  numcolor 256

void DoAnimate()
{
   CTabHandle     myColorTable, StoreCTab;
   PaletteHandle  myPalette;
   WindowPtr      myWindow;
   RGBColor       changeColor;

   myColorTable = GetCTable (clutID);

   /* create a new window */
   myWindow = NewCWindow(nil, &BaseRect, 
                        "\pUsing Palette Manager", 
                        TRUE, documentProc, 
                        (WindowPtr) -1, TRUE, nil);

   /* create a 256-color palette */
   myPalette = NewPalette(numcolor, myColorTable, pmAnimate, 0);

   /* assign the palette to the window */
   SetPalette ((WindowPtr) myWindow, myPalette, TRUE);


   GetEntryColor (myPalette, 1, &changecolor);
   AnimatePalette (myWindow, StoreCTab, 2, 1, numcolor - 2);
   AnimateEntry (myWindow, numcolor - 1, &changecolor);
   PaletteToCTab (myPalette, StoreCTab);
}
In Listing 1-5, the DoAnimate function does some setup before performing the animation. It first declares variables for two color lookup tables, for the window and palette IDs, and for an RGB color. It retrieves a color table with the GetCTable function (described in the chapter "Color QuickDraw" of Inside Macintosh: Imaging With QuickDraw) and creates a new color window with the NewCWindow function (described in the chapter "Window Manager" of Inside Macintosh: Macintosh Toolbox Essentials). It then creates a palette from the color table using NewPalette and assigns this palette to the newly created window using SetPalette. The palette contains 256 colors and the colors are all animated colors.

The first step in the animation is to use the GetEntryColor function to save the first color in the palette. Then AnimatePalette cycles through each color in the palette (except for black and white, which do not animate). The AnimateEntry function moves the saved color to the last entry in the palette. Finally, PaletteToCTab saves the new version of the palette to the color table for use during the next animation.

One thing you could do with this animation code is to put it in a loop. Because the first entry has been moved to the end, the animation, in effect, begins at the second entry during the second iteration, and so on for each time through the loop. The result is an animation that not only cycles through the palette but also displays a slight variation at each iteration by beginning with a different color.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 NOV 1996