Skip to content

Commit

Permalink
Add bold color with 256 color space
Browse files Browse the repository at this point in the history
There is no reason that bold cannot work in 256 color space.
Perhaps initially this was disabled due to old terminals not supporting this behavior.
But in 2024 there is no reason to disable bold.

The expected test result for upgrade 'bold red on ...' should now be 'bold color 1 on ...'.
  • Loading branch information
Rahlir authored Oct 26, 2024
1 parent e0d493d commit 47a750c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 54 deletions.
89 changes: 36 additions & 53 deletions src/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ void Color::blend (const Color& other)
Color c (other);
_value |= (c._value & _COLOR_UNDERLINE); // Always inherit underline.
_value |= (c._value & _COLOR_INVERSE); // Always inherit inverse.
_value |= (c._value & _COLOR_BOLD); // Always inherit bold.

// 16 <-- 16.
if (!(_value & _COLOR_256) &&
!(c._value & _COLOR_256))
{
_value |= (c._value & _COLOR_BOLD); // Inherit bold.
_value |= (c._value & _COLOR_BRIGHT); // Inherit bright.

if (c._value & _COLOR_HASFG)
Expand Down Expand Up @@ -379,11 +379,9 @@ void Color::upgrade ()
{
if (_value & _COLOR_HASFG)
{
bool bold = _value & _COLOR_BOLD;
unsigned int fg = _value & _COLOR_FG;
_value &= ~_COLOR_FG;
_value &= ~_COLOR_BOLD;
_value |= (bold ? fg + 7 : fg - 1);
_value |= fg - 1;
}

if (_value & _COLOR_HASBG)
Expand Down Expand Up @@ -429,72 +427,57 @@ void Color::_colorize (std::string &result, const std::string& input) const

int count = 0;

// 256 color
if (_value & _COLOR_256)
{
if (_value & _COLOR_UNDERLINE)
result += "\033[4m";

if (_value & _COLOR_INVERSE)
result += "\033[7m";

if (_value & _COLOR_HASFG)
{
result += "\033[38;5;";
result += colorstring[(_value & _COLOR_FG)];
result += 'm';
}

if (_value & _COLOR_HASBG)
{
result += "\033[48;5;";
result += colorstring[((_value & _COLOR_BG) >> 8)];
result += 'm';
}
result += "\033[";

result += input;
result += "\033[0m";
if (_value & _COLOR_BOLD)
{
result += '1';
++count;
}

// 16 color
else
if (_value & _COLOR_UNDERLINE)
{
result += "\033[";
if (count++) result += ';';
result += '4';
}

if (_value & _COLOR_BOLD)
{
if (count++) result += ';';
result += '1';
}
if (_value & _COLOR_INVERSE)
{
if (count++) result += ';';
result += '7';
}

if (_value & _COLOR_UNDERLINE)
if (_value & _COLOR_HASFG)
{
if (count++) result += ';';
if (_value & _COLOR_256)
{
if (count++) result += ';';
result += '4';
result += "38;5;";
result += colorstring[(_value & _COLOR_FG)];
}

if (_value & _COLOR_INVERSE)
else
{
if (count++) result += ';';
result += '7';
result += colorstring[(29 + (_value & _COLOR_FG))];
}
}

if (_value & _COLOR_HASFG)
if (_value & _COLOR_HASBG)
{
if (count++) result += ';';
if (_value & _COLOR_256)
{
if (count++) result += ';';
result += colorstring[(29 + (_value & _COLOR_FG))];
result += "48;5;";
result += colorstring[((_value & _COLOR_BG) >> 8)];
}

if (_value & _COLOR_HASBG)
else
{
if (count++) result += ';';
result += colorstring[((_value & _COLOR_BRIGHT ? 99 : 39) + ((_value & _COLOR_BG) >> 8))];
}

result += 'm';
result += input;
result += "\033[0m";
}

result += 'm';
result += input;
result += "\033[0m";
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion test/color.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main (int, char**)
t.is ((std::string) c, "color1 on color0", "upgrade color1 on black -> color1 on color0");

c = Color ("bold red on color0");
t.is ((std::string) c, "color9 on color0", "upgrade bold red on color0 -> color9 on color0");
t.is ((std::string) c, "bold color1 on color0", "upgrade bold red on color0 -> bold color1 on color0");

c = Color ("color1 on bright black");
t.is ((std::string) c, "color1 on color8", "upgrade color1 on bright black -> color1 on color8");
Expand Down

0 comments on commit 47a750c

Please sign in to comment.