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