Practical guide

graphics.h color constants explained

One of the nicest parts of graphics.h is that colors are easy to use. Instead of writing long color codes, you work with simple names such as WHITE, RED, YELLOW, and LIGHTCYAN. That simplicity is great for students, but it also causes confusion when people mix up drawing color, background color, and fill color.

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
0BLACK
1BLUE
2GREEN
3CYAN
4RED
5MAGENTA
6BROWN
7LIGHTGRAY
8DARKGRAY
9LIGHTBLUE
10LIGHTGREEN
11LIGHTCYAN
12LIGHTRED
13LIGHTMAGENTA
14YELLOW
15WHITE

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.