Click here to Skip to main content
12,828,883 members (45,725 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


89 bookmarked
Posted 21 Dec 2004

Add Color to Console Projects

, 14 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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


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.

#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:

std::ostream& Copyleft( std::ostream& os )
    os << "(L)2004 by EgoTripper";

cout << Copyleft << endl;


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


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


About the Author

Jaded Hobo
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Pranit Kothari21-May-12 19:25
memberPranit Kothari21-May-12 19:25 
Questionhow to use as argument in a fonction Pin
SublimeDeath9-Mar-11 14:50
memberSublimeDeath9-Mar-11 14:50 
AnswerRe: how to use as argument in a fonction Pin
Jaded Hobo10-Mar-11 11:01
memberJaded Hobo10-Mar-11 11:01 
Hi Charles,

Your question actually makes a lot of sense. The first thing to consider is the type of the color manipulators. In the section 'Points of interest' you can see that the color manipulators are not simple objects but functions. Thus if you want to pass a manipulator as a parameter you will have to pass the appropriate function pointer. That is all there really is to it Smile | :)

But since you indicate being a bit new to C++ I'll be a bit more explicit.

First thing to do when dealing with function pointers is to make the whole function pointer declaration somewhat readable with a typedef:

typedef std::basic_ostream& (*ColorManipulatorFnPtr)(std::basic_ostream&));

This line creates the correct function pointer type with the obvious name "ColorManipulatorFnPtr". Now it becomes a lot easier to declare a function that accepts a color manipulator as one of its arguments:

void center( const std::string& text, char filling, ColorManipulatorFnPtr FillColor )
    const size_t ( (80 - text.length())/2 );

    for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;
    std::cout << con::fg_green <<; text;
    for (unsigned int i = 0; i < PadSize; i++) std::cout << FillColor << filling;

    std::cout << con::fg_white;

int main()
    center( std::string( "Hello world!" ), '*', con::fg_red );

    return 0;

Disclaimer: untested code! Use at your own peril. Symptoms of invoking UB may include but is not limited to wiped hard-disks haku's in your boot sector and WW3 Poke tongue | ;-P
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath11-Mar-11 17:50
memberSublimeDeath11-Mar-11 17:50 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath11-Mar-11 18:09
memberSublimeDeath11-Mar-11 18:09 
GeneralRe: how to use as argument in a fonction Pin
Jaded Hobo12-Mar-11 10:00
memberJaded Hobo12-Mar-11 10:00 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath12-Mar-11 11:59
memberSublimeDeath12-Mar-11 11:59 
QuestionEasy to write Tcl wrapper ? Pin
klausnrooster3-Jul-10 23:15
memberklausnrooster3-Jul-10 23:15 
AnswerRe: Easy to write Tcl wrapper ? Pin
Jaded Hobo4-Jul-10 11:04
memberJaded Hobo4-Jul-10 11:04 
GeneralErrors with Dev-C++ Pin
Adam Kadlec19-Nov-09 11:30
memberAdam Kadlec19-Nov-09 11:30 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 14 Nov 2009
Article Copyright 2004 by Jaded Hobo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid