FTXUI  0.11.1
C++ functional terminal UI.
color.cpp
Go to the documentation of this file.
1 #include "ftxui/screen/color.hpp"
2 
3 #include <cassert>
4 
5 #include "ftxui/screen/color_info.hpp" // for GetColorInfo, ColorInfo
6 #include "ftxui/screen/terminal.hpp" // for Terminal, Terminal::Color, Terminal::Palette256, Terminal::TrueColor
7 
8 namespace ftxui {
9 
10 namespace {
11 const char* palette16code[16][2] = {
12  {"30", "40"}, {"31", "41"}, {"32", "42"}, {"33", "43"},
13  {"34", "44"}, {"35", "45"}, {"36", "46"}, {"37", "47"},
14  {"90", "100"}, {"91", "101"}, {"92", "102"}, {"93", "103"},
15  {"94", "104"}, {"95", "105"}, {"96", "106"}, {"97", "107"},
16 };
17 }
18 
19 bool Color::operator==(const Color& rhs) const {
20  return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ &&
21  type_ == rhs.type_;
22 }
23 
24 bool Color::operator!=(const Color& rhs) const {
25  return !operator==(rhs);
26 }
27 
28 std::string Color::Print(bool is_background_color) const {
29  switch (type_) {
30  case ColorType::Palette1:
31  return is_background_color ? "49" : "39";
32 
33  case ColorType::Palette16:
34  return palette16code[index_][is_background_color];
35 
36  case ColorType::Palette256:
37  return (is_background_color ? "48;5;" : "38;5;") + std::to_string(index_);
38 
39  case ColorType::TrueColor:
40  return (is_background_color ? "48;2;" : "38;2;") //
41  + std::to_string(red_) + ";" //
42  + std::to_string(green_) + ";" //
43  + std::to_string(blue_); //
44  }
45  return "";
46 }
47 
48 /// @brief Build a transparent color.
49 /// @ingroup screen
50 Color::Color() : type_(ColorType::Palette1) {}
51 
52 /// @brief Build a transparent color.
53 /// @ingroup screen
54 Color::Color(Palette1) : type_(ColorType::Palette1) {}
55 
56 /// @brief Build a transparent using Palette16 colors.
57 /// @ingroup screen
58 Color::Color(Palette16 index) : type_(ColorType::Palette16), index_(index) {}
59 
60 /// @brief Build a transparent using Palette256 colors.
61 /// @ingroup screen
62 Color::Color(Palette256 index) : type_(ColorType::Palette256), index_(index) {
64  return;
65  type_ = ColorType::Palette16;
66  index_ = GetColorInfo(Color::Palette256(index_)).index_16;
67 }
68 
69 /// @brief Build a Color from its RGB representation.
70 /// https://en.wikipedia.org/wiki/RGB_color_model
71 ///
72 /// @param red The quantity of red [0,255]
73 /// @param green The quantity of green [0,255]
74 /// @param blue The quantity of blue [0,255]
75 /// @ingroup screen
76 Color::Color(uint8_t red, uint8_t green, uint8_t blue)
77  : type_(ColorType::TrueColor), red_(red), green_(green), blue_(blue) {
79  return;
80 
81  int closest = 256 * 256 * 3;
82  int best = 0;
83  for (int i = 16; i < 256; ++i) {
84  ColorInfo color_info = GetColorInfo(Color::Palette256(i));
85  int dr = color_info.red - red;
86  int dg = color_info.green - green;
87  int db = color_info.blue - blue;
88  int dist = dr * dr + dg * dg + db * db;
89  if (closest > dist) {
90  closest = dist;
91  best = i;
92  }
93  }
94 
96  type_ = ColorType::Palette256;
97  index_ = best;
98  } else {
99  type_ = ColorType::Palette16;
100  index_ = GetColorInfo(Color::Palette256(best)).index_16;
101  }
102 }
103 
104 /// @brief Build a Color from its RGB representation.
105 /// https://en.wikipedia.org/wiki/RGB_color_model
106 ///
107 /// @param red The quantity of red [0,255]
108 /// @param green The quantity of green [0,255]
109 /// @param blue The quantity of blue [0,255]
110 /// @ingroup screen
111 // static
112 Color Color::RGB(uint8_t red, uint8_t green, uint8_t blue) {
113  return Color(red, green, blue);
114 }
115 
116 /// @brief Build a Color from its HSV representation.
117 /// https://en.wikipedia.org/wiki/HSL_and_HSV
118 ///
119 /// @param h The hue of the color [0,255]
120 /// @param s The "colorfulness" [0,255].
121 /// @param v The "Lightness" [0,255]
122 /// @ingroup screen
123 // static
124 Color Color::HSV(uint8_t h, uint8_t s, uint8_t v) {
125  if (s == 0)
126  return Color(v, v, v);
127 
128  uint8_t region = h / 43;
129  uint8_t remainder = (h - (region * 43)) * 6;
130  uint8_t p = (v * (255 - s)) >> 8;
131  uint8_t q = (v * (255 - ((s * remainder) >> 8))) >> 8;
132  uint8_t t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
133 
134  // clang-format off
135  switch (region) {
136  case 0: return Color(v,t,p);
137  case 1: return Color(q,v,p);
138  case 2: return Color(p,v,t);
139  case 3: return Color(p,q,v);
140  case 4: return Color(t,p,v);
141  case 5: return Color(v,p,q);
142  }
143  // clang-format on
144 
145  return Color(0, 0, 0);
146 }
147 
148 inline namespace literals {
149 
150 Color operator""_rgb(unsigned long long int combined) {
151  assert(combined <= 0xffffffU);
152  auto const red = static_cast<uint8_t>(combined >> 16);
153  auto const green = static_cast<uint8_t>(combined >> 8);
154  auto const blue = static_cast<uint8_t>(combined);
155  return Color(red, green, blue);
156 }
157 
158 } // namespace literals
159 
160 } // namespace ftxui
161 
162 // Copyright 2020 Arthur Sonzogni. All rights reserved.
163 // Use of this source code is governed by the MIT license that can be found in
164 // the LICENSE file.
A class representing terminal colors.
Definition: color.hpp:17
enum Palette1 uint8_t enum Palette16 uint8_t enum Palette256 uint8_t Color()
Build a transparent color.
Definition: color.cpp:50
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:124
bool operator!=(const Color &rhs) const
Definition: color.cpp:24
bool operator==(const Color &rhs) const
Definition: color.cpp:19
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:112
std::string Print(bool is_background_color) const
Definition: color.cpp:28
Color ColorSupport()
Definition: terminal.cpp:98
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:389
ColorInfo GetColorInfo(Color::Palette256 index)
Definition: color_info.cpp:266