Colour Palettes and Vertical Retrace |
As mentioned previously, mode13h can display pixels in one of 256 colours. However, it can actually display up to 262,144 colors, just not all at once. The most you will ever get is 256 colors.
The 256 available colurs available are known as the color "palatte", like the artists tool of the same name. You may only have 256 at a time, but you can change any one of these at any time.
Each color is made up of 3 components - red, green and blue (RGB). By altering the levels of these components, we can create other colours.
Each of the 3 RGB components range in values from 0-63, which means each color has 64 possible shades/intensities. This means we can create 64x64x64 combinatios of colors, or 262,144 colors.
Now, when messing around with the color palette, there are a few things to consider. Firstly, the graphics modes and text modes use the same palette, so it's a good idea to restore the palette before your program finishes. Also, palette changes take effect immediately, so any color on the screen that you change the palette settings of will also change on screen.
Palette rotation can also create some great effects, like Plasma (will publish one on the web if enuf ppl ask), and palette animation.
I have included a palette effect, known as a Fadeout, with this program.
Programming palette registers
Reading palette values
Reading in palette values is easy. Simple enter the number of the color you wish to read the values for into port 3c7h (0x03c7), then read in the values of red, green and blue respectively from port 3c9h (0x03c9). We do this using the outp() and inp() functions.
Setting palette values
This is fairly similar to reading palette values. What you do is enter the number of the color you want to change into port 3c8h (0x03c8), then enter the values of the red, green and blue respectively into port 3c9h (0x03c9). Once again we do this using the outp(). function.
Using the functions we developed above, we can now save and restore the entire palette. Simply use an 2-d array of bytes (unsigned char) like the following:
unsigned char Palette[256][3];
Then loop through each element and read in the values for each! If anyone is having trouble with this email me and let me know.
Okay, now that we know how to play with the palette, we can construct something interesting with it. The sample program will fill the screen with colored lines, and will then fade them out.
A fadeout routine is siimple. Loop through each color in the palette, and if it is not yet zero, decrease it by one until it is. Simple!
Vertical Retrace
If you play around with the palette, you may notice at times there is a bit of "fuzz" on the screen when you change it. This is because inside your monitor there is an electron beam that constantly updates you screen, moving from top to bottom. Once it reaches the bottom, it takes a while to move back to the top again.
The period where it moves from bottom back to the top is known as the "Vertical Retrace". If you write to the screen at this time, it is not seen until the electron beam once again updates the screen.
This is a very powerful, useful tool. It helps reduce fuzz, and nothing you draw is seen until after the retrace. It is also a helpful tool in program timing.
How do we code it? Easily! We simply loop while the value in port 0x03DA ANDed with 8 does equals 0.
Download the source code and example program here.