Skip to content

Commit

Permalink
fixup: Add 24bit color support
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Nov 6, 2024
1 parent 96fa3bf commit 05a7b23
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ std::string Color::colorize (const std::string& input) const
// 256 fg \033[38;5;Nm
// 256 bg \033[48;5;Nm
//
// 24 bit fg \033[38;2;R;G;Bm]
// 24 bit gg \033[48;2;R;G;Bm]
// 24 bit fg \033[38;2;R;G;Bm
// 24 bit gg \033[48;2;R;G;Bm
void Color::_colorize (std::string &result, const std::string& input) const
{
if (!nontrivial ())
Expand Down Expand Up @@ -567,11 +567,10 @@ void Color::_colorize (std::string &result, const std::string& input) const
if (count++) result += ';';
if (_value & _COLOR_24BIT)
{
result += "\033[38;2;";
result += "38;2;";
result += format((_value >> 16) & 0xFF) + ';';
result += format((_value >> 8) & 0xFF) + ';';
result += format((_value) & 0xFF);
result += 'm';
}
else if (_value & _COLOR_256)
{
Expand All @@ -589,11 +588,10 @@ void Color::_colorize (std::string &result, const std::string& input) const
if (count++) result += ';';
if (_value & _COLOR_24BIT)
{
result += "\033[48;2;";
result += "48;2;";
result += format((_value >> 48) & 0xFF) + ';';
result += format((_value >> 40) & 0xFF) + ';';
result += format((_value >> 32) & 0xFF);
result += 'm';
}
else if (_value & _COLOR_256)
{
Expand Down
14 changes: 7 additions & 7 deletions test/color.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ int main (int, char**)


// 24-bit color
t.is (Color::colorize ("foo", "0x0a1b2c"), std::string ("\033[38;2;10;27;44mfoo\033[0m"), "0x0a1b2c -> ^[[38;2;10;27;44m");
t.is (Color::colorize ("foo", "0x0a1b2c on color127"), std::string ("\033[38;2;10;27;44m\033[48;2;126;0;126mfoo\033[0m"), "0x0a1b2c on color127 -> ^[[38;2;10;27;44m^[[48;2;126;0;126m");
t.is (Color::colorize ("foo", "red on 0x0a1b2c"), std::string ("\033[38;2;128;0;0m\033[48;2;10;27;44mfoo\033[0m"), "red on 0x0a1b2c -> ^[[38;2;128;0;0m^[[38;2;10;27;44m");
t.is (Color::colorize ("foo", "bold red on 0x0a1b2c"), std::string ("\033[38;2;255;0;0m\033[48;2;10;27;44mfoo\033[0m"), "bold on 0x0a1b2c -> ^[[38;2;255;0;0m^[[38;2;10;27;44m");
t.is (Color::colorize ("foo", "0x0a1b2c on bright red"), std::string ("\033[38;2;10;27;44m\033[48;2;255;0;0mfoo\033[0m"), "0x0a1b2c on bright red -> ^[[38;2;10;27;44m^[[48;2;255;0;0m");
t.is (Color::colorize ("foo", "0x010101 on grey0"), std::string ("\033[38;2;1;1;1m\033[48;2;10;10;10mfoo\033[0m"), "0x0101010 on grey0 -> ^[[38;2;1;1;1m^[[48;2;10;10;10m");
t.is (Color::colorize ("foo", "0x101010 on grey23"), std::string ("\033[38;2;16;16;16m\033[48;2;240;240;240mfoo\033[0m"), "0x101010 on grey0 -> ^[[38;2;16;16;16m^[[48;2;240;240;240m");
t.is (Color::colorize ("foo", "0x0a1b2c"), std::string ("\033[38;2;10;27;44mfoo\033[0m"), "0x0a1b2c -> ^[[38;2;10;27;44m");
t.is (Color::colorize ("foo", "0x0a1b2c on color127"), std::string ("\033[38;2;10;27;44;48;2;126;0;126mfoo\033[0m"), "0x0a1b2c on color127 -> ^[[38;2;10;27;44;48;2;126;0;126m");
t.is (Color::colorize ("foo", "red on 0x0a1b2c"), std::string ("\033[38;2;128;0;0;48;2;10;27;44mfoo\033[0m"), "red on 0x0a1b2c -> ^[[38;2;128;0;0;48;2;10;27;44m");
t.is (Color::colorize ("foo", "bold red on 0x0a1b2c"), std::string ("\033[1;38;2;128;0;0;48;2;10;27;44mfoo\033[0m"), "bold on 0x0a1b2c -> ^[[1;38;2;255;0;0;48;2;10;27;44m");
t.is (Color::colorize ("foo", "0x0a1b2c on bright red"), std::string ("\033[38;2;10;27;44;48;2;255;0;0mfoo\033[0m"), "0x0a1b2c on bright red -> ^[[38;2;10;27;44;48;2;255;0;0m");
t.is (Color::colorize ("foo", "0x010101 on grey0"), std::string ("\033[38;2;1;1;1;48;2;10;10;10mfoo\033[0m"), "0x0101010 on grey0 -> ^[[38;2;1;1;1;48;2;10;10;10m");
t.is (Color::colorize ("foo", "0x101010 on grey23"), std::string ("\033[38;2;16;16;16;48;2;240;240;240mfoo\033[0m"), "0x101010 on grey0 -> ^[[38;2;16;16;16;48;2;240;240;240m");


t.is (Color ("0x0F1E2D").code (), "", "Color::code '0x0F1E2D' --> ^[[38;2;15;30;45m");
Expand Down

0 comments on commit 05a7b23

Please sign in to comment.