Color settings function

setcolor() in graphics.h

void far setcolor(int color);

The setcolor() function sets the current drawing color for BGI graphics operations. When you call a shape outline function (like line(), rectangle(), circle(), ellipse(), or arc()) or render text (using outtextxy() or outtext()), BGI queries the active drawing color register and draws pixels accordingly. Changing the color with setcolor() does not alter graphics already drawn on screen; it only determines the color of vector coordinates drawn after the function call.

Foreground color management is highly dependent on the active graphics driver and mode configured during initgraph(). In the default VGA mode (e.g. VGAHI), BGI provides a palette of 16 distinct colors, numbered 0 through 15. These indices map to specific pre-defined hardware colors, and standard named constants are provided in graphics.h so developers do not need to memorize numeric codes. If you are writing portable graphics code, using named constants such as YELLOW or LIGHTBLUE makes code significantly easier to read and maintain across compilers.

Parameters

NameTypeDescription
colorintAn 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

IndexConstant NameTypical Color Representation
0BLACKBlack (matches default background)
1BLUEDark Blue
2GREENDark Green
3CYANCyan / Aqua
4REDDark Red
5MAGENTADark Magenta / Purple
6BROWNBrown / Dark Yellow
7LIGHTGRAYLight Gray
8DARKGRAYDark Gray
9LIGHTBLUEBright Blue
10LIGHTGREENBright Green
11LIGHTCYANBright Cyan
12LIGHTREDBright Red
13LIGHTMAGENTABright Pink / Magenta
14YELLOWYellow
15WHITEWhite

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++.