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