Wednesday, February 29, 2012

playing with colors in C graphics


Hello all, Lets play with colors in c Graphics. In this example, lines are shown in different colors. The number of colors is limited by the graphic mode which is detected by the initgraph(). read "C graphics" for basic c graphic program.  To check the maximum colors a graphic mode supports, use getmaxcolor() function. In current system, the graphic mode supports 15 colors. Use setcolor() to change the color of the next drawing object. In this example, after setting the color, the line is drawn in that color.

The programs output is shown below.





/* Presented by www.ncooltips.com
 * Colors example.
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gdriver=DETECT,gmode=0;
int color=0;
int y=10;
/* Open graphic mode
Detect grahic driver and supporting mode
 automatically*/
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

for(color=0;color<getmaxcolor();color++,y+=20){
setcolor(color);

  /* draw  line default */
  line(0,y,getmaxx(),y);

}

getch();

/*Close graphic mode */
closegraph();

}




please leave comments behind. Thanks
Pin It
Related Posts Plugin for WordPress, Blogger...