How to Use Setbkcolor in C or C++ Graphics

set bk color

Introduction :

Using graphics programming in C or C++ language, you can create visual applications, animations and games. To develop such program, you have to manage the background color of the graphics window. One of the essential functions of graphics.h library in Turbo C++ IDE is setbkcolor() function which is used to set the background color of the graphics screen. It helps you to customize the canvas on which various graphic elements can be drawn.

In this article, I shall show you how to use setbkcolor() function in C or C++ graphics programming language. Here, you can learn how to change current background color in Turbo C++ IDE in the graphics mode.

What is setbkcolor() function :

The setbkcolor() is a function of the graphics.h library in Turbo C++ IDE. It is used in C or C++ graphics programming language to set the background color of the graphics window or screen. This background color fills the entire screen of the graphics window. Using the setbkcolor() function, you can create more visually appealing graphics applications to set appropriate background color of graphics window.

Syntax of the setbkcolor() function :

The following code is the syntax of the setbkcolor() function in C or C++ graphics programming language.

The return value of setbkcolor() function is void that means it does not return any value. The “color_index” is an integer parameter or argument of setbkcolor() function that represents the color to be set as the background of graphics screen. The “color_index” parameter is a predefined color constants available in graphics.h library such as BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE, LIGHTBLUE etc.

Predefined color constants in graphics.h :

Generally, default background color of graphics window is black. There are 16 predefined color constants in graphics.h library. You can use them either using their names or values. The following predefined color constants are defined in the graphics.h library.

  • Name ———— Value
  • BLACK ———— 0
  • BLUE ———— 1
  • GREEN ———— 2
  • CYAN ———— 3
  • RED ———— 4
  • MAGENTA ———— 5
  • BROWN ———— 6
  • LIGHTGRAY ———— 7
  • DARKGRAY ———— 8
  • LIGHTBLUE ———— 9
  • LIGHTGREEN ———— 10
  • LIGHTCYAN ———— 11
  • LIGHTRED ———— 12
  • LIGHTMAGENTA ———— 13
  • YELLOW ———— 14
  • WHITE ———— 15

About the program :

It is a simple C program that displays the different background colors one by one. When you run the program on your PC, you can see a continuous change of background colors after small intervals in a graphics window. After that, the program waits for a key press to close the graphics mode.

Explanation of the program :

First, you have to include the header files such as graphics.h, conio.h and dos.h in your program. Then, you can declare variables “graphic_driver”, “graphic_mode” and “i” in the main function. The “graphic_driver” and “graphic_mode” variables are used for initializing the graphics system. The “i” variable is used in the for loop. Now, Initialize the graphics system using initgraph() function with the specified graphics driver and mode.

The path “//turboc3/bgi” is the directory where BGI file is located on my PC. The path of BGI file of your PC may be different. In the for loop, call the setbkcolor() function with “i” variable. Here, the value of “i” will be changed from 0 to 15. Then, using delay() function, delay in between color changes to see each color properly.

After that, take a key press before closing the graphics window using getch() function. Finally, close the graphics mode using closegraph() function.

How run the program :

To run the program, you have to install the Turbo C++ IDE on your pc. Then, open the IDE and create a C or C++ file with .c or .cpp extension. Now, copy the below code and paste in the file you have just created. Do you know how to copy paste in the Turbo C++ IDE, if do not know follow this link. You have to also know how to use graphics.h in Turbo C++ IDE.

Source code of the program :

The following code for setbkcolor() function where all background color changes automatically in C or C++ graphics programming language.

Output of the program :

After running the program on your PC, you can see the output of the program like below image.

output of setbkcolor() function in C or C++ graphics programming language

Conclusion :

The setbkcolor() function is a simple function setting the background color of graphics screen. Now, you have learned how to use setbkcolor() function in the C programming language. So, you can use setbkcolor() function in your graphics programs. Thank you for visiting my site.

Scroll to Top