What color constants are in graphics.h
Classic graphics.h programs typically use named constants from the BGI palette. These names map to fixed color indices, which means you can write readable programs without remembering raw numbers all the time.
| Index | Constant |
|---|---|
| 0 | BLACK |
| 1 | BLUE |
| 2 | GREEN |
| 3 | CYAN |
| 4 | RED |
| 5 | MAGENTA |
| 6 | BROWN |
| 7 | LIGHTGRAY |
| 8 | DARKGRAY |
| 9 | LIGHTBLUE |
| 10 | LIGHTGREEN |
| 11 | LIGHTCYAN |
| 12 | LIGHTRED |
| 13 | LIGHTMAGENTA |
| 14 | YELLOW |
| 15 | WHITE |
Even if your lab sheet focuses on the names rather than the indices, knowing the mapping helps during viva questions and when reading older examples.
Where each color-setting function applies
setcolor()
setcolor() changes the active drawing color. That affects outlines, lines, points, arcs, circles, and text drawn with outtextxy().
setbkcolor()
setbkcolor() changes the graphics background. This matters when you want a dark canvas, a colored screen, or better contrast for text.
setfillstyle()
setfillstyle() controls how closed regions are filled. The color passed here is separate from the outline color. That is why a region can have a white border but a green fill.
Example: one screen, multiple color roles
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(BLACK);
setcolor(WHITE);
rectangle(80, 60, 560, 380);
setcolor(YELLOW);
circle(180, 180, 60);
setcolor(LIGHTCYAN);
outtextxy(120, 280, "Foreground text");
setfillstyle(SOLID_FILL, LIGHTGREEN);
bar(320, 120, 480, 240);
getch();
closegraph();
return 0;
}
In this example, the background is black, the outer frame is white, the circle is yellow, the label is light cyan, and the filled rectangle is light green. That separation makes the different color roles much easier to see.
Good beginner color combinations
- BLACK background + WHITE text: easy to read.
- BLACK background + YELLOW shapes: strong contrast for diagrams.
- BLACK background + LIGHTCYAN labels: nice for captions and coordinates.
- WHITE outline + LIGHTGREEN fill: useful for bars and blocks.
These combinations are practical because they keep the screen readable on classroom projectors and low-resolution displays.
Common color mistakes in graphics.h
- Drawing text in the same color as the background. The text exists, but it appears invisible.
- Assuming setcolor() changes fill color too. It does not.
- Using floodfill() with the wrong boundary color. The fill can leak or fail if the border color does not match.
- Choosing two low-contrast colors. The shape may be technically correct but visually weak.
Best way to remember the palette
Do not memorize the table in one sitting. Write a small program that draws 16 labels, one for each constant, and use the name as the displayed text. Seeing the colors on screen turns the list into something visual instead of abstract.
Practice this live
Open the online compiler and change only one line at a time: first setcolor(), then setbkcolor(), then setfillstyle(). That makes the role of each function very clear. For deeper syntax details, keep the setcolor(), setbkcolor(), and setfillstyle() docs open beside it.