Parameters
| Name | Type | Description |
|---|---|---|
color | int | An integer representing the color index or a pre-defined BGI color name constant. Valid values range from 0 to getmaxcolor(). |
The color index argument typically corresponds to one of the 16 BGI color constants listed in the table below. Note that BLACK is index 0 and WHITE is index 15. If you specify an index larger than the value returned by getmaxcolor(), the behavior is driver-dependent (usually wrapped around using modulo arithmetic or ignored).
Standard BGI Color Table
| Index | Constant Name | Typical Color Representation |
|---|---|---|
0 | BLACK | Black (matches default background) |
1 | BLUE | Dark Blue |
2 | GREEN | Dark Green |
3 | CYAN | Cyan / Aqua |
4 | RED | Dark Red |
5 | MAGENTA | Dark Magenta / Purple |
6 | BROWN | Brown / Dark Yellow |
7 | LIGHTGRAY | Light Gray |
8 | DARKGRAY | Dark Gray |
9 | LIGHTBLUE | Bright Blue |
10 | LIGHTGREEN | Bright Green |
11 | LIGHTCYAN | Bright Cyan |
12 | LIGHTRED | Bright Red |
13 | LIGHTMAGENTA | Bright Pink / Magenta |
14 | YELLOW | Yellow |
15 | WHITE | White |
Return Value
setcolor() returns void. To query what the current active foreground color index is, you can call the complementary function getcolor(), which returns an integer value corresponding to the current state.
Code Examples
All 16 Colors Demo
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int i, y = 30;
char text[20];
initgraph(&gd, &gm, "");
for(i = 0; i < 16; i++) {
// Change the active drawing color
setcolor(i);
// Draw a line across the screen in this color
line(50, y, 200, y);
// Print the color index number next to it
sprintf(text, "Color Index: %d", i);
outtextxy(220, y - 4, text);
y += 25;
}
getch();
closegraph();
return 0;
}
This program loops through all 16 standard indices, using setcolor() to apply each color sequentially. For each index, it draws a matching line and outputs a descriptive text label. This is a great way to verify how the colors look in your current compiler environment.
Colorful geometric pattern
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int r;
initgraph(&gd, &gm, "");
for (r = 20; r <= 200; r += 15) {
// Cycle colors based on radius to alternate outlines
setcolor((r / 15) % 15 + 1); // Avoid 0 (Black) so outlines are visible
circle(320, 240, r);
}
getch();
closegraph();
return 0;
}
This code draws a set of concentric circles, changing the drawing color index in each iteration. By avoiding index 0 (which is black), all shapes remain highly visible against the black background, creating an eye-catching nested pattern.
Common Mistakes
Using RGB hex codes
Modern graphics libraries use 24-bit RGB hex values like 0xFF0000 for colors. However, standard graphics.h BGI functions operate on a indexed color palette (0 to 15). Passing a high value like 0xFF0000 to setcolor() will be clipped or interpreted as black, rendering your drawings invisible.
Writing constant names in lowercase
C is a case-sensitive language. The constants defined in graphics.h are capitalized (e.g., RED, YELLOW, BLUE). Writing setcolor(red); in your code will result in a compiler syntax error indicating that red is an undefined identifier.
Expecting setcolor() to change fills
Fills created by bar(), fillellipse(), and fillpoly() are controlled by the fill settings from setfillstyle(), not setcolor(). If you draw a bar using bar(), calling setcolor(RED) will have no effect on the bar's internal color. Fills require setfillstyle().
Invisible drawing (matching background)
If you call setcolor(BLACK) or set the color index to match the current background color (which defaults to index 0, black), your outlines and text will be invisible. If shapes seem to disappear or fail to draw, verify that you did not accidentally configure the drawing color to match the background.
Try it online
Test colors instantly. Paste our code snippets into the browser-based editor panel to execute C graphics online without setting up Turbo C++.