Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Add Color to Console Projects

0.00/5 (No votes)
14 Nov 2009 1  
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.

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

History

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here