HIPO  4.3.0
High Performance Output data format for experimental physics
color.h
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_ASCII_COLOR_H_
13 #define INCLUDE_ASCII_COLOR_H_
14 
15 #include <iostream>
16 #include <ostream>
17 #include <sstream>
18 #include <string>
19 #include <type_traits>
20 #include <vector>
21 
25 namespace ascii {
28 enum class Color {
29  BLACK = 30,
30  RED = 31,
31  GREEN = 32,
32  YELLOW = 33,
33  BLUE = 34,
34  MAGENTA = 35,
35  CYAN = 36,
36  WHITE = 37,
37 
38  BRIGHT_BLACK = 90,
39  BRIGHT_RED = 91,
40  BRIGHT_GREEN = 92,
41  BRIGHT_YELLOW = 93,
42  BRIGHT_BLUE = 94,
43  BRIGHT_MAGENTA = 95,
44  BRIGHT_CYAN = 96,
45  BRIGHT_WHITE = 97,
46 
47  RESET = -1,
48 };
49 
55 class Color256 {
56 public:
61  Color256(int id) : id_(id) {}
62 
64  int id() { return id_; }
65 
66 private:
67  int id_;
68 };
69 
75 class RGB {
76 public:
83  RGB(int r, int g, int b) : r_(r), g_(g), b_(b) {}
84 
86  int r() { return r_; }
87 
89  int g() { return g_; }
90 
92  int b() { return b_; }
93 
94 private:
95  int r_;
96  int g_;
97  int b_;
98 };
99 
105 class Decoration {
106 public:
107  enum Attribute {
108  RESET = 0,
109  BOLD = 1,
110  DIM = 2,
111  ITALIC = 3,
115  REVERSED = 7,
116  CONCEAL = 8,
117  CROSSED = 9,
118 
119  NO_BOLD = 22,
120  NO_DIM = 22,
121  NO_ITALIC = 23,
123  NO_BLINK = 25,
126  NO_CROSSED = 29
127  };
128 
132  static Decoration From(Attribute attr) { return Decoration(attr); }
133 
138  friend std::ostream &operator<<(std::ostream &os, const Decoration &val) {
139  os << "\x1b[" << static_cast<int>(val.attr_) << "m";
140  return os;
141  }
142 
143 private:
144  Decoration(Attribute attr) : attr_(attr) {}
145  Attribute attr_;
146 };
147 
154 class Foreground {
155 public:
164  template <typename T> static Foreground From(T color) {
165  return Foreground(Resolve(color));
166  }
167 
172  friend std::ostream &operator<<(std::ostream &os, const Foreground &val) {
173  os << val.color_;
174  return os;
175  }
176 
177 private:
178  Foreground(std::string color) : color_(color) {}
179  std::string color_;
180 
181  static std::string Resolve(Color color) {
182  if (color == Color::RESET) {
183  return "\x1b[39m";
184  }
185  std::stringstream os;
186  os << "\x1b[" << static_cast<int>(color) << "m";
187  return os.str();
188  }
189 
190  static std::string Resolve(RGB color) {
191  std::stringstream os;
192  os << "\x1b[38;2;" << color.r() << ";" << color.g() << ";" << color.b()
193  << "m";
194  return os.str();
195  }
196 
197  static std::string Resolve(Color256 color) {
198  std::stringstream os;
199  os << "\x1b[38;5;" << color.id() << "m";
200  return os.str();
201  }
202 };
203 
210 class Background {
211 public:
220  template <typename T> static Background From(T color) {
221  return Background(Resolve(color));
222  }
223 
228  friend std::ostream &operator<<(std::ostream &os, const Background &val) {
229  os << val.color_;
230  return os;
231  }
232 
233 private:
234  Background(std::string color) : color_(color) {}
235  std::string color_;
236 
237  static std::string Resolve(Color color) {
238  if (color == Color::RESET) {
239  return "\x1b[49m";
240  }
241  std::stringstream os;
242  os << "\x1b[" << 10 + static_cast<int>(color) << "m";
243  return os.str();
244  }
245 
246  static std::string Resolve(RGB color) {
247  std::stringstream os;
248  os << "\x1b[48;2;" << color.r() << ";" << color.g() << ";" << color.b()
249  << "m";
250  return os.str();
251  }
252 
253  static std::string Resolve(Color256 color) {
254  std::stringstream os;
255  os << "\x1b[48;5;" << color.id() << "m";
256  return os.str();
257  }
258 };
259 } // namespace ascii
260 #endif
ANSI background color escape sequence generator.
Definition: color.h:210
friend std::ostream & operator<<(std::ostream &os, const Background &val)
Output the background color escape sequence to an output stream.
Definition: color.h:228
static Background From(T color)
Create a Background color from a color value.
Definition: color.h:220
256-color ANSI terminal color wrapper.
Definition: color.h:55
int id()
Definition: color.h:64
Color256(int id)
Construct a Color256 with the given palette ID.
Definition: color.h:61
ANSI text decoration (bold, italic, underline, etc.).
Definition: color.h:105
static Decoration From(Attribute attr)
Create a Decoration from an Attribute.
Definition: color.h:132
friend std::ostream & operator<<(std::ostream &os, const Decoration &val)
Output the decoration escape sequence to an output stream.
Definition: color.h:138
ANSI foreground color escape sequence generator.
Definition: color.h:154
static Foreground From(T color)
Create a Foreground color from a color value.
Definition: color.h:164
friend std::ostream & operator<<(std::ostream &os, const Foreground &val)
Output the foreground color escape sequence to an output stream.
Definition: color.h:172
24-bit true-color RGB terminal color.
Definition: color.h:75
int r()
Definition: color.h:86
RGB(int r, int g, int b)
Construct an RGB color from component values.
Definition: color.h:83
int g()
Definition: color.h:89
int b()
Definition: color.h:92
Color
Standard and bright ANSI terminal colors.
Definition: color.h:28
Definition: ascii.h:17