FTXUI 6.1.9
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#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
16namespace ftxui {
17
18/// @brief Color is a class that represents a color in the terminal user
19/// interface.
20///
21/// @ingroup screen
22class Color {
23 public:
24 enum Palette1 : uint8_t;
25 enum Palette16 : uint8_t;
26 enum Palette256 : uint8_t;
27
28 // NOLINTBEGIN
29 Color(); // Transparent.
30 Color(Palette1 index); // Transparent.
31 Color(Palette16 index); // Implicit conversion from index to Color.
32 Color(Palette256 index); // Implicit conversion from index to Color.
33 // NOLINTEND
34 Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
35 static Color RGB(uint8_t red, uint8_t green, uint8_t blue);
36 static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value);
37 static Color RGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
38 static Color HSVA(uint8_t hue,
39 uint8_t saturation,
40 uint8_t value,
41 uint8_t alpha);
42 static Color Interpolate(float t, const Color& a, const Color& b);
43 static Color Blend(const Color& lhs, const Color& rhs);
44
45 //---------------------------
46 // List of colors:
47 //---------------------------
48 // clang-format off
49 enum Palette1 : uint8_t{
50 Default, // Transparent
51 };
52
53 enum Palette16 : uint8_t {
54 Black = 0,
55 Red = 1,
56 Green = 2,
57 Yellow = 3,
58 Blue = 4,
60 Cyan = 6,
69 White = 15,
70 };
71
72 enum Palette256 : uint8_t {
76 Blue1 = 21,
77 Blue3 = 19,
89 Cornsilk1 = 230,
90 Cyan1 = 51,
91 Cyan2 = 50,
92 Cyan3 = 43,
97 DarkKhaki = 143,
144 Gold1 = 220,
145 Gold3 = 142,
146 Gold3Bis = 178,
147 Green1 = 46,
148 Green3 = 34,
150 Green4 = 28,
152 Grey0 = 16,
153 Grey100 = 231,
154 Grey11 = 234,
155 Grey15 = 235,
156 Grey19 = 236,
157 Grey23 = 237,
158 Grey27 = 238,
159 Grey3 = 232,
160 Grey30 = 239,
161 Grey35 = 240,
162 Grey37 = 59,
163 Grey39 = 241,
164 Grey42 = 242,
165 Grey46 = 243,
166 Grey50 = 244,
167 Grey53 = 102,
168 Grey54 = 245,
169 Grey58 = 246,
170 Grey62 = 247,
171 Grey63 = 139,
172 Grey66 = 248,
173 Grey69 = 145,
174 Grey7 = 233,
175 Grey70 = 249,
176 Grey74 = 250,
177 Grey78 = 251,
178 Grey82 = 252,
179 Grey84 = 188,
180 Grey85 = 253,
181 Grey89 = 254,
182 Grey93 = 255,
184 HotPink = 205,
185 HotPink2 = 169,
186 HotPink3 = 132,
193 Khaki1 = 228,
194 Khaki3 = 185,
221 Magenta1 = 201,
222 Magenta2 = 165,
224 Magenta3 = 127,
246 Orange1 = 214,
247 Orange3 = 172,
251 Orchid = 170,
252 Orchid1 = 213,
253 Orchid2 = 212,
261 Pink1 = 218,
262 Pink3 = 175,
263 Plum1 = 219,
264 Plum2 = 183,
265 Plum3 = 176,
266 Plum4 = 96,
267 Purple = 129,
272 Red1 = 196,
273 Red3 = 124,
274 Red3Bis = 160,
277 Salmon1 = 209,
283 SkyBlue1 = 117,
284 SkyBlue2 = 111,
299 Tan = 180,
300 Thistle1 = 225,
301 Thistle3 = 182,
304 Violet = 177,
305 Wheat1 = 229,
306 Wheat4 = 101,
307 Yellow1 = 226,
308 Yellow2 = 190,
309 Yellow3 = 148,
311 Yellow4 = 100,
313 };
314 // clang-format on
315
316 // --- Operators ------
317 bool operator==(const Color& rhs) const;
318 bool operator!=(const Color& rhs) const;
319
320 std::string Print(bool is_background_color) const;
321 bool IsOpaque() const { return alpha_ == 255; }
322
323 private:
324 enum class ColorType : uint8_t {
325 Palette1,
326 Palette16,
328 TrueColor,
329 };
330 ColorType type_ = ColorType::Palette1;
331 uint8_t red_ = 0;
332 uint8_t green_ = 0;
333 uint8_t blue_ = 0;
334 uint8_t alpha_ = 0;
335};
336
337inline namespace literals {
338
339/// @brief Creates a color from a combined hex RGB representation,
340/// e.g. 0x808000_rgb
341Color operator""_rgb(unsigned long long int combined);
342
343} // namespace literals
344
345} // namespace ftxui
346
347#endif // FTXUI_SCREEN_COLOR_HPP
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.
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.
static Color Blend(const Color &lhs, const Color &rhs)
Blend two colors together using the alpha channel.
bool operator!=(const Color &rhs) const
bool operator==(const Color &rhs) const
bool IsOpaque() const
Definition color.hpp:321
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.
@ LightGoldenrod2
Definition color.hpp:199
@ MediumOrchid1Bis
Definition color.hpp:229
@ DarkMagentaBis
Definition color.hpp:99
@ Chartreuse3Bis
Definition color.hpp:86
@ LightGoldenrod1
Definition color.hpp:198
@ MediumPurple3Bis
Definition color.hpp:236
@ LightGoldenrod3
Definition color.hpp:202
@ DarkSeaGreen4Bis
Definition color.hpp:119
@ LightSkyBlue3Bis
Definition color.hpp:214
@ DarkOliveGreen1
Definition color.hpp:100
@ Aquamarine1Bis
Definition color.hpp:74
@ DarkOliveGreen1Bis
Definition color.hpp:101
@ MediumPurple2Bis
Definition color.hpp:234
@ Chartreuse2Bis
Definition color.hpp:84
@ DarkSeaGreen1Bis
Definition color.hpp:113
@ MediumTurquoise
Definition color.hpp:239
@ DarkOliveGreen3Ter
Definition color.hpp:105
@ DarkSeaGreen3Bis
Definition color.hpp:117
@ LightSteelBlue1
Definition color.hpp:218
@ DeepSkyBlue3Bis
Definition color.hpp:137
@ MediumVioletRed
Definition color.hpp:240
@ LightGoldenrod2Ter
Definition color.hpp:201
@ DeepSkyBlue4Ter
Definition color.hpp:140
@ DarkOliveGreen2
Definition color.hpp:102
@ CornflowerBlue
Definition color.hpp:88
@ DarkGoldenrod
Definition color.hpp:95
@ LightGoldenrod2Bis
Definition color.hpp:200
@ DarkSeaGreen2Bis
Definition color.hpp:115
@ LightSalmon3Bis
Definition color.hpp:210
@ SpringGreen2Bis
Definition color.hpp:291
@ DeepSkyBlue4Bis
Definition color.hpp:139
@ DarkOliveGreen3Bis
Definition color.hpp:104
@ MediumSpringGreen
Definition color.hpp:238
@ DarkOliveGreen3
Definition color.hpp:103
@ SpringGreen3Bis
Definition color.hpp:293
@ LightSteelBlue3
Definition color.hpp:219
std::string Print(bool is_background_color) const
static Color Interpolate(float t, const Color &a, const Color &b)
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.
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
The FTXUI ftxui:: namespace.
Definition animation.hpp:10