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