setcolor


Syntax

#include <graphics.h>

void setcolor(int color);

Description
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 drawing colors shown below are available for the CGA and EGA, respectively.
Palette Number    Three Colors
0 LIGHTGREEN   LIGHTRED YELLOW
1 LIGHTCYAN LIGHTMAGENTA   WHITE
2 GREEN RED BROWN
3 CYAN MAGENTA LIGHTGRAY
NameValue
BLACK 0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTGRAY7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15
You select a drawing color by passing either the color number itself or the equivalent symbolic name to setcolor. For example, in CGAC0 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.

Windows Notes [WIN]
The winbgim version allows the color argument to be an ordinary BGI color (from 0 to 15) or an RGB color.

See also
getcolor
getmaxcolor
graphresult
setallpalette
setbkcolor
setpalette

Example

/* setcolor 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:");

      getch();

      exit(1);               /* 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=1; 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);

      getch();

   }



   /* clean up */

   closegraph();

   return 0;

}


Back to index