Implement Color4::ToHex() and Color4::ToHexAlpha()

This commit is contained in:
2025-03-22 12:22:05 -04:00
parent 944e568ecf
commit 8f49ba5e49
7 changed files with 314 additions and 94 deletions

229
include/AnsiEscapeCodes.hpp Normal file
View File

@@ -0,0 +1,229 @@
#pragma once
#include <cstdint>
#include <string>
#include <format>
// Gets set to whatever your terminal emulator is configured for.
// This means black can be shown as purple if configured that way.
namespace mcolor
{
/// Used for composing legacy ANSI escape sequence builtin colors.
/// We much recommend using modern Color4/Color3
enum class AnsiColor : uint8_t
{
RESET = 0,
BELL,
BOLD,
DIM = 2,
FG_BLACK = 30,
FG_RED,
FG_GREEN,
FG_YELLOW,
FG_BLUE,
FG_MAGENTA,
FG_CYAN,
FG_WHITE,
FG_DEFAULT = 39,
BG_BLACK = 40,
BG_RED,
BG_GREEN,
BG_YELLOW,
BG_BLUE,
BG_MAGENTA,
BG_CYAN,
BG_WHITE,
BG_DEFAULT = 49,
FG_BRIGHT_BLACK = 90,
FG_BRIGHT_RED,
FG_BRIGHT_GREEN,
FG_BRIGHT_YELLOW,
FG_BRIGHT_BLUE,
FG_BRIGHT_MAGENTA,
FG_BRIGHT_CYAN,
FG_BRIGHT_WHITE = 97,
BG_BRIGHT_BLACK = 100,
BG_BRIGHT_RED,
BG_BRIGHT_GREEN,
BG_BRIGHT_YELLOW,
BG_BRIGHT_BLUE,
BG_BRIGHT_MAGENTA,
BG_BRIGHT_CYAN,
BG_BRIGHT_WHITE = 107,
};
// TODO: Refactor to constexpr string literals
constexpr std::string AnsiEscapePrefix = "\033[";
namespace AnsiEscapeCodes {
#pragma region General
constexpr std::string Bell = "\a";
constexpr std::string Backspace = "\b";
constexpr std::string Tab = "\t";
constexpr std::string Newline = "\n";
constexpr std::string VerticalTab = "\v";
constexpr std::string Newpage = "\f";
constexpr std::string Return = "\r";
constexpr std::string Escape = "\033";
#pragma endregion
#pragma region Cursor Controls
/// Moves cursor to home position {0, 0}.
constexpr std::string CursorHome = AnsiEscapePrefix + "H";
/// Moves cursor to line Y, column X.
constexpr std::string CursorTo(int line, int column) {
return AnsiEscapePrefix + std::format("{};{}H", line, column);
}
/// Moves the cursor up # lines.
constexpr std::string CursorUp(int lines) {
return AnsiEscapePrefix + std::format("{}A", lines);
}
/// Moves the cursor down # lines.
constexpr std::string CursorDown(int lines) {
return AnsiEscapePrefix + std::format("{}B", lines);
}
/// Moves the cursor right # lines.
constexpr std::string CursorRight(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
/// Moves the cursor left # lines.
constexpr std::string CursorLeft(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
/// Moves cursor to the beginning of line, # lines down from the current line.
constexpr std::string CursorNextLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
/// Moves cursor to the beginning of line, # lines up from the current line.
constexpr std::string CursorPrevLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
/// Moves cursor to column #.
constexpr std::string CursorColumn(int column) {
return AnsiEscapePrefix + std::format("{}G", column);
}
/// Save current cursor position.
constexpr std::string CursorSave = AnsiEscapePrefix + "7";
/// Restore cursor to last saved position.
constexpr std::string CursorRestore = AnsiEscapePrefix + "8";
#pragma endregion
#pragma region Erase Functions
constexpr std::string EraseInDisplay = AnsiEscapePrefix + "J";
/// Erase from cursor until end of screen.
constexpr std::string EraseAfterCursor = AnsiEscapePrefix + "0J";
/// Erase from cursor to beginning of screen.
constexpr std::string EraseBeforeCursor = AnsiEscapePrefix + "1J";
constexpr std::string EraseEntireScreen = AnsiEscapePrefix + "2J";
constexpr std::string EraseSavedLines = AnsiEscapePrefix + "3J";
constexpr std::string EraseInLine = AnsiEscapePrefix + "K";
constexpr std::string EraseLineAfterCursor = AnsiEscapePrefix + "0K";
constexpr std::string EraseLineBeforeCursor = AnsiEscapePrefix + "1K";
#pragma endregion
#pragma region Graphics Sequences
constexpr std::string ResetAll = AnsiEscapePrefix + "0m";
constexpr std::string Bold = AnsiEscapePrefix + "1m";
constexpr std::string Dim = AnsiEscapePrefix + "2m";
constexpr std::string Italic = AnsiEscapePrefix + "3m";
constexpr std::string Underline = AnsiEscapePrefix + "4m";
constexpr std::string Blinking = AnsiEscapePrefix + "5m";
constexpr std::string Inverse = AnsiEscapePrefix + "6m";
constexpr std::string Hidden = AnsiEscapePrefix + "7m";
constexpr std::string Strikethrough = AnsiEscapePrefix + "8m";
#pragma endregion
#pragma region Color Sequences
constexpr std::string FgBlack = AnsiEscapePrefix + "30";
constexpr std::string BgBlack = AnsiEscapePrefix + "40";
constexpr std::string FgRed = AnsiEscapePrefix + "31";
constexpr std::string BgRed = AnsiEscapePrefix + "41";
constexpr std::string FgGreen = AnsiEscapePrefix + "32";
constexpr std::string BgGreen = AnsiEscapePrefix + "42";
constexpr std::string FgYellow = AnsiEscapePrefix + "33";
constexpr std::string BgYellow = AnsiEscapePrefix + "43";
constexpr std::string FgBlue = AnsiEscapePrefix + "34";
constexpr std::string BgBlue = AnsiEscapePrefix + "44";
constexpr std::string FgMagenta = AnsiEscapePrefix + "35";
constexpr std::string BgMagenta = AnsiEscapePrefix + "45";
constexpr std::string FgCyan = AnsiEscapePrefix + "36";
constexpr std::string BgCyan = AnsiEscapePrefix + "46";
constexpr std::string FgWhite = AnsiEscapePrefix + "37";
constexpr std::string BgWhite = AnsiEscapePrefix + "47";
constexpr std::string FgDefault = AnsiEscapePrefix + "38";
constexpr std::string BgDefault = AnsiEscapePrefix + "48";
constexpr std::string FgBrightBlack = AnsiEscapePrefix + "90";
constexpr std::string BgBrightBlack = AnsiEscapePrefix + "100";
constexpr std::string FgBrightRed = AnsiEscapePrefix + "91";
constexpr std::string BgBrightRed = AnsiEscapePrefix + "101";
constexpr std::string FgBrightGreen = AnsiEscapePrefix + "92";
constexpr std::string BgBrightGreen = AnsiEscapePrefix + "102";
constexpr std::string FgBrightYellow = AnsiEscapePrefix + "93";
constexpr std::string BgBrightYellow = AnsiEscapePrefix + "103";
constexpr std::string FgBrightBlue = AnsiEscapePrefix + "94";
constexpr std::string BgBrightBlue = AnsiEscapePrefix + "104";
constexpr std::string FgBrightMagenta = AnsiEscapePrefix + "95";
constexpr std::string BgBrightMagenta = AnsiEscapePrefix + "105";
constexpr std::string FgBrightCyan = AnsiEscapePrefix + "96";
constexpr std::string BgBrightCyan = AnsiEscapePrefix + "106";
constexpr std::string FgBrightWhite = AnsiEscapePrefix + "97";
constexpr std::string BgBrightWhite = AnsiEscapePrefix + "107";
constexpr std::string FgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("38;2;{};{};{}m", r, g, b);
}
constexpr std::string FgTrueColor(const Color4& c) {
return FgTrueColor(c.r, c.g, c.b);
}
constexpr std::string BgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("48;2;{};{};{}m", r, g, b);
}
constexpr std::string BgTrueColor(const Color4& c) {
return BgTrueColor(c.r, c.g, c.b);
}
#pragma endregion
#pragma region Private Modes
constexpr std::string CursorInvisible = AnsiEscapePrefix + "?25l";
constexpr std::string CursorVisible = AnsiEscapePrefix + "?25h";
constexpr std::string RestoreScreen = AnsiEscapePrefix + "?47l";
constexpr std::string SaveScreen = AnsiEscapePrefix + "?47h";
constexpr std::string EnableAltBuffer = AnsiEscapePrefix + "?1049h";
constexpr std::string DisableAltBuffer = AnsiEscapePrefix + "?1049l";
#pragma endregion
}
}

View File

@@ -32,40 +32,40 @@ public:
public:
/// The default constructor does not initialize any members.
Color4() = default;
constexpr Color4() = default;
/// Constructs a new Color4 from a Color3 and an optional alpha value.
explicit Color4(const Color3& color3, u8 alpha = 255);
constexpr explicit Color4(const Color3& color3, u8 alpha = 255);
/// Constructs a new Color4 from red, green, blue channel values, and an optional alpha value.
Color4(u8 red, u8 green, u8 blue, u8 alpha = 255);
constexpr Color4(u8 red, u8 green, u8 blue, u8 alpha = 255);
/// Constructs a new Color4 from an RGB structure and an optional alpha value.
explicit Color4(const RGB& rgb, u8 alpha = 255);
constexpr explicit Color4(const RGB& rgb, u8 alpha = 255);
/// Constructs a new Color4 from an RGBA structure.
explicit Color4(const RGBA& rgba);
constexpr explicit Color4(const RGBA& rgba);
/// Constructs a new Color4 from a floating-point RGB structure and an optional alpha value.
/// @note: Normalizes the color values from ranges [0, 1] to [0, 255].
explicit Color4(const RGBf& rgb, float alpha = 1.0f);
constexpr explicit Color4(const RGBf& rgb, float alpha = 1.0f);
/// Constructs a new Color4 from a floating-point RGBA structure.
/// /// @note: Normalizes the color values from ranges [0, 1] to [0, 255].
explicit Color4(const RGBAf& rgba);
constexpr explicit Color4(const RGBAf& rgba);
/// Constructs a new Color4 from an HSV structure.
explicit Color4(const HSV& hsv, float alpha = 1.f);
constexpr explicit Color4(const HSV& hsv, float alpha = 1.f);
/// Constructs a new Color4 from an HSVA structure.
explicit Color4(const HSVA& hsva);
constexpr explicit Color4(const HSVA& hsva);
/// TODO: HSL to RGB constructor
explicit Color4(const HSL& hsl, float alpha = 1.f);
constexpr explicit Color4(const HSL& hsl, float alpha = 1.f);
// TODO: LCH to RGB constructor
explicit Color4(const LCH& lch, float alpha = 1.f);
constexpr explicit Color4(const LCH& lch, float alpha = 1.f);
// TODO: LCHA to RGB constructor
explicit Color4(const LCHA& lcha);
constexpr explicit Color4(const LCHA& lcha);
static Color4 FromColor3(const Color3& color3, u8 alpha = 255);
@@ -93,7 +93,7 @@ public:
[[nodiscard]] Color4 LerpByHSVA(const Color4& rhs, float t) const;
[[nodiscard]] std::string ToHex() const;
[[nodiscard]] std::string ToHexA() const;
[[nodiscard]] std::string ToHexAlpha() const;
[[nodiscard]] u8 RedChannel() const;
[[nodiscard]] u8 GreenChannel() const;
@@ -128,4 +128,11 @@ public:
// TODO: Color4 ToLCH
LCH ToLCH() const;
// TODO: Disparate with mcolor::toEscapeCode, resolve.
[[nodiscard]] std::string EscapeCode(bool bg = false, bool bold = false);
[[nodiscard]] std::string FGEscapeCode(bool bold = false);
[[nodiscard]] std::string BGEscapeCode(bool bold = false);
};

View File

@@ -5,7 +5,8 @@
#define COLOR static const Color4
namespace Colors {
COLOR Transparent{0,0,0,0};
COLOR Transparent = Color4(0,0,0,0);
namespace Primary {
COLOR Red{255, 0, 0};
COLOR Green{0, 255, 0};
@@ -174,4 +175,5 @@ namespace Colors {
COLOR Brown{164, 42, 42};
COLOR Maroon{128, 0, 0};
}
}

View File

@@ -8,78 +8,9 @@
#include <format>
#include <vector>
#include "Color4.hpp"
#include "AnsiEscapeCodes.hpp"
// Gets set to whatever your terminal emulator is configured for.
// This means black can be shown as purple if configured that way.
namespace mcolor
{
enum class AnsiColor : uint8_t
{
RESET = 0,
BOLD,
DIM = 2,
FG_BLACK = 30,
FG_RED,
FG_GREEN,
FG_YELLOW,
FG_BLUE,
FG_MAGENTA,
FG_CYAN,
FG_WHITE,
FG_DEFAULT = 39,
BG_BLACK = 40,
BG_RED,
BG_GREEN,
BG_YELLOW,
BG_BLUE,
BG_MAGENTA,
BG_CYAN,
BG_WHITE,
BG_DEFAULT = 49,
FG_BRIGHT_BLACK = 90,
FG_BRIGHT_RED,
FG_BRIGHT_GREEN,
FG_BRIGHT_YELLOW,
FG_BRIGHT_BLUE,
FG_BRIGHT_MAGENTA,
FG_BRIGHT_CYAN,
FG_BRIGHT_WHITE = 97,
BG_BRIGHT_BLACK = 100,
BG_BRIGHT_RED,
BG_BRIGHT_GREEN,
BG_BRIGHT_YELLOW,
BG_BRIGHT_BLUE,
BG_BRIGHT_MAGENTA,
BG_BRIGHT_CYAN,
BG_BRIGHT_WHITE = 107,
};
}
namespace mcolor::rgbColors
{
// Basically default ugly standard UNIX colors for now
static const Color4 BLACK = {0,0,0};
static const Color4 RED = {170, 0, 0};
static const Color4 GREEN = {0, 170, 0};
static const Color4 YELLOW = {170, 170, 0};
static const Color4 BLUE = {0, 0, 170};
static const Color4 MAGENTA = {170, 0, 170};
static const Color4 CYAN = {0, 170, 170};
static const Color4 WHITE = {170, 170, 170};
static const Color4 DEFAULT = {170, 170, 170};
static const Color4 BRIGHT_BLACK = {8,5, 85,};
static const Color4 BRIGHT_RED = {85, 85, 255};
static const Color4 BRIGHT_GREEN = {85, 255, 85};
static const Color4 BRIGHT_YELLOW = {85, 255, 255};
static const Color4 BRIGHT_BLUE = {255, 85, 85};
static const Color4 BRIGHT_MAGENTA = {255, 85, 255};
static const Color4 BRIGHT_CYAN = {255, 255, 85};
static const Color4 BRIGHT_WHITE = {255, 255, 255};
}
namespace mcolor
{
namespace mcolor {
std::string toEscapeCode(Color4 c, bool bg=false);
std::string toEscapeCode(AnsiColor c);