Skip to content

Instantly share code, notes, and snippets.

@dvtate
Last active March 31, 2017 06:30
Show Gist options
  • Save dvtate/a7672c0893ee82a6c47513fecce4045d to your computer and use it in GitHub Desktop.
Save dvtate/a7672c0893ee82a6c47513fecce4045d to your computer and use it in GitHub Desktop.
a simple test of ansi color codes for modern terminal emulators
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#define COLOR_RESET "\x1B[0m"
inline void textColor()
{
puts(COLOR_RESET);
}
inline void cycle3(uint8_t& v0, uint8_t& v1, uint8_t& v2, uint8_t& curHi)
{
if (curHi == 0) {
v0--; v1++;
} else if (curHi == 1) {
v1--; v2++;
} else if (curHi == 2) {
v2--; v0++;
}
if (v0 <= 0 && curHi == 0)
curHi = 1;
else if (v1 <= 0 && curHi == 1)
curHi = 2;
else if (v2 <= 0 && curHi == 2)
curHi = 0;
}
inline void color_puts(const char* text, uint8_t red, uint8_t green, uint8_t blue)
{ printf("\x1B[38;2;%d;%d;%dm%s\x1B[0m", red, green, blue, text); }
void color_printf(uint8_t red, uint8_t green, uint8_t blue, const char* format, ...){
va_list args;
va_start(args, format);
printf("\x1B[38;2;%d;%d;%dm", red, green, blue);
vprintf(format, args);
va_end(args);
printf(COLOR_RESET);
}
int main(){
color_printf(0, 255, 255, "%s %d\n", "wat", 122);
color_puts("hello world\n", 255, 0, 255);
puts("");
}
@dvtate
Copy link
Author

dvtate commented Mar 31, 2017

note: this has been upgraded: https://github.com/dvtate/terminal-colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment