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