HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#ifndef INCLUDE_ASCII_COLOR_H_
2#define INCLUDE_ASCII_COLOR_H_
3
4#include <iostream>
5#include <ostream>
6#include <sstream>
7#include <string>
8#include <type_traits>
9#include <vector>
10
14namespace ascii {
15enum class Color {
16 BLACK = 30,
17 RED = 31,
18 GREEN = 32,
19 YELLOW = 33,
20 BLUE = 34,
21 MAGENTA = 35,
22 CYAN = 36,
23 WHITE = 37,
24
25 BRIGHT_BLACK = 90,
26 BRIGHT_RED = 91,
27 BRIGHT_GREEN = 92,
28 BRIGHT_YELLOW = 93,
29 BRIGHT_BLUE = 94,
30 BRIGHT_MAGENTA = 95,
31 BRIGHT_CYAN = 96,
32 BRIGHT_WHITE = 97,
33
34 RESET = -1,
35};
36
37class Color256 {
38public:
39 Color256(int id) : id_(id) {}
40 int id() { return id_; }
41
42private:
43 int id_;
44};
45
46class RGB {
47public:
48 RGB(int r, int g, int b) : r_(r), g_(g), b_(b) {}
49 int r() { return r_; }
50 int g() { return g_; }
51 int b() { return b_; }
52
53private:
54 int r_;
55 int g_;
56 int b_;
57};
58
60public:
82
83 static Decoration From(Attribute attr) { return Decoration(attr); }
84 friend std::ostream &operator<<(std::ostream &os, const Decoration &val) {
85 os << "\x1b[" << static_cast<int>(val.attr_) << "m";
86 return os;
87 }
88
89private:
90 Decoration(Attribute attr) : attr_(attr) {}
91 Attribute attr_;
92};
93
95public:
96 template <typename T> static Foreground From(T color) {
97 return Foreground(Resolve(color));
98 }
99
100 friend std::ostream &operator<<(std::ostream &os, const Foreground &val) {
101 os << val.color_;
102 return os;
103 }
104
105private:
106 Foreground(std::string color) : color_(color) {}
107 std::string color_;
108
109 static std::string Resolve(Color color) {
110 if (color == Color::RESET) {
111 return "\x1b[39m";
112 }
113 std::stringstream os;
114 os << "\x1b[" << static_cast<int>(color) << "m";
115 return os.str();
116 }
117
118 static std::string Resolve(RGB color) {
119 std::stringstream os;
120 os << "\x1b[38;2;" << color.r() << ";" << color.g() << ";" << color.b()
121 << "m";
122 return os.str();
123 }
124
125 static std::string Resolve(Color256 color) {
126 std::stringstream os;
127 os << "\x1b[38;5;" << color.id() << "m";
128 return os.str();
129 }
130};
131
133public:
134 template <typename T> static Background From(T color) {
135 return Background(Resolve(color));
136 }
137
138 friend std::ostream &operator<<(std::ostream &os, const Background &val) {
139 os << val.color_;
140 return os;
141 }
142
143private:
144 Background(std::string color) : color_(color) {}
145 std::string color_;
146
147 static std::string Resolve(Color color) {
148 if (color == Color::RESET) {
149 return "\x1b[49m";
150 }
151 std::stringstream os;
152 os << "\x1b[" << 10 + static_cast<int>(color) << "m";
153 return os.str();
154 }
155
156 static std::string Resolve(RGB color) {
157 std::stringstream os;
158 os << "\x1b[48;2;" << color.r() << ";" << color.g() << ";" << color.b()
159 << "m";
160 return os.str();
161 }
162
163 static std::string Resolve(Color256 color) {
164 std::stringstream os;
165 os << "\x1b[48;5;" << color.id() << "m";
166 return os.str();
167 }
168};
169} // namespace ascii
170#endif
Definition color.h:132
static Background From(T color)
Definition color.h:134
friend std::ostream & operator<<(std::ostream &os, const Background &val)
Definition color.h:138
Definition color.h:37
int id()
Definition color.h:40
Color256(int id)
Definition color.h:39
Definition color.h:59
static Decoration From(Attribute attr)
Definition color.h:83
Attribute
Definition color.h:61
@ REVERSED
Definition color.h:69
@ RAPIDBLINK
Definition color.h:68
@ CONCEAL
Definition color.h:70
@ RESET
Definition color.h:62
@ CROSSED
Definition color.h:71
@ SLOWBLINK
Definition color.h:67
@ NO_ITALIC
Definition color.h:75
@ ITALIC
Definition color.h:65
@ NO_DIM
Definition color.h:74
@ NO_CONCEAL
Definition color.h:79
@ BOLD
Definition color.h:63
@ NO_CROSSED
Definition color.h:80
@ NO_BOLD
Definition color.h:73
@ UNDERLINE
Definition color.h:66
@ NO_REVERSED
Definition color.h:78
@ NO_UNDERLINE
Definition color.h:76
@ NO_BLINK
Definition color.h:77
@ DIM
Definition color.h:64
friend std::ostream & operator<<(std::ostream &os, const Decoration &val)
Definition color.h:84
Definition color.h:94
static Foreground From(T color)
Definition color.h:96
friend std::ostream & operator<<(std::ostream &os, const Foreground &val)
Definition color.h:100
Definition color.h:46
int r()
Definition color.h:49
RGB(int r, int g, int b)
Definition color.h:48
int g()
Definition color.h:50
int b()
Definition color.h:51
for detailed info, refered to https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
Definition ascii.h:17
Color
Definition color.h:15