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