Skip to content

Instantly share code, notes, and snippets.

@bfu4
Last active June 4, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfu4/a262a0a9ca73f11723c2d6d6474c0288 to your computer and use it in GitHub Desktop.
Save bfu4/a262a0a9ca73f11723c2d6d6474c0288 to your computer and use it in GitHub Desktop.
I KEEP WRITING COLOR FORMATS, HELP
#define npos_v 18446744073709551615UL
static std::string color_format(int color) {
std::string formatted = "\u001b[38;5;" + std::to_string(color) + "m";
return formatted;
}
typedef struct color_t {
std::string color;
std::string escape;
} color_t;
color_t Colors[] = {
{ "RED", color_format(124) },
{ "ORANGE", color_format(214) },
{ "YELLOW", color_format(226) },
{ "GREEN", color_format(82) },
{ "AQUA", color_format(81) },
{ "BLUE", color_format(64) },
{ "PURPLE", color_format(129) },
{ "PINK", color_format(219) },
{ "WHITE", color_format(255) },
{ "GRAY", color_format(250) },
{ "RESET", "\u001b[0m" }
};
static std::string _color(std::string fmt) {
std::string colored = fmt;
for (color_t& color: Colors) {
std::string to_find = "<" + color.color + ">";
int index;
while ((index = colored.find(to_find)) != npos_v) {
colored = colored.replace(index, to_find.size() , color.escape);
}
}
return colored;
}
static void Write(std::string msg) {
printf("%s", _color(msg).c_str());
}
// Usage:
// std::string colored_string = COLOR_SHIFT(39, 4) + "Cool string!";
// (39, 4) = 183 or #d7afff
//
// Image Reference:
// https://i.stack.imgur.com/KTSQa.png
#define COLOR_SHIFT(x, y) color_format(x + (36 * y))
#undef npos_v // unneeded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment