QwAnalysis
QwColor.h
Go to the documentation of this file.
1 #ifndef QWCOLOR_H
2 #define QWCOLOR_H
3 
4 // System headers
5 #include <map>
6 #include <cstdio>
7 #include <iostream>
8 
9 // This header contains the terminals color codes.
10 // This might be useful to distingush many output of QwAnalysis into
11 // terminals.
12 
13 // Simply use printf function
14 // printf("%s Bold text print %s\n", BOLD, NORMAL);
15 // printf("%s RED text %s\n", RED, NORMAL);
16 
17 // detailed information
18 // see http://www.faqs.org/docs/abs/HTML/colorizing.html
19 // http://linuxgazette.net/issue65/padala.html
20 
21 // Also see a listing of the full ANSI escape sequences at:
22 // http://en.wikipedia.org/wiki/ANSI_escape_code
23 
24 // Note that you should replace "\E" with "\033" as the
25 // escape character because only "\033" conforms to the ISO
26 // standard, while "\E" is a GNU extension.
27 
28 #define BLACK "\033[30m"
29 #define RED "\033[31m"
30 #define GREEN "\033[32m"
31 #define BROWN "\033[33m"
32 #define BLUE "\033[34m"
33 #define MAGENTA "\033[35m"
34 #define CYAN "\033[36m"
35 #define WHITE "\033[37m"
36 
37 #define BOLD "\033[1m"
38 /* Code "\033[2m" sets the intensity to faint, but it is not
39  * universally supported.
40  * Code "\033[22m" sets the intensity to normal (not bold or faint).
41  */
42 
43 /* The bolded codes below could alternately be achieved by
44  * two codes; i.e. BOLDRED could be done by the concatenation
45  * of BOLD and RED.
46  */
47 #define BOLDRED "\033[31;1m"
48 #define BOLDGREEN "\033[32;1m"
49 #define BOLDBROWN "\033[33;1m"
50 #define BOLDBLUE "\033[34;1m"
51 #define BOLDMAGENTA "\033[35;1m"
52 #define BOLDCYAN "\033[36;1m"
53 #define BOLDWHITE "\033[37;1m"
54 
55 #define BACKRED "\033[41m"
56 #define BACKGREEN "\033[42m"
57 #define BACKBLUE "\033[44m"
58 
59 /* Code "\033[0m" clears all formatting codes.
60  */
61 #define NORMAL "\033[0m"
62 
63 // How to use the Color Code with printf
64 // printf("%sName%s", BOLD, NORMAL);
65 // printf("[%s%+4.2e%s +- %s%+4.2e%s]", RED, out[0], NORMAL, BLUE, out[1], NORMAL);
66 
67 
68 // All colors need to go in a namespace because of conflicts with ROOT colors.
69 // Really, ROOT should have its colors in a namespace.
70 namespace Qw {
71 
72  enum EQwColor {
78  };
79 
80 } // namespace Qw
81 
82 // Map of the enum to the defined strings
83 typedef std::map<Qw::EQwColor, std::string> QwColorMap;
84 
85 /**
86  * \class QwColor
87  * \ingroup QwAnalysis
88  * \brief A color changing class for the output stream
89  *
90  * This class changes the color in the output stream by inserting 'magic'
91  * escape sequences that are interpreted by smart terminals. On dumb terminals
92  * this will probably just print garbage. To use the color, a QwColor object
93  * should be streamed to the output stream:
94  * \code
95  * QwMessage << "Hello, " << QwColor(Qw::kRed) << "Qweak!" << QwLog::endl;
96  * \endcode
97  */
98 class QwColor
99 {
100  public:
101 
102  /// Default constructor
104  : foreground(f), background(b) { };
105  virtual ~QwColor() { };
106 
107  /// \brief Output stream operator
108  friend std::ostream& operator<<(std::ostream& out, const QwColor& color);
109 
110  protected:
111 
113  QwColorMap map;
114  map[Qw::kBlack] = BLACK;
115  map[Qw::kRed] = RED;
116  map[Qw::kGreen] = GREEN;
117  map[Qw::kBrown] = BROWN;
118  map[Qw::kBlue] = BLUE;
119  map[Qw::kMagenta] = MAGENTA;
120  map[Qw::kCyan] = CYAN;
121  map[Qw::kWhite] = WHITE;
122  //
123  map[Qw::kBold] = BOLD;
124  //
125  map[Qw::kBoldRed] = BOLDRED;
126  map[Qw::kBoldGreen] = BOLDGREEN;
127  map[Qw::kBoldBrown] = BOLDBROWN;
128  map[Qw::kBoldBlue] = BOLDBLUE;
130  map[Qw::kBoldCyan] = BOLDCYAN;
131  map[Qw::kBoldWhite] = BOLDWHITE;
132  //
133  map[Qw::kBackRed] = BACKRED;
134  map[Qw::kBackGreen] = BACKGREEN;
135  map[Qw::kBackBlue] = BACKBLUE;
136  //
137  map[Qw::kDefaultForeground] = "\033[39m";
138  map[Qw::kDefaultBackground] = "\033[49m";
139  //
140  map[Qw::kNormal] = NORMAL;
141  return map;
142  };
143  static QwColorMap kColorMap;
144 
145  private:
146 
147  Qw::EQwColor foreground; ///< Foreground color
148  Qw::EQwColor background; ///< Background color (not yet supported)
149 
150 }; // class QwColor
151 
152 /// Output stream operator which uses the enum-to-escape-code mapping
153 inline std::ostream& operator<<(std::ostream& out, const QwColor& color)
154 {
155  return out << color.kColorMap[color.foreground];
156 }
157 
158 #endif // QWCOLOR_H
#define WHITE
Definition: QwColor.h:35
Qw::EQwColor foreground
Foreground color.
Definition: QwColor.h:147
#define BLACK
Definition: QwColor.h:28
std::ostream & operator<<(std::ostream &out, const QwColor &color)
Output stream operator which uses the enum-to-escape-code mapping.
Definition: QwColor.h:153
#define BOLDGREEN
Definition: QwColor.h:48
#define BOLD
Definition: QwColor.h:37
#define BOLDCYAN
Definition: QwColor.h:52
virtual ~QwColor()
Definition: QwColor.h:105
#define CYAN
Definition: QwColor.h:34
EQwColor
Definition: QwColor.h:72
#define BACKRED
Definition: QwColor.h:55
Qw::EQwColor background
Background color (not yet supported)
Definition: QwColor.h:148
#define BACKBLUE
Definition: QwColor.h:57
#define BOLDBROWN
Definition: QwColor.h:49
static QwColorMap kColorMap
Definition: QwColor.h:142
#define GREEN
Definition: QwColor.h:30
static QwColorMap CreateColorMap()
Definition: QwColor.h:112
#define RED
Definition: QwColor.h:29
#define BOLDMAGENTA
Definition: QwColor.h:51
A color changing class for the output stream.
Definition: QwColor.h:98
#define BLUE
Definition: QwColor.h:32
#define BOLDBLUE
Definition: QwColor.h:50
#define BOLDRED
Definition: QwColor.h:47
#define NORMAL
Definition: QwColor.h:61
#define MAGENTA
Definition: QwColor.h:33
QwColor(const Qw::EQwColor f=Qw::kDefaultForeground, const Qw::EQwColor b=Qw::kDefaultBackground)
Default constructor.
Definition: QwColor.h:103
#define BOLDWHITE
Definition: QwColor.h:53
#define BACKGREEN
Definition: QwColor.h:56
#define BROWN
Definition: QwColor.h:31
friend std::ostream & operator<<(std::ostream &out, const QwColor &color)
Output stream operator.
Definition: QwColor.h:153
std::map< Qw::EQwColor, std::string > QwColorMap
Definition: QwColor.h:83