How to Use Setcolor in C or C++ Graphics

set color

Introduction :

Using graphics programming in C and C++ language, you can create various shapes, text, images and animations on your application. Here, you have to control and manipulate the color of different graphical elements in such graphics applications. In C or C++ graphics programming language, the setcolor() function is commonly used to set the current drawing color.

In this article, I shall show you how to use setcolor() function in the C or C++ graphics programming language. Here, I also show you how to change the current drawing color using setcolor() function in graphics mode.

What is setcolor() function :

The setcolor() is a function of the graphics.h library in Turbo C++ IDE. It is used to set the current drawing color in C or C++ graphics programming language. Using setcolor() function, you can draw colourful shapes such as lines, circles and rectangles on the graphics screen. If you call setcolor() function with a color, all the subsequent drawing functions will use this color until it is called again with a different color.

Syntax of the setcolor() function :

In C or C++ graphics programming language the following code is the syntax of setcolor() function.

The setcolor() function does not return any value. It takes an argument (color_constant) that represents the color to be set as current drawing color. This argument can be a predefined color constant defined in the graphics.h library.

The default drawing color of graphics.h library is white. The following predefined color constants (16) 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 :

This program is the example of using setcolor() function in C or C++ graphics programming language. In this program, you see the drawing of circles with 16 different colors on the graphics screen. Here, all drawing color of circle changes automatically one by one with a delay. After completing the program, you have to press any key to stop the program.

Explanation of the program :

In the program, first you have to include graphics.h, conio.h and dos.h libraries or header files. Then, declare integer variables such as “graphic_driver”, “graphic_mode” and “i” in the main() function. Now, you need to initialize the graphics mode using initgraph() function with the graphics driver and mode. In the for loop, call the setcolor() function with variable “i”.

Here, the value of “i” variable will be changed to set different color of the circle. Then, draw a circle using the currently set color by circle() function on the screen. Here, you can use delay() function to introduce a delay for visual effect. The getch() function takes a key press from the user before close the program. At last, close the graphics mode using closegraph() function.

How run the program :

You have to install the Turbo C++ IDE on your pc to run the program. Then, open it and create a new C or C++ file with .c or .cpp extension. Now, copy the below code and paste in the C or C++ file. You have to know how to copy paste in the Turbo C++ IDE. Do you know how to use graphics.h in Turbo C++ IDE?

Source code of the program :

The following code is the source code of the program.

Output of the program :

The below image is the output of the program.

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

Conclusion :

After completing the above article, you have learned how to use setcolor() function in the C or C++ graphics programming language. Here, you have also learned the syntax and usage of setcolor() function. Now you can use setcolor() function to create your own programs. Thank you for visiting my site.

Scroll to Top