Storing colour codes in enum/string ??

Hey all,

I want to change the colour of text outputting to the screen, but dont want to constantly use the codes to keep switching colour.

So instead of having
 
cout << "Default colour " << "\033[22;31m" << "this is the colour red" << endl;


I could just write something like
 
cout << "Default colour " << RED << "this is the colour red" << endl;


Was thinking of making use of enum, but wasn't sure how to go about this...
Any ideas???
I'm guessing by the colour code that this is *nix and not Windows, correct?

I would be tempted to make use of the define preprocessor directive in this scenario. You could use an enum, yes, but then I think you'd probably have to overload the ofstream << operator to allow compilation.

Easiest way is defines.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

#ifndef CC_RED
#define CC_RED  "\033[22;31m"       // Define console colour red
#endif

int main(int argc, char* argv[])
{
   cout << "Default colour\t" << CC_RED << "This is red\n";
   return 0;
}


Note that in *nix, or at least in the implementation I'm using (HP-UX through PuTTY), this set will set everything outside the program, i.e. - anything you enter in the command line and anything printed in the output buffer, to the colour you've specified. You might what to output the default colour before quitting the program.

Hope that helps.
Last edited on
Topic archived. No new replies allowed.