Code Snippets
Code Samples

The Blog
Our Projects

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

Tutorial - Palette
Category:DOS Graphics
Uploaded at:29-May-03
The palette is used in 8-bit color depth (as used in mode13 for instance).

The palette is a buffer that contains definitions for 256 colors
(color = Red, Green, Blue).
The video buffer contains a Color Index for each pixel on the screen.
The color that would be displayed on the monitor is color number
'Color Index' from the palette.

That means that if a pixel's color index is 74, and color no. 74 in the
palette is RGB(63, 0, 0) (=Red) then that pixel will be red.

Each value in the palette (Red, Green or blue) should
be in the range of 0-63.
That means you have 64^3 = 262144 possible colors to display
(but remember - only 256 colors in the same time).

Playing with the palette you can make some cool effects
such as 'Fade In'/'Fade Out'.

You can set or retrieve each color from the palette pretty easily.

To set a color in the palette do:
void setpal(unsigned char col, 
            unsigned char r,
            unsigned char g,
            unsigned char b)
 outportb(0x3C8, col);
 // We send to port 0x3C8 the index of the color in the 
 // palette we want to change

 outportb(0x3C9, r);   // Send Red Value to port 0x3C9
 outportb(0x3C9, g);   // Send Green Value to port 0x3C9
 outportb(0x3C9, b);   // Send Blue Value to port 0x3C9

// The retrieve a color from the palette do:

void getpal(unsigned char col, 
            unsigned char *red, 
            unsigned char *green, 
            unsigned char *blue)
 outportb(0x3C7, col);
  // Send to port 0x3C7 the index of the color in the
  // palette we want to retrieve

 *red = inportb(0x3C9);   // Retrieve Red Value from port 0x3C9
 *green = inportb(0x3C9); // Retrieve Green Value from port 0x3C9
 *blue = inportb(0x3C9);  // Retrieve Blue  Value from port 0x3C9

Note: Palette may be found in other modes that use 8 bits per pixel

Source Code:
DJGPP - palette.c
Turbo C - palette.cpp

User Contributed Comments(None)
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
::Top 5 Tutorials::
Embedded Python[119194]
2D Rotated Rectangles Collision Detection[90098]
Keyboard Hook[78635]
HTTP Proxy[41966]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11602]
PS2 Mouse Driver[7002]
Wave Format Player[5828]
Reading FAT12[5652]

All rights reserved to RageStorm © 2009