Click here to Skip to main content
11,578,739 members (62,879 online)
Click here to Skip to main content

Add Color to Console Projects

, 14 Nov 2009 CPOL 142.1K 4.6K 88
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

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

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 18:25
memberPranit Kothari21-May-12 18:25 
Questionhow to use as argument in a fonction Pin
SublimeDeath9-Mar-11 13:50
memberSublimeDeath9-Mar-11 13:50 
AnswerRe: how to use as argument in a fonction Pin
Jaded Hobo10-Mar-11 10:01
memberJaded Hobo10-Mar-11 10: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 16:50
memberSublimeDeath11-Mar-11 16:50 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath11-Mar-11 17:09
memberSublimeDeath11-Mar-11 17:09 
GeneralRe: how to use as argument in a fonction Pin
Jaded Hobo12-Mar-11 9:00
memberJaded Hobo12-Mar-11 9:00 
GeneralRe: how to use as argument in a fonction Pin
SublimeDeath12-Mar-11 10:59
memberSublimeDeath12-Mar-11 10:59 
QuestionEasy to write Tcl wrapper ? Pin
klausnrooster3-Jul-10 22:15
memberklausnrooster3-Jul-10 22:15 
AnswerRe: Easy to write Tcl wrapper ? Pin
Jaded Hobo4-Jul-10 10:04
memberJaded Hobo4-Jul-10 10:04 
GeneralErrors with Dev-C++ Pin
Adam Kadlec19-Nov-09 10:30
memberAdam Kadlec19-Nov-09 10:30 
GeneralRe: Errors with Dev-C++ Pin
Adam Kadlec19-Nov-09 13:30
memberAdam Kadlec19-Nov-09 13:30 
GeneralRe: Errors with Dev-C++ Pin
Jaded Hobo25-Nov-09 9:00
memberJaded Hobo25-Nov-09 9:00 
GeneralGood, but... Pin
elbertlev4-Nov-09 13:04
memberelbertlev4-Nov-09 13:04 
GeneralRe: Good, but... Pin
Jaded Hobo11-Nov-09 8:25
memberJaded Hobo11-Nov-09 8:25 
GeneralRe: Good, but... Pin
Jaded Hobo17-Nov-09 8:18
memberJaded Hobo17-Nov-09 8:18 
GeneralThank you. Pin
Member 269292821-Mar-08 12:40
memberMember 269292821-Mar-08 12:40 
GeneralThanks a million Pin
spinelesspsycho22-Nov-07 6:11
memberspinelesspsycho22-Nov-07 6:11 
Questionclearscreen func? Pin
Member #38925979-Mar-07 22:45
memberMember #38925979-Mar-07 22:45 
AnswerRe: clearscreen func? Pin
Jaded Hobo20-Mar-07 11:05
memberJaded Hobo20-Mar-07 11:05 
AnswerRe: clearscreen func? Pin
Dathletez1327-Aug-07 21:46
memberDathletez1327-Aug-07 21:46 
GeneralUsing Dev C++ Pin
naMretupmoC22-Feb-07 9:46
membernaMretupmoC22-Feb-07 9:46 
GeneralAdding different color to stdout and stderr Pin
sachin ahuja30-Jan-07 21:57
membersachin ahuja30-Jan-07 21:57 
AnswerRe: Adding different color to stdout and stderr Pin
Jaded Hobo4-Feb-07 6:15
memberJaded Hobo4-Feb-07 6:15 
GeneralWOW! Pin
JimmyDreak1-Dec-06 18:41
memberJimmyDreak1-Dec-06 18:41 
QuestionHow to get it? PURPURE! Pin
gravellpawepek15-Sep-06 2:12
membergravellpawepek15-Sep-06 2:12 

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

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

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