FTXUI  0.11.1
C++ functional terminal UI.
color.hpp
Go to the documentation of this file.
1 #ifndef FTXUI_SCREEN_COLOR
2 #define FTXUI_SCREEN_COLOR
3 
4 #include <stdint.h> // for uint8_t
5 #include <string> // for wstring
6 
7 #ifdef RGB
8 // Workaround for wingdi.h (via Windows.h) defining macros that break things.
9 // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-rgb
10 #undef RGB
11 #endif
12 
13 namespace ftxui {
14 
15 /// @brief A class representing terminal colors.
16 /// @ingroup screen
17 class Color {
18  public:
19  enum Palette1 : uint8_t;
20  enum Palette16 : uint8_t;
21  enum Palette256 : uint8_t;
22 
23  Color(); // Transparent.
24  Color(Palette1 index); // Transparent.
25  Color(Palette16 index); // Implicit conversion from index to Color.
26  Color(Palette256 index); // Implicit conversion from index to Color.
27  Color(uint8_t red, uint8_t green, uint8_t blue);
28  static Color RGB(uint8_t red, uint8_t green, uint8_t blue);
29  static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value);
30 
31  //---------------------------
32  // List of colors:
33  //---------------------------
34  // clang-format off
35  enum Palette1 : uint8_t{
36  Default, // Transparent
37  };
38 
39  enum Palette16 : uint8_t {
40  Black = 0,
41  Red = 1,
42  Green = 2,
43  Yellow = 3,
44  Blue = 4,
45  Magenta = 5,
46  Cyan = 6,
47  GrayLight = 7,
48  GrayDark = 8,
49  RedLight = 9,
50  GreenLight = 10,
52  BlueLight = 12,
54  CyanLight = 14,
55  White = 15,
56  };
57 
58  enum Palette256 : uint8_t {
59  Aquamarine1 = 122,
62  Blue1 = 21,
63  Blue3 = 19,
64  Blue3Bis = 20,
65  BlueViolet = 57,
66  CadetBlue = 72,
68  Chartreuse1 = 118,
69  Chartreuse2 = 112,
75  Cornsilk1 = 230,
76  Cyan1 = 51,
77  Cyan2 = 50,
78  Cyan3 = 43,
79  DarkBlue = 18,
80  DarkCyan = 36,
82  DarkGreen = 22,
83  DarkKhaki = 143,
92  DarkOrange = 208,
93  DarkOrange3 = 130,
95  DarkRed = 52,
96  DarkRedBis = 88,
97  DarkSeaGreen = 108,
110  DarkViolet = 128,
112  DeepPink1 = 198,
114  DeepPink2 = 197,
115  DeepPink3 = 161,
117  DeepPink4 = 125,
130  Gold1 = 220,
131  Gold3 = 142,
132  Gold3Bis = 178,
133  Green1 = 46,
134  Green3 = 34,
135  Green3Bis = 40,
136  Green4 = 28,
137  GreenYellow = 154,
138  Grey0 = 16,
139  Grey100 = 231,
140  Grey11 = 234,
141  Grey15 = 235,
142  Grey19 = 236,
143  Grey23 = 237,
144  Grey27 = 238,
145  Grey3 = 232,
146  Grey30 = 239,
147  Grey35 = 240,
148  Grey37 = 59,
149  Grey39 = 241,
150  Grey42 = 242,
151  Grey46 = 243,
152  Grey50 = 244,
153  Grey53 = 102,
154  Grey54 = 245,
155  Grey58 = 246,
156  Grey62 = 247,
157  Grey63 = 139,
158  Grey66 = 248,
159  Grey69 = 145,
160  Grey7 = 233,
161  Grey70 = 249,
162  Grey74 = 250,
163  Grey78 = 251,
164  Grey82 = 252,
165  Grey84 = 188,
166  Grey85 = 253,
167  Grey89 = 254,
168  Grey93 = 255,
169  Honeydew2 = 194,
170  HotPink = 205,
171  HotPink2 = 169,
172  HotPink3 = 132,
173  HotPink3Bis = 168,
174  HotPinkBis = 206,
175  IndianRed = 131,
176  IndianRed1 = 203,
179  Khaki1 = 228,
180  Khaki3 = 185,
181  LightCoral = 210,
183  LightCyan3 = 152,
189  LightGreen = 119,
191  LightPink1 = 217,
192  LightPink3 = 174,
207  Magenta1 = 201,
208  Magenta2 = 165,
209  Magenta2Bis = 200,
210  Magenta3 = 127,
211  Magenta3Bis = 163,
212  Magenta3Ter = 164,
227  MistyRose1 = 224,
228  MistyRose3 = 181,
231  NavyBlue = 17,
232  Orange1 = 214,
233  Orange3 = 172,
234  Orange4 = 58,
236  OrangeRed1 = 202,
237  Orchid = 170,
238  Orchid1 = 213,
239  Orchid2 = 212,
240  PaleGreen1 = 121,
242  PaleGreen3 = 114,
247  Pink1 = 218,
248  Pink3 = 175,
249  Plum1 = 219,
250  Plum2 = 183,
251  Plum3 = 176,
252  Plum4 = 96,
253  Purple = 129,
254  Purple3 = 56,
255  Purple4 = 54,
257  PurpleBis = 93,
258  Red1 = 196,
259  Red3 = 124,
260  Red3Bis = 160,
261  RosyBrown = 138,
263  Salmon1 = 209,
264  SandyBrown = 215,
265  SeaGreen1 = 84,
267  SeaGreen2 = 83,
268  SeaGreen3 = 78,
269  SkyBlue1 = 117,
270  SkyBlue2 = 111,
271  SkyBlue3 = 74,
281  SteelBlue = 67,
285  Tan = 180,
286  Thistle1 = 225,
287  Thistle3 = 182,
290  Violet = 177,
291  Wheat1 = 229,
292  Wheat4 = 101,
293  Yellow1 = 226,
294  Yellow2 = 190,
295  Yellow3 = 148,
296  Yellow3Bis = 184,
297  Yellow4 = 100,
298  Yellow4Bis = 106,
299  };
300  // clang-format on
301 
302  // --- Operators ------
303  bool operator==(const Color& rhs) const;
304  bool operator!=(const Color& rhs) const;
305 
306  std::string Print(bool is_background_color) const;
307 
308  private:
309  enum class ColorType : uint8_t {
310  Palette1,
311  Palette16,
312  Palette256,
313  TrueColor,
314  };
315 
316  ColorType type_;
317  union {
318  uint8_t index_ = 0;
319  uint8_t red_;
320  };
321  uint8_t green_ = 0;
322  uint8_t blue_ = 0;
323 };
324 
325 inline namespace literals {
326 
327 /// @brief Creates a color from a combined hex RGB representation,
328 /// e.g. 0x808000_rgb
329 Color operator""_rgb(unsigned long long int combined);
330 
331 } // namespace literals
332 
333 } // namespace ftxui
334 
335 #endif /* end of include guard: FTXUI_COLOR_H_ */
336 
337 // Copyright 2020 Arthur Sonzogni. All rights reserved.
338 // Use of this source code is governed by the MIT license that can be found in
339 // the LICENSE file.
A class representing terminal colors.
Definition: color.hpp:17
enum Palette1 uint8_t enum Palette16 uint8_t enum Palette256 uint8_t Color()
Build a transparent color.
Definition: color.cpp:50
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
Build a Color from its HSV representation. https://en.wikipedia.org/wiki/HSL_and_HSV.
Definition: color.cpp:124
bool operator!=(const Color &rhs) const
Definition: color.cpp:24
bool operator==(const Color &rhs) const
Definition: color.cpp:19
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
Build a Color from its RGB representation. https://en.wikipedia.org/wiki/RGB_color_model.
Definition: color.cpp:112
@ DarkSeaGreen3
Definition: color.hpp:102
@ LightGoldenrod2
Definition: color.hpp:185
@ MediumOrchid1Bis
Definition: color.hpp:215
@ PaleGreen3Bis
Definition: color.hpp:243
@ DarkSlateGray3
Definition: color.hpp:108
@ LightSlateGrey
Definition: color.hpp:202
@ DarkOrange3
Definition: color.hpp:93
@ DeepSkyBlue4
Definition: color.hpp:124
@ DarkMagentaBis
Definition: color.hpp:85
@ PaleGreen1Bis
Definition: color.hpp:241
@ MediumOrchid1
Definition: color.hpp:214
@ Chartreuse3Bis
Definition: color.hpp:72
@ DarkOrange
Definition: color.hpp:92
@ LightGoldenrod1
Definition: color.hpp:184
@ LightSkyBlue3
Definition: color.hpp:199
@ DeepPink4Bis
Definition: color.hpp:118
@ DarkSlateGray2
Definition: color.hpp:107
@ DarkMagenta
Definition: color.hpp:84
@ PaleTurquoise1
Definition: color.hpp:244
@ MediumPurple3Bis
Definition: color.hpp:222
@ Chartreuse3
Definition: color.hpp:71
@ LightGoldenrod3
Definition: color.hpp:188
@ LightGreenBis
Definition: color.hpp:190
@ Chartreuse2
Definition: color.hpp:69
@ DarkSeaGreen
Definition: color.hpp:97
@ DarkSeaGreen4Bis
Definition: color.hpp:105
@ LightSkyBlue3Bis
Definition: color.hpp:200
@ DarkOrange3Bis
Definition: color.hpp:94
@ IndianRed1Bis
Definition: color.hpp:177
@ DarkOliveGreen1
Definition: color.hpp:86
@ Aquamarine1Bis
Definition: color.hpp:60
@ DarkOliveGreen1Bis
Definition: color.hpp:87
@ MediumPurple2Bis
Definition: color.hpp:220
@ LightSalmon1
Definition: color.hpp:194
@ SpringGreen3
Definition: color.hpp:278
@ MediumOrchid3
Definition: color.hpp:216
@ DeepSkyBlue1
Definition: color.hpp:120
@ LightYellow3
Definition: color.hpp:206
@ Chartreuse2Bis
Definition: color.hpp:70
@ DeepPink1Bis
Definition: color.hpp:113
@ DarkSeaGreen1Bis
Definition: color.hpp:99
@ DarkTurquoise
Definition: color.hpp:109
@ MediumPurple3
Definition: color.hpp:221
@ LightSeaGreen
Definition: color.hpp:197
@ Chartreuse4
Definition: color.hpp:73
@ SeaGreen1Bis
Definition: color.hpp:266
@ MediumTurquoise
Definition: color.hpp:225
@ NavajoWhite1
Definition: color.hpp:229
@ DarkOliveGreen3Ter
Definition: color.hpp:91
@ DarkSeaGreen3Bis
Definition: color.hpp:103
@ PaleVioletRed1
Definition: color.hpp:246
@ Chartreuse1
Definition: color.hpp:68
@ LightSteelBlue1
Definition: color.hpp:204
@ DeepSkyBlue3Bis
Definition: color.hpp:123
@ IndianRedBis
Definition: color.hpp:178
@ MediumVioletRed
Definition: color.hpp:226
@ SpringGreen1
Definition: color.hpp:275
@ DeepSkyBlue2
Definition: color.hpp:121
@ LightGoldenrod2Ter
Definition: color.hpp:187
@ DarkVioletBis
Definition: color.hpp:111
@ DeepSkyBlue4Ter
Definition: color.hpp:126
@ DeepSkyBlue3
Definition: color.hpp:122
@ MediumPurple
Definition: color.hpp:217
@ LightSalmon3
Definition: color.hpp:195
@ MediumOrchid
Definition: color.hpp:213
@ LightSkyBlue1
Definition: color.hpp:198
@ NavajoWhite3
Definition: color.hpp:230
@ DarkRedBis
Definition: color.hpp:96
@ DarkOliveGreen2
Definition: color.hpp:88
@ CornflowerBlue
Definition: color.hpp:74
@ DarkGoldenrod
Definition: color.hpp:81
@ LightSlateBlue
Definition: color.hpp:201
@ BlueViolet
Definition: color.hpp:65
@ LightGoldenrod2Bis
Definition: color.hpp:186
@ DarkSeaGreen4
Definition: color.hpp:104
@ DeepPink3Bis
Definition: color.hpp:116
@ MediumPurple2
Definition: color.hpp:219
@ DarkSeaGreen2Bis
Definition: color.hpp:101
@ SpringGreen2
Definition: color.hpp:276
@ DeepPink4Ter
Definition: color.hpp:119
@ LightCyan1Bis
Definition: color.hpp:182
@ LightSalmon3Bis
Definition: color.hpp:196
@ CadetBlueBis
Definition: color.hpp:67
@ SpringGreen2Bis
Definition: color.hpp:277
@ DeepSkyBlue4Bis
Definition: color.hpp:125
@ SlateBlue3Bis
Definition: color.hpp:274
@ DarkSeaGreen2
Definition: color.hpp:100
@ DarkOliveGreen3Bis
Definition: color.hpp:90
@ Aquamarine3
Definition: color.hpp:61
@ DarkSeaGreen1
Definition: color.hpp:98
@ LightSteelBlue
Definition: color.hpp:203
@ MediumSpringGreen
Definition: color.hpp:224
@ MediumPurple4
Definition: color.hpp:223
@ DarkOliveGreen3
Definition: color.hpp:89
@ SpringGreen3Bis
Definition: color.hpp:279
@ SpringGreen4
Definition: color.hpp:280
@ SteelBlue1Bis
Definition: color.hpp:283
@ Aquamarine1
Definition: color.hpp:59
@ MediumPurple1
Definition: color.hpp:218
@ LightSteelBlue3
Definition: color.hpp:205
@ DarkSlateGray1
Definition: color.hpp:106
@ PaleTurquoise4
Definition: color.hpp:245
std::string Print(bool is_background_color) const
Definition: color.cpp:28
@ YellowLight
Definition: color.hpp:51
@ MagentaLight
Definition: color.hpp:53
@ GreenLight
Definition: color.hpp:50