C++ setcolor() - Set Color of Text and Object

- July 11, 2018
color setting for text in cpp


In C++, the setcolor() function is used to define color of the object and text in graphics mode. This function can be used to customize text color according to your need. To use this function include "graphics.h" header file in your program.

Syntax of setcolor() Function

The general syntax of this function is as looks like:

setcolor(color_value);
Where:
color_value It represents value of specific color that is from 0 to 15. It may also be a numerical constant identifier such as RED, GREEN or BLUE etc.

Table of Colors Name and Values

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

Example of setcolor() Function

The following C++ program displays text "Best of Luck" in various colors.

#include<iostream>
#include<canio>
#include<graphics>
using namespace std;
  main()
   {
      int driver, mode, co;
      driver = DETECT;
      initgraph (&driver, &mode, " ");
      cleardevice();
      for (co = 0; c<= 15; co++)
      {
         settextstyle (0, 0, 2);
         outtextxy (100, 10+co*20, "Best of Luck");
         outtextxy(450, 400, "Press any key");
         getch();
      }
  closegraph();
}

Leave your reply.
EmoticonEmoticon