New color system; it's now a simple array

Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:27:56 -05:00
parent 3068ed3461
commit 526a64e982
Signed by: 0xf8
GPG Key ID: 446580D758689584

View File

@ -18,61 +18,43 @@ I DON'T CARE HOW YOU USE OR CONFIGURE THIS PROGRAM.
// How many times a color should be shown before switching to the next ( DEFAULT = 2 )
#define CONF_COLOR_LAG 2
// BEWARE RICERS
// If you add extra colors make sure to
// a) Add it to the color enum
// b) Add the case to the switch statement in the genColors function
// c) Update the COLORS definition with the new amount of colors
// FORMAT
// #define COLOR "R;G;B"
#define RED "193;71;87"
#define ORANGE "193;116;71"
#define YELLOW "193;177;71"
#define GREEN "149;193;71"
#define CYAN "71;193;161"
#define BLUE "71;165;193"
#define PURPLE "155;71;193"
#define PINK "193;71;153"
#define COLORS 8
enum color {
red,orange,yellow,green,cyan,blue,purple,pink
// RICERS:
// To add colors, simply add the color (according to the format below) to COLORS
// FORMAT: "R;G;B",
const char *COLORS[] = {
"193;71;87", // RED
"193;116;71", // ORANGE
"193;177;71", // YELLOW
"149;193;71", // GREEN
"71;193;161", // CYAN
"71;165;193", // BLUE
"155;71;193", // PURPLE
"193;71;153", // PINK
};
#define addcase(enum, defin)\
case enum:\
cstr = "\e[38;2;"defin"m";\
break;
unsigned long long t = 0;
void genColors(char *s) {
char *cstr = malloc(20 * sizeof(char));
for (int i = 0; i < strlen(s); i++) {
enum color c = (((t + i) / CONF_COLOR_LAG) % COLORS);
switch (c) {
//addcase(enum value, #define name)
addcase(red, RED);
addcase(orange, ORANGE);
addcase(yellow, YELLOW);
addcase(green, GREEN);
addcase(cyan, CYAN);
addcase(blue, BLUE);
addcase(purple, PURPLE);
addcase(pink, PINK);
}
char *cstr = (char *)malloc(20 * sizeof(char) + 1);
printf("%s%c",cstr,s[i]);
for (unsigned long i = 0; i < strlen(s); i++) {
unsigned long c = (((t + i) / CONF_COLOR_LAG) % (sizeof(COLORS) / sizeof(char *)));
snprintf(cstr, 20 * sizeof(char), "\e[38;2;%sm", COLORS[c]);
printf("%s%c\e[0m",cstr,s[i]);
}
free(cstr);
}
int main(int carg, char **varg) {
if (carg < 2) {
// [ERROR] No arguments given. USAGE: ... <text>
printf("\e[1;31m[ERROR]\e[0m No arguments given.\n\tUSAGE: \e[34m%s \e[33m<text>\e[0m\n",varg[0]);
return 1;
}
char *text = malloc(1024 * sizeof(char));
char *text = (char *)malloc(1024 * sizeof(char) + 1);
// Similar to pythons str.join(' ') method
// Adds all the arguments together with spaces seperating them
@ -89,7 +71,6 @@ int main(int carg, char **varg) {
while (1) {
printf("\r\e[2K");
genColors(text);
printf("\e[0m");
fflush(stdout);
usleep(CONF_SLEEP_TIMEMS*1000);
t += 1;