Click here to Skip to main content
13,344,508 members (58,324 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 26 Dec 2009

Create Simple Palette Animation

, 26 Dec 2009
Rate this:
Please Sign up or sign in to vote.
To show you how to create a easy animation with palette


The article demonstrates how to create an animation with palette. As we all know, palette is outdated, but there are still many places where it is used. If you didn't know more about palette, go to search online to check out more about it. It's so cool that when you add some animation to your project, it could attract other's eyeballs. Of course, this code is very simple. You can change it in any way as per your wish to make it more useful and stronger.


I had nothing to do, so I wrote this code. I love, but I feel that I found it very late.

Using the Code

It's quite easy to use this code:

  1. Add the two class files to your project, "PalAnimClass.h" and "PalAnimClass.cpp".
  2. Call its public method Init() to finish initialization.
  3. Now, call its UpdateDate() frequently. I call this function once every 30ms. You can alter the value and adjust your computer. For this reason, no handle of window was passed into the class, so that we must use another timer to loop it. Let's take a look at the definition of the class:
class PalAnimClass 
BOOL GetDirection();
BOOL SetType(int nType);
BOOL SetDirect(BOOL dir);
BOOL SetColor(int r,int g,int b);
BOOL SetSize(int iWidth,int iHeight);
void UpdateData(HDC hdc);
BOOL Init();
virtual ~PalAnimClass();

BOOL m_bDirection;
int m_nKind;
HBRUSH hBrush;
void DrawBackground(HDC hdc,int type);
void TimerRoutine(HPALETTE hPalette);
HPALETTE CreatePal();
RECT rt;
HPALETTE hPalette,hOldPal;
int m_nHeight;
int m_nWidth;
int m_r,m_g,m_b; 
int i,j;

Only several public methods were found.

  • GetDirection() is used to get the direction of movement. Actually it's a BOOL date type, indacates the movement to left or to right, in or out. We use this method to get the value of movement and to use another method SetDirect() to set a new value.
  • SetType() is used to set the type of animations. The scope of animations' type is 0~6. Be sure that is not out of bound.
  • SetColor(): Three parameters should be passed into, they are RED, GREEN, BLUE colors. The scope of each of them is 0~255, just like the macro RGB.
  • SetSize() is used to set the size of animation. The default size is 300X300.
  • Init() is used for initialization.


The principle of palette animation is really very easy. There are only three steps in total.

  1. Create a palette. The palette is special, we will talk about it later.
  2. Draw shapes with the colors in the palette which we created just now.
  3. We change the value of paletteentrys.

So easy, isn't it? Let's start implementing it.

Create a special palette:

HPALETTE PalAnimClass::CreatePal()
HPALETTE hPalette;

::DeleteObject (hPalette);

plp=(LOGPALETTE *)malloc(sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*64);
plp->palVersion =0x300; //Always 0x300
plp->palNumEntries =64;//We only use 64 so no need change to var

for(int i=0;i<64;i++)
plp->palPalEntry [i].peRed =m_r*sin(PI*i/64);
plp->palPalEntry[i].peGreen =m_g*sin(PI*i/64);
plp->palPalEntry [i].peBlue =m_b*sin(PI*i/64);
plp->palPalEntry [i].peFlags =PC_RESERVED; //This is very important!Must be 
		// PC_RESERVED. That is why I always say that it's a special palette.
}//If peFlasg is not this value, AinamatePalette() will not get the result we want!
return hPalette;

Using the method we create a palette, now we draw shapes in the background. Remember to use the colors in the palette which we just created. This step is easy, look:

hBrush = CreateSolidBrush (PALETTEINDEX (min(64*sin(PI*j/64),64*sin(PI*i/64)))) ;

Use PALETTEINDEX or PALETTERGB to use the color in our new palette. Why are there so many sine functions here, that's just a math problem. You could understand it well. After painting our shapes in the background, now let's go to alter the value of palette. The snippets are as follows:

void PalAnimClass::TimerRoutine(HPALETTE hPalette)
   if(m_bDirection)  //m_bDirecton is a BOOL data structure to 
		//decide whether the palletteentry move to left or to right
   {              //here just a shift oporation,we take the last to first or 
		//take first to last
  temp=plp->palPalEntry [0];
   plp->palPalEntry[i]=plp->palPalEntry [i+1];
  plp->palPalEntry [63]=temp;
  temp=plp->palPalEntry [63];    
   plp->palPalEntry[i]=plp->palPalEntry [i-1];
  plp->palPalEntry [0]=temp;

 AnimatePalette(hPalette,0,64,plp->palPalEntry); //here it is very important to
					// alter the physical device's data.

The AnimatePalette function replaces entries in the specified logical palette.

BOOL AnimatePalette(
  HPALETTE hpal,            // handle to logical color palette
  UINT iStartIndex,         // first entry in logical palette
  UINT cEntries,            // count of entries in logical palette
  CONST PALETTEENTRY *ppe   // pointer to first replacement


An application can determine whether a device supports palette operations by calling the GetDeviceCaps function and specifying the RASTERCAPS constant.

The AnimatePalette function only changes entries with the PC_RESERVED flag set in the corresponding palPalEntry member of the LOGPALETTE structure.

If the given palette is associated with the active window, the colors in the palette are replaced immediately.

Getting to the start now. It's your turn, try to write more interesting code. Sorry about my shortcomings. Any questions, add me to your ICQ chat.

Points of Interest

I found that I became more and more patient and efficient. Thanks to everyone's help! Thanks again! CodeProject helped me improve!^.^


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aric Wang
China China

You may also be interested in...


Comments and Discussions

GeneralCustomization of PaletteAnim Pin
Member 189546128-Dec-09 12:30
memberMember 189546128-Dec-09 12:30 
Hello Aric,

your small programm looks nice, and I guess it could be used as visual attractor in other programs.

To use it as an eyecatcher for an application, it would be nice if you could add some options, so that the changes can be programmed and automated:

a) Add another slider to change the SPEED of the changing palettes.

b) Adjust the values of the colors and of the inverse, according to a given set of values.
Maybe after each second use another RGB value from a given set (2, 3, 5, 7, 11, ..., next_prime)
and always calculate modulo 256 so you always get a value between 0 and 255.

Thanks, Bernhard
GeneralRe: Customization of PaletteAnim Pin
Aric Green28-Dec-09 16:44
memberAric Green28-Dec-09 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 27 Dec 2009
Article Copyright 2009 by Aric Wang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid