18const std::array<const char*, 33> palette16code = {
37void AppendNumber(std::string& out, uint8_t n) {
39 out +=
static_cast<char>(
'0' + n / 100);
41 out +=
static_cast<char>(
'0' + n / 10);
43 out +=
static_cast<char>(
'0' + n);
45 out +=
static_cast<char>(
'0' + n / 10);
47 out +=
static_cast<char>(
'0' + n);
49 out +=
static_cast<char>(
'0' + n);
56 return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ &&
66 PrintTo(out, is_background_color);
75 case ColorType::Palette1:
76 out.append(is_background_color ?
"49" :
"39", 2);
78 case ColorType::Palette16:
79 out.append(palette16code[2 * red_ + (is_background_color ? 1 : 0)]);
81 case ColorType::Palette256:
82 out.append(is_background_color ?
"48;5;" :
"38;5;", 5);
83 AppendNumber(out, red_);
85 case ColorType::TrueColor:
86 out.append(is_background_color ?
"48;2;" :
"38;2;", 5);
87 AppendNumber(out, red_);
89 AppendNumber(out, green_);
91 AppendNumber(out, blue_);
104 : type_(ColorType::
Palette16), red_(index), alpha_(255) {}
108 : type_(ColorType::
Palette256), red_(index), alpha_(255) {
112 type_ = ColorType::Palette16;
124 : type_(ColorType::TrueColor),
134 const int max_distance = 256 * 256 * 3;
135 int closest = max_distance;
137 const int database_begin = 16;
138 const int database_end = 256;
139 for (
int i = database_begin; i < database_end; ++i) {
141 const int dr = color_info.
red - red;
142 const int dg = color_info.
green - green;
143 const int db = color_info.
blue - blue;
144 const int dist = dr * dr + dg * dg + db * db;
145 if (closest > dist) {
152 type_ = ColorType::Palette256;
155 type_ = ColorType::Palette16;
168 return RGBA(red, green, blue, 255);
180 return {red, green, blue, alpha};
192 uint8_t region = h / 43;
193 uint8_t remainder = (h - (region * 43)) * 6;
194 uint8_t p = (v * (255 - s)) >> 8;
195 uint8_t q = (v * (255 - ((s * remainder) >> 8))) >> 8;
196 uint8_t t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
200 case 0:
return Color(v,t,p, alpha);
201 case 1:
return Color(q,v,p, alpha);
202 case 2:
return Color(p,v,t, alpha);
203 case 3:
return Color(p,q,v, alpha);
204 case 4:
return Color(t,p,v, alpha);
205 case 5:
return Color(v,p,q, alpha);
208 return {0, 0, 0, alpha};
219 return HSVA(h, s, v, 255);
224 if (a.type_ == ColorType::Palette1 ||
225 b.type_ == ColorType::Palette1) {
234 [](
const Color&
color) -> std::tuple<uint8_t, uint8_t, uint8_t> {
235 switch (color.type_) {
236 case ColorType::Palette1: {
239 case ColorType::Palette16: {
243 case ColorType::Palette256: {
247 case ColorType::TrueColor:
249 return {color.red_, color.green_, color.blue_};
254 const auto [a_r, a_g, a_b] = to_rgb(a);
255 const auto [b_r, b_g, b_b] = to_rgb(b);
259 auto interp = [t](uint8_t a_u, uint8_t b_u) {
260 constexpr float gamma = 2.2F;
261 const float a_f = powf(a_u, gamma);
262 const float b_f = powf(b_u, gamma);
263 const float c_f = a_f * (1.0F - t) +
265 return static_cast<uint8_t
>(powf(c_f, 1.F / gamma));
276 out.alpha_ = lhs.alpha_ + rhs.alpha_ - lhs.alpha_ * rhs.alpha_ / 255;
282Color operator""_rgb(
unsigned long long int combined) {
284 auto const red =
static_cast<uint8_t
>(combined >> 16U);
285 auto const green =
static_cast<uint8_t
>(combined >> 8U);
286 auto const blue =
static_cast<uint8_t
>(combined);
287 return {red, green, blue};
Decorator color(Color)
Decorate using a foreground color.
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
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.
std::string Print(bool is_background_color) const
void PrintTo(std::string &out, bool is_background_color) const
Append the ANSI color code to a string (zero-allocation fast path).
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.
ColorInfo is a structure that contains information about the terminal color palette.
The FTXUI ftxui:: namespace.
ColorInfo GetColorInfo(Color::Palette256 index)
The FTXUI ftxui::literals:: namespace.