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
17namespace ftxui {
18
19/// @brief A class representing terminal colors.
20/// @ingroup screen
21class 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, uint8_t alpha = 255);
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 RGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
35 static Color HSVA(uint8_t hue,
36 uint8_t saturation,
37 uint8_t value,
38 uint8_t alpha);
39 static Color Interpolate(float t, const Color& a, const Color& b);
40 static Color Blend(const Color& lhs, const Color& rhs);
41
42 //---------------------------
43 // List of colors:
44 //---------------------------
45 // clang-format off
46 enum Palette1 : uint8_t{
47 Default, // Transparent
48 };
49
50 enum Palette16 : uint8_t {
51 Black = 0,
52 Red = 1,
53 Green = 2,
54 Yellow = 3,
55 Blue = 4,
57 Cyan = 6,
66 White = 15,
67 };
68
69 enum Palette256 : uint8_t {
73 Blue1 = 21,
74 Blue3 = 19,
86 Cornsilk1 = 230,
87 Cyan1 = 51,
88 Cyan2 = 50,
89 Cyan3 = 43,
94 DarkKhaki = 143,
141 Gold1 = 220,
142 Gold3 = 142,
143 Gold3Bis = 178,
144 Green1 = 46,
145 Green3 = 34,
147 Green4 = 28,
149 Grey0 = 16,
150 Grey100 = 231,
151 Grey11 = 234,
152 Grey15 = 235,
153 Grey19 = 236,
154 Grey23 = 237,
155 Grey27 = 238,
156 Grey3 = 232,
157 Grey30 = 239,
158 Grey35 = 240,
159 Grey37 = 59,
160 Grey39 = 241,
161 Grey42 = 242,
162 Grey46 = 243,
163 Grey50 = 244,
164 Grey53 = 102,
165 Grey54 = 245,
166 Grey58 = 246,
167 Grey62 = 247,
168 Grey63 = 139,
169 Grey66 = 248,
170 Grey69 = 145,
171 Grey7 = 233,
172 Grey70 = 249,
173 Grey74 = 250,
174 Grey78 = 251,
175 Grey82 = 252,
176 Grey84 = 188,
177 Grey85 = 253,
178 Grey89 = 254,
179 Grey93 = 255,
181 HotPink = 205,
182 HotPink2 = 169,
183 HotPink3 = 132,
190 Khaki1 = 228,
191 Khaki3 = 185,
218 Magenta1 = 201,
219 Magenta2 = 165,
221 Magenta3 = 127,
243 Orange1 = 214,
244 Orange3 = 172,
248 Orchid = 170,
249 Orchid1 = 213,
250 Orchid2 = 212,
258 Pink1 = 218,
259 Pink3 = 175,
260 Plum1 = 219,
261 Plum2 = 183,
262 Plum3 = 176,
263 Plum4 = 96,
264 Purple = 129,
269 Red1 = 196,
270 Red3 = 124,
271 Red3Bis = 160,
274 Salmon1 = 209,
280 SkyBlue1 = 117,
281 SkyBlue2 = 111,
296 Tan = 180,
297 Thistle1 = 225,
298 Thistle3 = 182,
301 Violet = 177,
302 Wheat1 = 229,
303 Wheat4 = 101,
304 Yellow1 = 226,
305 Yellow2 = 190,
306 Yellow3 = 148,
308 Yellow4 = 100,
310 };
311 // clang-format on
312
313 // --- Operators ------
314 bool operator==(const Color& rhs) const;
315 bool operator!=(const Color& rhs) const;
316
317 std::string Print(bool is_background_color) const;
318 bool IsOpaque() const { return alpha_ == 255; }
319
320 private:
321 enum class ColorType : uint8_t {
322 Palette1,
323 Palette16,
325 TrueColor,
326 };
327 ColorType type_ = ColorType::Palette1;
328 uint8_t red_ = 0;
329 uint8_t green_ = 0;
330 uint8_t blue_ = 0;
331 uint8_t alpha_ = 0;
332};
333
334inline namespace literals {
335
336/// @brief Creates a color from a combined hex RGB representation,
337/// e.g. 0x808000_rgb
338Color operator""_rgb(unsigned long long int combined);
339
340} // namespace literals
341
342} // namespace ftxui
343
344#endif // FTXUI_SCREEN_COLOR_HPP
A class representing terminal colors.
Definition: color.hpp:21
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:212
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:167
static Color Blend(const Color &lhs, const Color &rhs)
Blend two colors together using the alpha channel.
Definition: color.cpp:287
bool operator!=(const Color &rhs) const
Definition: color.cpp:45
bool operator==(const Color &rhs) const
Definition: color.cpp:40
bool IsOpaque() const
Definition: color.hpp:318
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:154
@ DarkSeaGreen3
Definition: color.hpp:113
@ LightGoldenrod2
Definition: color.hpp:196
@ MediumOrchid1Bis
Definition: color.hpp:226
@ PaleGreen3Bis
Definition: color.hpp:254
@ DarkSlateGray3
Definition: color.hpp:119
@ LightSlateGrey
Definition: color.hpp:213
@ DeepSkyBlue4
Definition: color.hpp:135
@ DarkMagentaBis
Definition: color.hpp:96
@ PaleGreen1Bis
Definition: color.hpp:252
@ MediumOrchid1
Definition: color.hpp:225
@ Chartreuse3Bis
Definition: color.hpp:83
@ LightGoldenrod1
Definition: color.hpp:195
@ LightSkyBlue3
Definition: color.hpp:210
@ DeepPink4Bis
Definition: color.hpp:129
@ DarkSlateGray2
Definition: color.hpp:118
@ DarkMagenta
Definition: color.hpp:95
@ PaleTurquoise1
Definition: color.hpp:255
@ MediumPurple3Bis
Definition: color.hpp:233
@ Chartreuse3
Definition: color.hpp:82
@ LightGoldenrod3
Definition: color.hpp:199
@ LightGreenBis
Definition: color.hpp:201
@ Chartreuse2
Definition: color.hpp:80
@ DarkSeaGreen
Definition: color.hpp:108
@ DarkSeaGreen4Bis
Definition: color.hpp:116
@ LightSkyBlue3Bis
Definition: color.hpp:211
@ DarkOrange3Bis
Definition: color.hpp:105
@ IndianRed1Bis
Definition: color.hpp:188
@ DarkOliveGreen1
Definition: color.hpp:97
@ Aquamarine1Bis
Definition: color.hpp:71
@ DarkOliveGreen1Bis
Definition: color.hpp:98
@ MediumPurple2Bis
Definition: color.hpp:231
@ LightSalmon1
Definition: color.hpp:205
@ SpringGreen3
Definition: color.hpp:289
@ MediumOrchid3
Definition: color.hpp:227
@ DeepSkyBlue1
Definition: color.hpp:131
@ LightYellow3
Definition: color.hpp:217
@ Chartreuse2Bis
Definition: color.hpp:81
@ DeepPink1Bis
Definition: color.hpp:124
@ DarkSeaGreen1Bis
Definition: color.hpp:110
@ DarkTurquoise
Definition: color.hpp:120
@ MediumPurple3
Definition: color.hpp:232
@ LightSeaGreen
Definition: color.hpp:208
@ Chartreuse4
Definition: color.hpp:84
@ SeaGreen1Bis
Definition: color.hpp:277
@ MediumTurquoise
Definition: color.hpp:236
@ NavajoWhite1
Definition: color.hpp:240
@ DarkOliveGreen3Ter
Definition: color.hpp:102
@ DarkSeaGreen3Bis
Definition: color.hpp:114
@ PaleVioletRed1
Definition: color.hpp:257
@ Chartreuse1
Definition: color.hpp:79
@ LightSteelBlue1
Definition: color.hpp:215
@ DeepSkyBlue3Bis
Definition: color.hpp:134
@ IndianRedBis
Definition: color.hpp:189
@ MediumVioletRed
Definition: color.hpp:237
@ SpringGreen1
Definition: color.hpp:286
@ DeepSkyBlue2
Definition: color.hpp:132
@ LightGoldenrod2Ter
Definition: color.hpp:198
@ DarkVioletBis
Definition: color.hpp:122
@ DeepSkyBlue4Ter
Definition: color.hpp:137
@ DeepSkyBlue3
Definition: color.hpp:133
@ MediumPurple
Definition: color.hpp:228
@ LightSalmon3
Definition: color.hpp:206
@ MediumOrchid
Definition: color.hpp:224
@ LightSkyBlue1
Definition: color.hpp:209
@ NavajoWhite3
Definition: color.hpp:241
@ DarkOliveGreen2
Definition: color.hpp:99
@ CornflowerBlue
Definition: color.hpp:85
@ DarkGoldenrod
Definition: color.hpp:92
@ LightSlateBlue
Definition: color.hpp:212
@ BlueViolet
Definition: color.hpp:76
@ LightGoldenrod2Bis
Definition: color.hpp:197
@ DarkSeaGreen4
Definition: color.hpp:115
@ DeepPink3Bis
Definition: color.hpp:127
@ MediumPurple2
Definition: color.hpp:230
@ DarkSeaGreen2Bis
Definition: color.hpp:112
@ SpringGreen2
Definition: color.hpp:287
@ DeepPink4Ter
Definition: color.hpp:130
@ LightCyan1Bis
Definition: color.hpp:193
@ LightSalmon3Bis
Definition: color.hpp:207
@ CadetBlueBis
Definition: color.hpp:78
@ SpringGreen2Bis
Definition: color.hpp:288
@ DeepSkyBlue4Bis
Definition: color.hpp:136
@ SlateBlue3Bis
Definition: color.hpp:285
@ DarkSeaGreen2
Definition: color.hpp:111
@ DarkOliveGreen3Bis
Definition: color.hpp:101
@ Aquamarine3
Definition: color.hpp:72
@ DarkSeaGreen1
Definition: color.hpp:109
@ LightSteelBlue
Definition: color.hpp:214
@ MediumSpringGreen
Definition: color.hpp:235
@ MediumPurple4
Definition: color.hpp:234
@ DarkOliveGreen3
Definition: color.hpp:100
@ SpringGreen3Bis
Definition: color.hpp:290
@ SpringGreen4
Definition: color.hpp:291
@ SteelBlue1Bis
Definition: color.hpp:294
@ Aquamarine1
Definition: color.hpp:70
@ MediumPurple1
Definition: color.hpp:229
@ LightSteelBlue3
Definition: color.hpp:216
@ DarkSlateGray1
Definition: color.hpp:117
@ PaleTurquoise4
Definition: color.hpp:256
std::string Print(bool is_background_color) const
Definition: color.cpp:49
@ YellowLight
Definition: color.hpp:62
@ MagentaLight
Definition: color.hpp:64
@ GreenLight
Definition: color.hpp:61
static Color Interpolate(float t, const Color &a, const Color &b)
Definition: color.cpp:217
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:180