Click here to Skip to main content
15,176,221 members
Articles / Programming Languages / C++
Posted 21 Dec 2004

Tagged as


94 bookmarked

Add Color to Console Projects

Rate me:
Please Sign up or sign in to vote.
4.86/5 (65 votes)
14 Nov 2009CPOL2 min read
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

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 
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 
GeneralRe: Errors with Dev-C++ Pin
Adam Kadlec19-Nov-09 14:30
MemberAdam Kadlec19-Nov-09 14:30 
GeneralRe: Errors with Dev-C++ Pin
Jaded Hobo25-Nov-09 10:00
MemberJaded Hobo25-Nov-09 10:00 
GeneralGood, but... Pin
elbertlev4-Nov-09 14:04
Memberelbertlev4-Nov-09 14:04 
GeneralRe: Good, but... Pin
Jaded Hobo11-Nov-09 9:25
MemberJaded Hobo11-Nov-09 9:25 
GeneralRe: Good, but... Pin
Jaded Hobo17-Nov-09 9:18
MemberJaded Hobo17-Nov-09 9:18 
GeneralThank you. Pin
musicdev21-Mar-08 13:40
Membermusicdev21-Mar-08 13:40 
GeneralThanks a million Pin
spinelesspsycho22-Nov-07 7:11
Memberspinelesspsycho22-Nov-07 7:11 
Questionclearscreen func? Pin
Member 38925979-Mar-07 23:45
MemberMember 38925979-Mar-07 23:45 
AnswerRe: clearscreen func? Pin
Jaded Hobo20-Mar-07 12:05
MemberJaded Hobo20-Mar-07 12:05 
AnswerRe: clearscreen func? Pin
Dathletez1327-Aug-07 22:46
MemberDathletez1327-Aug-07 22:46 
GeneralUsing Dev C++ Pin
naMretupmoC22-Feb-07 10:46
MembernaMretupmoC22-Feb-07 10:46 
GeneralAdding different color to stdout and stderr Pin
sachin ahuja30-Jan-07 22:57
Membersachin ahuja30-Jan-07 22:57 
AnswerRe: Adding different color to stdout and stderr Pin
Jaded Hobo4-Feb-07 7:15
MemberJaded Hobo4-Feb-07 7:15 
GeneralWOW! Pin
JimmyDreak1-Dec-06 19:41
MemberJimmyDreak1-Dec-06 19:41 
QuestionHow to get it? PURPURE! Pin
gravellpawepek15-Sep-06 3:12
Membergravellpawepek15-Sep-06 3:12 

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.