setpalette
Changes one palette color.
Defined in header <graphics.h>
Syntax
#include <graphics.h>
void far setpalette(int colormim, int color);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ |
Remarks
setpalette changes the colornum entry in the palette to color. For example, setpalette(0,5) changes the first color in the current palette (the back-ground color) to actual color number 5. If size is the number of entries in the current palette, colornum can range between 0 and (size - 1).
You can partially (or completely) change the colors in the EGA/VGA palette with setpalette. On a CGA, you can only change the first entry in the palette {colornum equals 0, the background color) with a call to setpalette.
The color parameter passed to setpalette can be represented by symbolic constants defined in graphics.h.
| CGA | EGAA/GA | ||
| Name | Value | Name | Value |
| BLACK | 0 | EGA BLACK | 0 |
| BLUE | 1 | EGA BLUE | 1 |
| GREEN | 2 | EGA GREEN | 2 |
| CYAN | 3 | EGA CYAN | 3 |
| RED | 4 | EGA RED | 4 |
| MAGENTA | 5 | EGA MAGENTA | 5 |
| BROWN | 6 | EGA LIGHTGRAY | 7 |
| LIGHTGRAY | 7 | EGA BROWN | 20 |
| DARKGRAY | 8 | EGA DARKGRAY | 56 |
| LIGHTBLUE | 9 | EGA LIGHTBLUE | 57 |
| LIGHTGREEN | 10 | EGA LIGHTGREEN | 58 |
| LIGHTCYAN | 11 | EGA LIGHTCYAN | 59 |
| LIGHTRED | 12 | EGA LIGHTRED | 60 |
| LIGHTMAGENTA | 13 | EGA LIGHTMAGENTA | 61 |
| YELLOW | 14 | EGA YELLOW | 62 |
| WHITE | 15 | EGA WHITE | 63 |
Note that valid colors depend on the current graphics driver and current graphics mode.
Changes made to the palette are seen immediately onscreen. Each time a palette color is changed, all occurrences of that color onscreen change to the new color value.
b^
setpalette cannot be used with the lBM-8514 driver; use setrgbpalette instead.
Return value
If invalid input is passed to setpalette, graphresult returns -11, and the current palette remains unchanged.
See also
Example
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
int gdriver - DETECT, gmode, errorcode;
int color, maxcolor, ht;
int y = 10;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult ();
if (errorcode != grOk) { /* an error occurred */
printf ("Graphics error: %s\n", grapherrormsg(errorcode));
printf ("Press any key to halt:");
getchO ;
exit(l); /* terminate with an error code */
}
maxcolor = getmaxcolor ();
ht = 2 * textheight("W");
/* display the default colors */
for (color=l; color<=maxcolor; color++) {
setcolor (color);
sprintf (msg, "Color; %d", color);
outtextxy(l, y, msg);
y += ht;
}
/* wait for a key */
getchO ;
/* black out the colors one by'one */
for (color=l; color<=maxcolor; color++) {
setpalette (color, BLACK);
. getchO ;
}
/* clean up */
closegraphO ;
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/graphics.h/setpalette/
· short form /3.1/setpalette/