Skip to content

Commit

Permalink
Color changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-n-t-h-o-n-y committed Feb 15, 2024
1 parent c0754f4 commit 5df154c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ int main()
std::cout
<< escape(Brush{
.background = XColor::Cyan,
.foreground = TrueColor{0xF02127},
.foreground = TColor{0xF02127},
.traits = Trait::Italic,
}) << "Hello"

<< escape(Brush{}) << " 👽 "

<< escape(
fg(TrueColor{0x76F09C}),
fg(TColor{0x76F09C}),
Trait::Bold | Trait::Underline
) << "World!"

Expand Down
12 changes: 4 additions & 8 deletions examples/pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,15 @@ using EventResponse = std::optional<State>;
return std::string(std::max((dimensions.width - width) / 2, 0), ' ');
}();

auto hsl = HSL{
.hue = hue,
.saturation = 90,
.lightness = 70,
};
auto hsl = HSL{.hue = hue, .saturation = 90, .lightness = 70};

auto bytes = escape(Cursor{.x = 0, .y = 6}, Trait::Bold);
bytes += escape(fg(TrueColor{hsl})) + padding;
bytes += escape(fg(TColor{hsl})) + padding;
for (char c : state.display) {
bytes += c;
if (c == '\n') {
hsl.hue = (hsl.hue + 20) % 360;
bytes += escape(fg(TrueColor{hsl})) + padding;
bytes += escape(fg(TColor{hsl})) + padding;
}
}

Expand Down Expand Up @@ -390,7 +386,7 @@ using EventResponse = std::optional<State>;
auto const speed =
std::sqrt(velocity.dx * velocity.dx + velocity.dy * velocity.dy);

return TrueColor{HSL{
return TColor{HSL{
.hue = std::uint16_t((90 + (int)(speed * 160)) % 360),
.saturation = 80,
.lightness = 70,
Expand Down
4 changes: 2 additions & 2 deletions include/esc/brush.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace esc {
* escape(Brush) in sequence.hpp
*/
struct Brush {
Color background = DefaultColor{};
Color foreground = DefaultColor{};
Color background = XColor::Default;
Color foreground = XColor::Default;
Traits traits = Traits{Trait::None};

[[nodiscard]] constexpr bool operator==(Brush const&) const = default;
Expand Down
25 changes: 9 additions & 16 deletions include/esc/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

namespace esc {

/**
* Represents the default color of the terminal.
*
* @details When used with foregound() this will be the default foreground color
* of the terminal. When used with background() this will be the
* default background color of the terminal.
*/
struct DefaultColor {
[[nodiscard]] constexpr bool operator==(DefaultColor const&) const =
default;
[[nodiscard]] constexpr bool operator!=(DefaultColor const&) const =
default;
};

// -----------------------------------------------------------------------------

/**
Expand All @@ -35,8 +21,9 @@ struct DefaultColor {
* @see https://www.ditig.com/publications/256-colors-cheat-sheet
*/
struct XColor {
std::uint8_t value;
std::uint16_t value;

static XColor const Default;
static XColor const Black;
static XColor const Red;
static XColor const Green;
Expand All @@ -59,6 +46,7 @@ struct XColor {
};

// Yes, defining constexpr with const declaration is legal here.
constexpr XColor XColor::Default = XColor{256};
constexpr XColor XColor::Black = XColor{0};
constexpr XColor XColor::Red = XColor{1};
constexpr XColor XColor::Green = XColor{2};
Expand Down Expand Up @@ -269,14 +257,19 @@ struct TrueColor {
[[nodiscard]] constexpr bool operator!=(TrueColor const&) const = default;
};

/**
* Alias for TrueColor.
*/
using TColor = TrueColor;

// -----------------------------------------------------------------------------

/**
* Variant holding any of the color types that can be used.
*
* @details escape(Color) will generate an escape sequence for the color.
*/
using Color = std::variant<XColor, TrueColor, DefaultColor>;
using Color = std::variant<XColor, TrueColor>;

// -----------------------------------------------------------------------------

Expand Down
14 changes: 0 additions & 14 deletions include/esc/sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ struct BlankScreen {};
*/
[[nodiscard]] auto escape_bg(TrueColor c) -> std::string;

/// Return control sequence to reset the background to the terminal default

/**
* Get the control sequence to reset the background to the terminal default.
* @return The control sequence to reset the background to the terminal default.
*/
[[nodiscard]] auto escape_bg(DefaultColor c) -> std::string;

/**
* Get the last Color that was created with escape(ColorBG).
*
Expand Down Expand Up @@ -174,12 +166,6 @@ struct BlankScreen {};
*/
[[nodiscard]] auto escape_fg(TrueColor c) -> std::string;

/**
* Get the control sequence to reset the foreground to the terminal default.
* @return The control sequence to reset the foreground to the terminal default.
*/
[[nodiscard]] auto escape_fg(DefaultColor c) -> std::string;

/**
* Get the last Color that was created with escape(ColorFG).
*
Expand Down
42 changes: 20 additions & 22 deletions src/sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace {

// Mutable, currently set Traits & Colors. Yes, they are global to this TU.
auto current_traits = esc::Traits{esc::Trait::None};
auto current_background = esc::Color{esc::DefaultColor{}};
auto current_foreground = esc::Color{esc::DefaultColor{}};
auto current_background = esc::Color{esc::XColor::Default};
auto current_foreground = esc::Color{esc::XColor::Default};

/**
* Translate a single Trait into its control sequence parameter integer.
Expand Down Expand Up @@ -128,9 +128,15 @@ auto escape(ColorBG c) -> std::string
auto escape_bg(XColor c) -> std::string
{
::current_background = c;
return "\033["
"48;5;" +
std::to_string(c.value) + 'm';
if (c == XColor::Default) {
return "\033["
"49m";
}
else {
return "\033["
"48;5;" +
std::to_string(c.value) + 'm';
}
}

auto escape_bg(TrueColor c) -> std::string
Expand All @@ -142,13 +148,6 @@ auto escape_bg(TrueColor c) -> std::string
std::to_string(c.blue) + 'm';
}

auto escape_bg(DefaultColor c) -> std::string
{
::current_background = c;
return "\033["
"49m";
}

auto background_color() -> Color { return ::current_background; }

auto escape(ColorFG c) -> std::string
Expand All @@ -159,9 +158,15 @@ auto escape(ColorFG c) -> std::string
auto escape_fg(XColor c) -> std::string
{
::current_foreground = c;
return "\033["
"38;5;" +
std::to_string(c.value) + 'm';
if (c == XColor::Default) {
return "\033["
"39m";
}
else {
return "\033["
"38;5;" +
std::to_string(c.value) + 'm';
}
}

auto escape_fg(TrueColor c) -> std::string
Expand All @@ -173,13 +178,6 @@ auto escape_fg(TrueColor c) -> std::string
std::to_string(c.blue) + 'm';
}

auto escape_fg(DefaultColor c) -> std::string
{
::current_foreground = c;
return "\033["
"39m";
}

auto foreground_color() -> Color { return ::current_foreground; }

auto escape(Brush b) -> std::string
Expand Down
4 changes: 2 additions & 2 deletions tools/termcaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ using namespace esc;
}, bg(XColor{static_cast<std::uint8_t>(i)})));
result.push_back(' ');
}
return result.append(escape(bg(DefaultColor{})));
return result.append(escape(bg(XColor::Default)));
}

[[nodiscard]] auto hue_increment_row(int width, HSL start) -> std::string
Expand Down Expand Up @@ -253,7 +253,7 @@ using namespace esc;
saturation_increment_row(width, init) +
escape(Cursor{offset.x, offset.y + 3}) +
lightness_increment_row(width, init) +
escape(bg(DefaultColor{})
escape(bg(XColor::Default)
);
}

Expand Down

0 comments on commit 5df154c

Please sign in to comment.