Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++

Add Color to Console Projects

Rate me:
Please Sign up or sign in to vote.
4.86/5 (67 votes)
14 Nov 2009CPOL2 min read 212.3K   6K   96   45
With the use of a few support functions, color can be added to console applications running under Win32.

Sample Image - maximum width is 600 pixels

Introduction

The standard C++ iostream library does not know about colors. Hence, console applications tend to look very boring and often fail to emphasize the important bits in the flood of text. As I normally use console applications to do unit testing, I wanted the input conditions and the results of the test cases to stand out and be easily interpreted (i.e. PASSED in green and FAIL in red).

The Win32 API has some functions to manipulate the color of characters in a console but these are C style functions that do not interface seemlessly with the C++ stream style programming. Moreover, I want to keep my main code clean and uncluttered. Anybody reading my test cases should not have to wade through dozens of lines of output formatting code.

To that end, I created a handy set of iostream manipulators that allow me to change background and foreground colors at any point in the output stream.

Using the Code

All the necessary code is contained in one header file: Console.h. To guard against name clashing, the functions in this file are placed in a single namespace: "JadedHoboConsole".

As the following snippet shows, the use of the iostream color manipulators is simplicity itself.

C++
#include "Console.h"

namespace con = JadedHoboConsole;

int main()
{
    using std::cout;
    using std::endl;

    cout << "I like " << con::fg_green << "green" << con::fg_white << " eggs and ham." 
         << endl;
}

The header file has stream manipulators to set background colors (those are the ones starting with bg_) and to set foreground colors (the ones prefixed with fg_). Additionally, I created a manipulator to clear the screen (clr).

This is the list of available manipulators:

  • fg_black
  • fg_gray
  • fg_white
  • fg_reg
  • fg_green
  • fg_blue
  • fg_cyan
  • fg_magenta
  • fg_yellow
  • bg_black
  • bg_gray
  • bg_white
  • bg_reg
  • bg_green
  • bg_blue
  • bg_cyan
  • bg_magenta
  • bg_yellow
  • clr

Points of Interest

New iostream manipulators are easy to implement because the most important part has already been done by the standardization committee when they included the basic_ostream& operator<<( basic_ostream& (*pf)(basic_ostream&)); overload in the standard. Thus, any function with the correct signature can be used as an stream manipulator:

C++
std::ostream& Copyleft( std::ostream& os )
{
    os << "(L)2004 by EgoTripper";
}

cout << Copyleft << endl;

History

  • December 2004: First published
  • November 2009: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWOW! Pin
JimmyDreak1-Dec-06 18:41
JimmyDreak1-Dec-06 18:41 
QuestionHow to get it? PURPURE! Pin
gravellpawepek15-Sep-06 2:12
gravellpawepek15-Sep-06 2:12 
GeneralAmazing code, but... Pin
Narook15-Jul-06 6:18
Narook15-Jul-06 6:18 
GeneralError windows.h Pin
Muchailpski16-Jun-06 11:16
Muchailpski16-Jun-06 11:16 
GeneralRe: Error windows.h Pin
Da-Huntha25-Oct-07 13:30
Da-Huntha25-Oct-07 13:30 
GeneralExcellent :) Pin
yoco3154-Jun-06 19:32
yoco3154-Jun-06 19:32 
GeneralGreat Job Pin
Molerat24-May-06 7:36
Molerat24-May-06 7:36 
GeneralRe: Great Job Pin
Molerat15-Jun-06 18:03
Molerat15-Jun-06 18:03 
after working with it a bit more i realized i was just being a bit stupid - fgLoWhite is also the default color, but it is still nice to have a fg_default function to implement it.

the way these colors are working are through the default windows palette though right (or am i wrong about that)?
GeneralAnother thing I noticed missing Pin
Sir Bastille30-Nov-05 6:13
Sir Bastille30-Nov-05 6:13 
QuestionOther Colors? Pin
donaldGuy21-May-05 18:03
donaldGuy21-May-05 18:03 
AnswerRe: Other Colors? Pin
Jaded Hobo24-May-05 7:18
Jaded Hobo24-May-05 7:18 
GeneralAdding parameters Pin
nicchap4-Apr-05 11:25
nicchap4-Apr-05 11:25 
GeneralRe: Adding parameters Pin
nicchap5-Apr-05 16:05
nicchap5-Apr-05 16:05 
GeneralRe: Adding parameters [modified] Pin
RadekHa17-Jul-08 21:37
RadekHa17-Jul-08 21:37 
GeneralSmall correction needed Pin
Skyhawk29-Dec-04 14:29
Skyhawk29-Dec-04 14:29 
GeneralRe: Small correction needed Pin
Jaded Hobo30-Dec-04 5:48
Jaded Hobo30-Dec-04 5:48 
GeneralGreat job Pin
WayneC29-Dec-04 0:36
WayneC29-Dec-04 0:36 
Generalgood Pin
spetsnaz28-Dec-04 11:03
spetsnaz28-Dec-04 11:03 
GeneralInteresting! Pin
WREY27-Dec-04 6:48
WREY27-Dec-04 6:48 
GeneralRe: Interesting! Pin
zghelp20-Nov-05 17:16
zghelp20-Nov-05 17:16 
GeneralPerfect! Pin
Dandy Cheung26-Dec-04 1:29
Dandy Cheung26-Dec-04 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.