23int rgb_to_ansi256(std::uint8_t r, std::uint8_t g, std::uint8_t b)
26 if (r == g && g == b) {
33 return static_cast<int>(std::lround((r - 8) / 247.0 * 24)) + 232;
38 auto to_6 = [](
const std::uint8_t val) ->
int {
45 return (val - 35) / 40;
48 const int r_idx = to_6(r);
49 const int g_idx = to_6(g);
50 const int b_idx = to_6(b);
52 return 16 + 36 * r_idx + 6 * g_idx + b_idx;
66PredefinedColor find_closest_plain_color(std::uint8_t r, std::uint8_t g, std::uint8_t b)
69 const std::array<std::pair<PredefinedColor, std::array<int, 3>>, 8> plain_colors = {{
70 {PredefinedColor::black, {0, 0, 0}},
71 {PredefinedColor::red, {255, 0, 0}},
72 {PredefinedColor::green, {0, 255, 0}},
73 {PredefinedColor::yellow, {255, 255, 0}},
74 {PredefinedColor::blue, {0, 0, 255}},
75 {PredefinedColor::magenta, {255, 0, 255}},
76 {PredefinedColor::cyan, {0, 255, 255}},
77 {PredefinedColor::white, {255, 255, 255}},
81 double min_distance = std::numeric_limits<double>::max();
83 for (
const auto &[color, rgb] : plain_colors) {
84 const int dr = r - rgb[0];
85 const int dg = g - rgb[1];
86 const int db = b - rgb[2];
89 const double distance = 2.0 * dr * dr + 4.0 * dg * dg + 3.0 * db * db;
91 if (distance < min_distance) {
92 min_distance = distance;
101const std::string ansi_reset =
"\033[0m";
104const std::array<std::pair<PredefinedColor, std::string>, 8> fg_color_mappings = {{
105 {PredefinedColor::black,
"\033[30m"},
106 {PredefinedColor::red,
"\033[31m"},
107 {PredefinedColor::green,
"\033[32m"},
108 {PredefinedColor::yellow,
"\033[33m"},
109 {PredefinedColor::blue,
"\033[34m"},
110 {PredefinedColor::magenta,
"\033[35m"},
111 {PredefinedColor::cyan,
"\033[36m"},
112 {PredefinedColor::white,
"\033[37m"},
116const std::array<std::pair<PredefinedColor, std::string>, 8> bg_color_mappings = {{
117 {PredefinedColor::black,
"\033[40m"},
118 {PredefinedColor::red,
"\033[41m"},
119 {PredefinedColor::green,
"\033[42m"},
120 {PredefinedColor::yellow,
"\033[43m"},
121 {PredefinedColor::blue,
"\033[44m"},
122 {PredefinedColor::magenta,
"\033[45m"},
123 {PredefinedColor::cyan,
"\033[46m"},
124 {PredefinedColor::white,
"\033[47m"},
144 for (
const auto &[color, ansi_code] : fg_color_mappings) {
145 if (color == closest) {
154 const int color_code = rgb_to_ansi256(rgb.
r, rgb.
g, rgb.
b);
155 prefix +=
"\033[38;5;" + std::to_string(color_code) +
"m";
160 prefix +=
"\033[38;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" +
161 std::to_string(rgb.
b) +
"m";
169 for (
const auto &[color, ansi_code] : fg_color_mappings) {
187 for (
const auto &[color, ansi_code] : bg_color_mappings) {
188 if (color == closest) {
197 const int color_code = rgb_to_ansi256(rgb.
r, rgb.
g, rgb.
b);
198 prefix +=
"\033[48;5;" + std::to_string(color_code) +
"m";
203 prefix +=
"\033[48;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" +
204 std::to_string(rgb.
b) +
"m";
212 for (
const auto &[color, ansi_code] : bg_color_mappings) {
239 if (prefix.empty()) {
243 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)