16const std::array<const char*, 33> palette16code = {
38 return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ &&
47 if (is_background_color) {
49 case ColorType::Palette1:
51 case ColorType::Palette16:
52 return palette16code[2 * red_ + 1];
53 case ColorType::Palette256:
54 return "48;5;" + std::to_string(red_);
55 case ColorType::TrueColor:
56 return "48;2;" + std::to_string(red_) +
";" + std::to_string(green_) +
57 ";" + std::to_string(blue_);
61 case ColorType::Palette1:
63 case ColorType::Palette16:
64 return palette16code[2 * red_];
65 case ColorType::Palette256:
66 return "38;5;" + std::to_string(red_);
67 case ColorType::TrueColor:
68 return "38;2;" + std::to_string(red_) +
";" + std::to_string(green_) +
69 ";" + std::to_string(blue_);
84 : type_(ColorType::
Palette16), red_(index), alpha_(255) {}
88 : type_(ColorType::
Palette256), red_(index), alpha_(255) {
92 type_ = ColorType::Palette16;
104 : type_(ColorType::TrueColor),
114 const int max_distance = 256 * 256 * 3;
115 int closest = max_distance;
117 const int database_begin = 16;
118 const int database_end = 256;
119 for (
int i = database_begin; i < database_end; ++i) {
121 const int dr = color_info.
red - red;
122 const int dg = color_info.
green - green;
123 const int db = color_info.
blue - blue;
124 const int dist = dr * dr + dg * dg + db * db;
125 if (closest > dist) {
132 type_ = ColorType::Palette256;
135 type_ = ColorType::Palette16;
148 return RGBA(red, green, blue, 255);
160 return {red, green, blue, alpha};
172 uint8_t region = h / 43;
173 uint8_t remainder = (h - (region * 43)) * 6;
174 uint8_t p = (v * (255 - s)) >> 8;
175 uint8_t q = (v * (255 - ((s * remainder) >> 8))) >> 8;
176 uint8_t t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
180 case 0:
return Color(v,t,p, alpha);
181 case 1:
return Color(q,v,p, alpha);
182 case 2:
return Color(p,v,t, alpha);
183 case 3:
return Color(p,q,v, alpha);
184 case 4:
return Color(t,p,v, alpha);
185 case 5:
return Color(v,p,q, alpha);
188 return {0, 0, 0, alpha};
199 return HSVA(h, s, v, 255);
204 if (a.type_ == ColorType::Palette1 ||
205 b.type_ == ColorType::Palette1) {
214 uint8_t* red, uint8_t* green, uint8_t* blue) {
215 switch (color.type_) {
216 case ColorType::Palette1: {
220 case ColorType::Palette16: {
228 case ColorType::Palette256: {
236 case ColorType::TrueColor:
239 *green = color.green_;
252 get_color(a, &a_r, &a_g, &a_b);
253 get_color(b, &b_r, &b_g, &b_b);
257 auto interp = [t](uint8_t a_u, uint8_t b_u) {
258 constexpr float gamma = 2.2F;
259 const float a_f = powf(a_u, gamma);
260 const float b_f = powf(b_u, gamma);
261 const float c_f = a_f * (1.0F - t) +
263 return static_cast<uint8_t
>(powf(c_f, 1.F / gamma));
274 out.alpha_ = lhs.alpha_ + rhs.alpha_ - lhs.alpha_ * rhs.alpha_ / 255;
278inline namespace literals {
280Color operator""_rgb(
unsigned long long int combined) {
282 auto const red =
static_cast<uint8_t
>(combined >> 16U);
283 auto const green =
static_cast<uint8_t
>(combined >> 8U);
284 auto const blue =
static_cast<uint8_t
>(combined);
285 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
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)