Playing with the Palette


Before You Read

Before you start diving into this GREAT (yeah, right) literature, please take the note of the following.
  • This is tutorial was intended with use of DJGPP. All the concepts and code MAY work with other compilers, but don't email me saying how none of my code works if your using a compilers besides DJGPP.
  • This tutorial is assuming that you know what/and how to enter mode13h, and that you know the basics of C/C++. all that? Well, let's get to reading and see if you can learn a thing or two.

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaBack to the Top ->


Ok, I know everyone hates this part, but I'm not taking any chances, so here goes.

I do not take any responsiblity if the code, ideas, etc. causes your computer to :
  • Crash
  • Stop Running/Working
  • Explode
  • Kill Your Loved Ones
  • Anything Else I Didn't Cover
So I don't want any bitch or hate mail sent to me complaining about one of those things listed above, or anything else like that happens to you or your computer. Got it?

With that read, and understood, let's get on with the tutorial and have some fun :)


The palette, as I've heard other programmers say, has been on of the biggest pains in the ass for game programmers to understand and master. Hopefully I can shed some light on the subject.

The palette is basically an array of [256][3] representing the 256 colors that mode13h supports and the 3 intensities, red, green, and blue.

When using, or manipulating, the palette, you can achieve some cool effects for your games and/or demos as you'll later see. Some of the more popular, that I'll mention later, are the fade and palette rotation routines.

Basic Palette Routines

Although a good bit of this tutorial could be skipped and you could still become very good at using the palette, this part should NOT be skipped.

Also, it would be good, for both me and you, if you read through this section two or more times. It'd help you because you'd understand it better, and it'd help me because,

The parts that this section includes are :

This is a simple, yet very important thing to learn if you want to become a master of the palette.

Basically, all you have to do is write the color you want to change into port 0x03C8, and then write it's intensities of red, green, and blue into port 0x03C9.

Here's a function to do that for you...

void set_pal(unsigned char color, unsigned char red
aaaaaaaaaaaaaunsigned char green, unsigned char blue)
aaaoutp(0x03C8, color);

aaaoutp(0x03C9, red);
aaaoutp(0x03C9, green);
aaaoutp(0x03C9, blue);

Now let me explain how you would use it.

Let's say you want to take the color red, which is the number 12, and change it from red to purple. All you would do is input the number 12 as the first parameter, then enter in the intensities that you want it to be changed to, which would be 63, 0, 63 if you wanted it to become purple.

For our example, the function would look like this as real code.

void set_pal(12, 63, 0, 63);

Not to hard, huh?


Here's yet another one of those must-know routines in order to fully understand the palette.

All you do when you read in a color from the palette is you enter in the number of the color you want to read from into port 0x03C7, and then enter in the values of red, blue, and green into port 0x03C9.

Here comes a sample function :

void get_pal(unsigned char color, unsigned char &red,
aaaaaaaaaaaaaunsigned char &green, unsigned char &blue)
aaaoutp(0x03C7, color);

aaared = inp(0x03C9);
aaagreen = inp(0x03C9);
aaablue = inp(0x03C9);

Now let me explain how to use this wonderous function.

To use it type in the number of the color you want to read, say 15 which is white. Then enter in the values of red, green,and blue. It'll then read in the color. One example of this code is...

void get_pal(15, 12, 23, 24);

No prob, right? Right?


Ok, listen up and read carefully because this is a very important function.

If you have ever played around with the palette before, then you might have noticed that when you do some palette procedures the screen seems to gather some fuzz or 'snow' as it's called. Now the way that this little monster is stopped is as follows...

There is an electronic beam moving up and down your monitor's screen updating it's display. As it gets to the bottom of your screen, it takes it a little while to get back up to the top. This is what we call the vertical retrace.

During this, we can update the screen, doing whatever it is that we may have todo, without having it appear on screen intill it is completely through been done, thus getting rid of the on screen fuzz.

Just a little thing to note is that, if you ever get into Windoze's programming, the upcoming vertical retrace function will not have to be used if you mess with the palette, since Windoze is fast enough to update the screen without the extra fuzz. Poor old DOS.

Here's the important vertical retrace function...

void w_retrace()


All you have to do to use it is call it when you need it.


A simple description of what this does is that it takes the current palette, whatever that may be, and stores it into a nice, neat little array.

One thing to keep in mind when doing this is to make the array in which you are going to store the palette in as big as palette, less it won't work. It will compile, I think, but it won't work.

Also, you'll need to define a global array that will store your array such as the one below...

unsigned char pal[256][3];

Ok then, on with the code.

void grab_pal()
aaa int loop1;

aaa for(loop1 = 0; loop1 < 256; loop1++)
aaaaaaget_pal(loop1, pal[loop1][0], pal[loop2][1], pal[loop3][2]);

That's all there is to it. This function, when called, will store the palette into the array specified.

If you change the name of the global array, make sure you change it in the function too, or errors will appear out of thin air.

See I told you that the get_pal() function would come in handy sooner or later.


This isn't really a necessary function, but you'll most likely need it when, and if, you want to try out/use the fade-up function(s).

Basically, what this does is it puts a black mask across your screen, so that if you had a pixel on the screen and then you called the blackout function, it wouldn't erase your pixel, it would just cover it up with a black "cover".

The function for this little do-dad is below...

void blackout()
aaaint loop1;


aaafor(loop1 = 0; loop1 < 256; loop1++);
aaaaaaset_pal(loop1, 0, 0, 0);

This is another simple function to use. All you have todo, like the w_retrace() function above, is call it when and where needed.

Playing with the Palette by Keith Dowd

TAOTA of PyroMantics Studios
ICQ# - 20099215
"...and the rivers became as blood."

Discuss this article in the forums

Date this article was posted to 7/16/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:

© 1999-2011 All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!