setcolor
Sets the current drawing color using the palette.
Defined in header <graphics.h>
Syntax
#include <graphics.h>
void far setcolor(int color);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ |
Remarks
setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
The current drawing color is the value to which pixels are set when lines, and so on are drawn. The following tables show the drawing colors available for the CGA and EGA, respectively.
Constantassigned to colornumber (pixel value) Palette
| number | 1 | 2 | 3 |
| 0 | CGA_LIGHTGREEN | CGA_LIGHTRED CGA_YELLOW | |
| 1 | CGA_LIGHTCYAN | CGA__LIGHTMAGENTA CGA_WHITE | |
| 2 | CGA_GREEN | CGA__RED CGA_BROWN | |
| 3 | CGA_CYAN | CGA.MAGENTA CGA_LIGHTGRAY |
| Numericvalue | Symbolic name |
| 0 | BLACK | 8 | DARKGRAY |
| 1 | BLUE | 9 | LIGHTBLUE |
| 2 | GREEN | 10 | LIGHTGREEN |
| 3 | CYAN | 11 | LIGHTCYAN |
| 4 | RED | 12 | LIGHTRED |
| 5 | MAGENTA | 13 | LIGHTMAGENTA |
| BROWN | 14 | YELLOW | |
| LIGHTGRAY | 15 | WHITE |
You select a drawing color by passing either the color number itself or the equivalent symbolic name to setcolor. For example, in CGACO mode, the palette contains four colors: the background color, light green, light red, and yellow. In this mode, either setcolor(3) or setcolor(CGA_YELLOW) selects a drawing color of yellow.
Return value
None.
See also
getcolorgetmaxcolorgraphresultseta11palettesetbkcolorsetpalette
Example
*include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select driver and mode that supports multiple drawing colors */
int gdriver = EGA, gmode = EGAHI, errorcode;
int color, maxcolor, x, y;
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 */
/* maximum color index supported */
maxcolor = getmaxcolor();
/* for centering text messages */
settextjustify(CENTER„TEXT, CENTER_TEXT);
X = getmaxx() / 2;
y = getmaxy() / 2;
/* loop through the available colors */
for (color=l; color<=maxcolor; color++) {
cleardevice(); /* clear the screen */
setcolor(color); /* select new background color */
/* output a messsage */
sprintf(msg, "Color: %d", color)
outtextxy(x, y, msg);
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/setcolor/
· short form /3.1/setcolor/