25int rgb_to_ansi256(std::uint8_t r, std::uint8_t g, std::uint8_t b)
28 if (r == g && g == b) {
35 return static_cast<int>(std::lround((r - 8) / 247.0 * 24)) + 232;
40 auto to_6 = [](
const std::uint8_t val) ->
int {
47 return (val - 35) / 40;
50 const int r_idx = to_6(r);
51 const int g_idx = to_6(g);
52 const int b_idx = to_6(b);
54 return 16 + 36 * r_idx + 6 * g_idx + b_idx;
70PredefinedColor find_closest_plain_color(std::uint8_t r, std::uint8_t g, std::uint8_t b)
73 const std::array<std::pair<PredefinedColor, std::array<int, 3>>, 8> plain_colors = {{
74 {PredefinedColor::black, {0, 0, 0}},
75 {PredefinedColor::red, {255, 0, 0}},
76 {PredefinedColor::green, {0, 255, 0}},
77 {PredefinedColor::yellow, {255, 255, 0}},
78 {PredefinedColor::blue, {0, 0, 255}},
79 {PredefinedColor::magenta, {255, 0, 255}},
80 {PredefinedColor::cyan, {0, 255, 255}},
81 {PredefinedColor::white, {255, 255, 255}},
85 double min_distance = std::numeric_limits<double>::max();
87 for (
const auto &[color, rgb] : plain_colors) {
88 const int dr = r - rgb[0];
89 const int dg = g - rgb[1];
90 const int db = b - rgb[2];
93 const double distance = 2.0 * dr * dr + 4.0 * dg * dg + 3.0 * db * db;
95 if (distance < min_distance) {
96 min_distance = distance;
105const std::string ansi_reset =
"\033[0m";
108const std::array<std::pair<PredefinedColor, std::string>, 8> fg_color_mappings = {{
109 {PredefinedColor::black,
"\033[30m"},
110 {PredefinedColor::red,
"\033[31m"},
111 {PredefinedColor::green,
"\033[32m"},
112 {PredefinedColor::yellow,
"\033[33m"},
113 {PredefinedColor::blue,
"\033[34m"},
114 {PredefinedColor::magenta,
"\033[35m"},
115 {PredefinedColor::cyan,
"\033[36m"},
116 {PredefinedColor::white,
"\033[37m"},
120const std::array<std::pair<PredefinedColor, std::string>, 8> bg_color_mappings = {{
121 {PredefinedColor::black,
"\033[40m"},
122 {PredefinedColor::red,
"\033[41m"},
123 {PredefinedColor::green,
"\033[42m"},
124 {PredefinedColor::yellow,
"\033[43m"},
125 {PredefinedColor::blue,
"\033[44m"},
126 {PredefinedColor::magenta,
"\033[45m"},
127 {PredefinedColor::cyan,
"\033[46m"},
128 {PredefinedColor::white,
"\033[47m"},
148 for (
const auto &[color, ansi_code] : fg_color_mappings) {
149 if (color == closest) {
158 const int color_code = rgb_to_ansi256(rgb.
r, rgb.
g, rgb.
b);
159 prefix +=
"\033[38;5;" + std::to_string(color_code) +
"m";
164 prefix +=
"\033[38;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" +
165 std::to_string(rgb.
b) +
"m";
173 for (
const auto &[color, ansi_code] : fg_color_mappings) {
191 for (
const auto &[color, ansi_code] : bg_color_mappings) {
192 if (color == closest) {
201 const int color_code = rgb_to_ansi256(rgb.
r, rgb.
g, rgb.
b);
202 prefix +=
"\033[48;5;" + std::to_string(color_code) +
"m";
207 prefix +=
"\033[48;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" +
208 std::to_string(rgb.
b) +
"m";
216 for (
const auto &[color, ansi_code] : bg_color_mappings) {
243 if (prefix.empty()) {
247 return prefix + text + ansi_reset;
std::string style(const std::string &text, Style styles) const override
Applies ANSI escape codes to style text according to the specified Style flags.
Text styling class supporting formatting, foreground colors, and background colors.
constexpr bool is_bg_rgb() const
Checks if the background color is in RGB mode.
constexpr bool has_faint() const
Checks if this Style has faint formatting.
constexpr bool has_fg_color() const
Checks if this Style has a foreground color set.
constexpr bool has_blink() const
Checks if this Style has blink formatting.
constexpr RgbColor fg_rgb() const
Returns the foreground RGB color.
constexpr bool has_underline() const
Checks if this Style has underline formatting.
constexpr bool has_italic() const
Checks if this Style has italic formatting.
constexpr PredefinedColor bg_predefined() const
Returns the predefined background color.
constexpr PredefinedColor fg_predefined() const
Returns the predefined foreground color.
constexpr bool has_bold() const
Checks if this Style has bold formatting.
constexpr RgbColor bg_rgb() const
Returns the background RGB color.
constexpr bool is_fg_rgb() const
Checks if the foreground color is in RGB mode.
constexpr bool has_bg_color() const
Checks if this Style has a background color set.
PredefinedColor
Predefined color options for text styling.
RGB color representation for extracting color components from Style values.
std::uint8_t b
Blue channel (0-255)
std::uint8_t r
Red channel (0-255)
std::uint8_t g
Green channel (0-255)