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