16using namespace std::literals;
19const std::array<const char*, 33> palette16code = {
41 return red_ == rhs.red_ && green_ == rhs.green_ && blue_ == rhs.blue_ &&
51 case ColorType::Palette1:
52 return is_background_color ?
"49"s :
"39"s;
54 case ColorType::Palette16:
55 return palette16code[2 * red_ + is_background_color];
57 case ColorType::Palette256:
58 return (is_background_color ?
"48;5;"s :
"38;5;"s) +
std::to_string(red_);
60 case ColorType::TrueColor:
62 return (is_background_color ?
"48;2;"s :
"38;2;"s)
97 type_ = ColorType::Palette16;
120 const int max_distance = 256 * 256 * 3;
121 int closest = max_distance;
123 const int database_begin = 16;
124 const int database_end = 256;
125 for (
int i = database_begin; i < database_end; ++i) {
127 const int dr = color_info.
red - red;
128 const int dg = color_info.
green - green;
129 const int db = color_info.
blue - blue;
130 const int dist = dr * dr + dg * dg + db * db;
131 if (closest > dist) {
138 type_ = ColorType::Palette256;
141 type_ = ColorType::Palette16;
155 return RGBA(red, green, blue, 255);
168 return {red, green, blue, alpha};
182 return {0, 0, 0, alpha};
185 uint8_t region = h / 43;
186 uint8_t remainder = (h - (region * 43)) * 6;
187 uint8_t p = (v * (255 - s)) >> 8;
188 uint8_t q = (v * (255 - ((s * remainder) >> 8))) >> 8;
189 uint8_t t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
193 case 0:
return Color(v,t,p, alpha);
194 case 1:
return Color(q,v,p, alpha);
195 case 2:
return Color(p,v,t, alpha);
196 case 3:
return Color(p,q,v, alpha);
197 case 4:
return Color(t,p,v, alpha);
198 case 5:
return Color(v,p,q, alpha);
201 return {0, 0, 0, alpha};
213 return HSVA(h, s, v, 255);
218 if (a.type_ == ColorType::Palette1 ||
219 b.type_ == ColorType::Palette1) {
228 uint8_t* red, uint8_t* green, uint8_t* blue) {
229 switch (
color.type_) {
230 case ColorType::Palette1: {
234 case ColorType::Palette16: {
242 case ColorType::Palette256: {
250 case ColorType::TrueColor:
253 *green =
color.green_;
266 get_color(a, &a_r, &a_g, &a_b);
267 get_color(b, &b_r, &b_g, &b_b);
271 auto interp = [t](uint8_t a_u, uint8_t b_u) {
272 constexpr float gamma = 2.2F;
273 const float a_f = powf(a_u, gamma);
274 const float b_f = powf(b_u, gamma);
275 const float c_f = a_f * (1.0F - t) +
277 return static_cast<uint8_t
>(powf(c_f, 1.F / gamma));
289 out.alpha_ = lhs.alpha_ + rhs.alpha_ - lhs.alpha_ * rhs.alpha_ / 255;
293inline namespace literals {
295Color operator""_rgb(
unsigned long long int combined) {
297 auto const red =
static_cast<uint8_t
>(combined >> 16U);
298 auto const green =
static_cast<uint8_t
>(combined >> 8U);
299 auto const blue =
static_cast<uint8_t
>(combined);
300 return {red, green, blue};
A class representing terminal colors.
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.