Click here to Skip to main content
Click here to Skip to main content

Add Color to Console Projects

, 14 Nov 2009
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

Comments and Discussions

 
GeneralMy vote of 5 PinmemberPranit Kothari21-May-12 18:25 
Questionhow to use as argument in a fonction PinmemberSublimeDeath9-Mar-11 13:50 
AnswerRe: how to use as argument in a fonction PinmemberJaded Hobo10-Mar-11 10:01 
GeneralRe: how to use as argument in a fonction PinmemberSublimeDeath11-Mar-11 16:50 
GeneralRe: how to use as argument in a fonction PinmemberSublimeDeath11-Mar-11 17:09 
GeneralRe: how to use as argument in a fonction PinmemberJaded Hobo12-Mar-11 9:00 
GeneralRe: how to use as argument in a fonction PinmemberSublimeDeath12-Mar-11 10:59 
QuestionEasy to write Tcl wrapper ? Pinmemberklausnrooster3-Jul-10 22:15 
AnswerRe: Easy to write Tcl wrapper ? PinmemberJaded Hobo4-Jul-10 10:04 
GeneralErrors with Dev-C++ PinmemberAdam Kadlec19-Nov-09 10:30 
GeneralRe: Errors with Dev-C++ PinmemberAdam Kadlec19-Nov-09 13:30 
GeneralRe: Errors with Dev-C++ PinmemberJaded Hobo25-Nov-09 9:00 
GeneralGood, but... Pinmemberelbertlev4-Nov-09 13:04 
GeneralRe: Good, but... PinmemberJaded Hobo11-Nov-09 8:25 
GeneralRe: Good, but... PinmemberJaded Hobo17-Nov-09 8:18 
GeneralThank you. PinmemberMember 269292821-Mar-08 12:40 
GeneralThanks a million Pinmemberspinelesspsycho22-Nov-07 6:11 
Questionclearscreen func? PinmemberMember #38925979-Mar-07 22:45 
AnswerRe: clearscreen func? PinmemberJaded Hobo20-Mar-07 11:05 
AnswerRe: clearscreen func? PinmemberDathletez1327-Aug-07 21:46 
GeneralUsing Dev C++ PinmembernaMretupmoC22-Feb-07 9:46 
GeneralAdding different color to stdout and stderr Pinmembersachin ahuja30-Jan-07 21:57 
Hi All
Well I have used the following code to add color to stdout and stderr.
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(INVALID_HANDLE_VALUE == hStdOut)
Error = ERR_HT_HANDLE;
GetConsoleScreenBufferInfo(hStdOut , &ConsoleScreenBufferInfoOutPut);

//Setting color for stdout
ConsoleScreenBufferInfoOutPut.wAttributes = FOREGROUND_GREEN;

SetConsoleTextAttribute(hStdOut ,ConsoleScreenBufferInfoOutPut.wAttributes);
It is working fine.
But when I used the same code with color red for stderr, I am getting the color for stdout as red though i have mentioned it as gree.
I have tried to use different structure for consolescreenbufferinfo but the problem is not solved.
Can anyone help in this regard.
AnswerRe: Adding different color to stdout and stderr PinmemberJaded Hobo4-Feb-07 6:15 
GeneralWOW! PinmemberJimmyDreak1-Dec-06 18:41 
QuestionHow to get it? PURPURE! Pinmembergravellpawepek15-Sep-06 2:12 
GeneralAmazing code, but... PinmemberNarook15-Jul-06 6:18 
GeneralError windows.h PinmemberMuchailpski16-Jun-06 11:16 
GeneralRe: Error windows.h PinmemberDa-Huntha25-Oct-07 13:30 
GeneralExcellent :) Pinmemberpeterxiau4-Jun-06 19:32 
GeneralGreat Job PinmemberMolerat24-May-06 7:36 
GeneralRe: Great Job PinmemberMolerat15-Jun-06 18:03 
GeneralAnother thing I noticed missing PinmemberSir Bastille30-Nov-05 6:13 
QuestionOther Colors? PinmemberdonaldGuy21-May-05 18:03 
AnswerRe: Other Colors? PinmemberJaded Hobo24-May-05 7:18 
GeneralAdding parameters Pinmembernicchap4-Apr-05 11:25 
GeneralRe: Adding parameters Pinmembernicchap5-Apr-05 16:05 
GeneralRe: Adding parameters [modified] PinmemberRadekHa17-Jul-08 21:37 
GeneralSmall correction needed PinmemberSkyhawk29-Dec-04 14:29 
GeneralRe: Small correction needed PinmemberJaded Hobo30-Dec-04 5:48 
GeneralGreat job PinmemberWayneC29-Dec-04 0:36 
Generalgood Pinmemberspetsnaz28-Dec-04 11:03 
GeneralInteresting! PinmemberWREY27-Dec-04 6:48 
GeneralRe: Interesting! Pinmemberzghelp20-Nov-05 17:16 
GeneralPerfect! PinmemberDandy Cheung26-Dec-04 1:29 

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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 14 Nov 2009
Article Copyright 2004 by Jaded Hobo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid