16 const 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:
55 case ColorType::TrueColor:
61 case ColorType::Palette1:
63 case ColorType::Palette16:
64 return palette16code[2 * red_];
65 case ColorType::Palette256:
67 case ColorType::TrueColor:
87 : type_(ColorType::
Palette16), red_(index), alpha_(255) {}
92 : type_(ColorType::
Palette256), red_(index), alpha_(255) {
96 type_ = ColorType::Palette16;
119 const int max_distance = 256 * 256 * 3;
120 int closest = max_distance;
122 const int database_begin = 16;
123 const int database_end = 256;
124 for (
int i = database_begin; i < database_end; ++i) {
126 const int dr = color_info.
red - red;
127 const int dg = color_info.
green - green;
128 const int db = color_info.
blue - blue;
129 const int dist = dr * dr + dg * dg + db * db;
130 if (closest > dist) {
137 type_ = ColorType::Palette256;
140 type_ = ColorType::Palette16;
154 return RGBA(red, green, blue, 255);
167 return {red, green, blue, alpha};
180 uint8_t region = h / 43;
181 uint8_t remainder = (h - (region * 43)) * 6;
182 uint8_t p = (v * (255 - s)) >> 8;
183 uint8_t q = (v * (255 - ((s * remainder) >> 8))) >> 8;
184 uint8_t t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
188 case 0:
return Color(v,t,p, alpha);
189 case 1:
return Color(q,v,p, alpha);
190 case 2:
return Color(p,v,t, alpha);
191 case 3:
return Color(p,q,v, alpha);
192 case 4:
return Color(t,p,v, alpha);
193 case 5:
return Color(v,p,q, alpha);
196 return {0, 0, 0, alpha};
208 return HSVA(h, s, v, 255);
213 if (a.type_ == ColorType::Palette1 ||
214 b.type_ == ColorType::Palette1) {
223 uint8_t* red, uint8_t* green, uint8_t* blue) {
224 switch (
color.type_) {
225 case ColorType::Palette1: {
229 case ColorType::Palette16: {
237 case ColorType::Palette256: {
245 case ColorType::TrueColor:
248 *green =
color.green_;
261 get_color(a, &a_r, &a_g, &a_b);
262 get_color(b, &b_r, &b_g, &b_b);
266 auto interp = [t](uint8_t a_u, uint8_t b_u) {
267 constexpr
float gamma = 2.2F;
268 const float a_f = powf(a_u, gamma);
269 const float b_f = powf(b_u, gamma);
270 const float c_f = a_f * (1.0F - t) +
272 return static_cast<uint8_t
>(powf(c_f, 1.F / gamma));
283 out.alpha_ = lhs.alpha_ + rhs.alpha_ - lhs.alpha_ * rhs.alpha_ / 255;
287 inline namespace literals {
289 Color operator""_rgb(
unsigned long long int combined) {
291 auto const red =
static_cast<uint8_t
>(combined >> 16U);
292 auto const green =
static_cast<uint8_t
>(combined >> 8U);
293 auto const blue =
static_cast<uint8_t
>(combined);
294 return {red, green, blue};
A class representing terminal colors.
enum Palette1 uint8_t enum Palette16 uint8_t enum Palette256 uint8_t 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 ColorSupport()
Get the color support of the terminal.
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
ColorInfo GetColorInfo(Color::Palette256 index)
Decorator color(Color)
Decorate using a foreground color.