Switched to use RGB hex for colors

This commit is contained in:
Bruce Hill 2019-06-13 15:19:35 -07:00
parent ced4364da1
commit b35873bb61
2 changed files with 24 additions and 6 deletions

View File

@ -10,8 +10,8 @@ Command line flags:
* `-S [0-8]*` the rules for when an alive cell survives
* `-B [0-8]*` the rules for when a new cell is born
* `-A <color>` provide a color for alive cells
* `-D <color>` provide a color for dead cells
* `-A <color>` provide a color for alive cells (RGB hex, either 3 or 6 chars)
* `-D <color>` provide a color for dead cells (RGB hex, either 3 or 6 chars)
Some interesting rulesets can be found in [this list](http://psoup.math.wisc.edu/mcell/rullex_life.html).

View File

@ -39,13 +39,27 @@ static void update(FILE *out, const char *cells, char *future_cells)
fflush(out);
}
static int gethexrgb(const char *str, int *r, int *g, int *b)
{
if (str[0] == '#') ++str;
if (strlen(str) == 3) {
if (sscanf(str, "%01X%01X%01X", r, g, b) != 3)
return 0;
*r *= 16; *g *= 16; *b *= 16;
return 3;
} else {
return sscanf(str, "%02X%02X%02X", r, g, b);
}
}
int main(int argc, char **argv) {
int ret = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
usage:
printf("Conway's Game of Life Viewer\nUsage: conway [-S<survival rules>] [-B<birth rules>] [-A<alive color>] [-D<dead color>]\n");
return 0;
fprintf(stderr, "Conway's Game of Life Viewer\n"
"Usage: conway [-S <survival rules>] [-B <birth rules>] [-A <alive RGB hex>] [-D <dead RGB hex>]\n");
return ret;
} else if (strcmp(argv[i], "-B") == 0) {
if (i + 1 >= argc) goto usage;
memset(rules[0], 0, sizeof(rules[0]));
@ -60,9 +74,13 @@ int main(int argc, char **argv) {
rules[1][*p - '0'] = 1;
} else if (strcmp(argv[i], "-A") == 0) {
if (i + 1 >= argc) goto usage;
sprintf(alive_color, "\033[%sm", argv[++i]);
int r,g,b;
if (gethexrgb(argv[++i], &r, &g, &b) != 3) goto usage;
sprintf(alive_color, "\033[48;2;%d;%d;%d;m", r,g,b);
} else if (strcmp(argv[i], "-D") == 0) {
sprintf(dead_color, "\033[%sm", argv[++i]);
int r,g,b;
if (gethexrgb(argv[++i], &r, &g, &b) != 3) goto usage;
sprintf(dead_color, "\033[48;2;%d;%d;%d;m", r,g,b);
} else {
goto usage;
}