C++ gurus and learners, we program ourselves :P all work is CC licensed

Saturday, November 16

Colour Coding in C++

15:37 By

The first time i saw colour in c++ was amazing experience because just black  and white stuff is more like old school comparing to modern software and  technology. It was hard to find well defined code and definition for colours in c++ language ; took me a while but i got it finally so here is this code in simple language, hope it helps you...

_________________________________________________________________________
To change whole screen colour i.e. foreground (text) and background, it is very simple and         easy command.

The header file for this command is :                   <windows.h>
The command   is :                                          system("Colour _ _");

In place of _ _ you have to enter colour combination you want ; first place takes background colour and 2nd place takes foreground colour (text colourthis is list of all colours available for this command:

1_____Blue                                       A______Light Green
2_____Green                                    B______Light Aqua
3_____Aqua                                      C______Light Red
4_____Red                                        D______Light Purple
5_____Purple                                   E______Light Yellow
6_____Yellow                                   F______Bright White 
7_____White
8_____Gray
9_____Light Blue
0_____Black

It's not perfact colouring but it is simple to use

  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6.            system("Color 0A");  // first 0 = black background second A = light green
  7.            cout<<"This is my fev. colour combination for this command"<<endl;
  8.       return 0;
  9. }
This is my fev. colour combination for this command              

Press any key to continue . . .                                                 
=========================================================================

The detailed coding

_________________________________________________________________________
To change minor colour i.e. colour of just 1 character so that we can use as much colour combinations ; it comes to this code because command showed above will not allow you to print 2 or more colours in single output combination. This command is not capable of changing whole background so i prefer to select background colour from system command and then change text colour from this code i'm about to share with you guys.

This must be wirten at start of main:   HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
The header file for this command is :                   <windows.h>
The command   is :                            SetConsoleTextAttribute(color, _ );


In place of _ you have to enter colour code you want ; there is a brief list of some basic colours for this command:

1_____Blue                                       10______Light Green
2_____Green                                    11______Light Aqua
3_____Aqua                                      12______Light Red
4_____Red                                        13______Light Purple
5_____Purple                                   14______Light Yellow
6_____Yellow                                   15______Bright White 
7_____White
8_____Gray
9_____Light Blue
0_____Black


  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6.         HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE); //just once
  7.         SetConsoleTextAttribute(color, 10);
  8.            cout<<"This";
  9.         SetConsoleTextAttribute(color, 11);
  10.         cout<<" is";
  11.         SetConsoleTextAttribute(color, 12);
  12.         cout<<" a";
  13.         SetConsoleTextAttribute(color, 13);
  14.         cout<<" colour";
  15.         SetConsoleTextAttribute(color, 14);
  16.         cout<<" combination"<<endl;
  17.       return 0;
  18. }
This is a colour combination                                                   

Press any key to continue . . .                                                 

But the best thing is that it doesnot stops here at 15 ; you can go up to 300 for different colour combinations for headline your text on your program; enjoy
Like this post then i will tell you how to use 2nd one command very easily like this:
clr( _ );
that's it..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

5 comments :

  1. I usually used "CF" of "FC" for color coding in my C++ tasks

    ReplyDelete
  2. wish i had those programs of first semester with me, those shashkaas :P

    ReplyDelete
  3. Really Professional Blogging ,,,, Usman Shery Good One ,, It is Really Helpfull

    ReplyDelete
  4. May ALLAH bless you. Aameen :)

    ReplyDelete
  5. thanks i appritiate it
    i hope you are finding me useful?

    ReplyDelete