Coders Packet

Color Coding in C++

By Apurv Singh

Here, we get to know about how to add color to text and the background in C++. The header file for this command is windows.h or stdlib.h in C++.

Color Coding in C++ 

We get a new experience when we see colors in C++ as black and white is more like an old school when compared to daily changing technology. The idea can change the color of both the background and text in the output screen.


system("Color PQ")

To change the background color, we need to change the value of P of the syntax given to the corresponding color we want and for change in text color change the value of Q in the given syntax.

Let’s see how to change the color of the text in C++.

using namespace std;
int main()
/* 1	Blue	9	Light Blue
   2	Green	0	Black
   3	Aqua	10	Light Green
   4	Red	11	Light Aqua
   5	Purple	12	Light Red
   7	White	14	Light Yellow
   8	Gray	15	Bright White */
        HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE); //just once
        SetConsoleTextAttribute(color, 10);
        SetConsoleTextAttribute(color, 11);
        cout<<" is";
        SetConsoleTextAttribute(color, 12);
        cout<<" a";
        SetConsoleTextAttribute(color, 13);
        cout<<" coder";
        SetConsoleTextAttribute(color, 14);
        cout<<" packet"<<endl;
      return 0;

Output :


Now let's have a look at how to change the color of the background in C++.

using namespace std;

int main()

 /* Color id	Color	Color id	Color
  1	        Blue	9   	      Light Blue
  2        	Green	0   	      Black
  3	        Aqua	A	      Light Green
  4	        Red	B	      Light Aqua
  5	       Purple	C	      Light Red
  6            Yellow	D	      Light Purple
  7     	White	E	      Light Yellow
  8     	Gray	F	      Bright White */

  // C for background Color(Light Red)
  // 3 for text color(Aqua)
  system("Color C3");
  cout << "This ";

  // 2 for background Color(Green)
  // 6 for text color(Yellow)
  system("Color 26");
  cout << " changes ";

  // E for background Color(Light Yellow)
  // C for text color(Light Red)
  system("Color EC");
  cout << "background color";

  return 0;



In the above codes, we can see color codes of some of the basic colors, but the best thing is that it doesn’t stop here, we can go upto 300 different color combinations.


Download Complete Code


No comments yet