From 39e6fe44effdfb443b1b9766b31803dd173eb1be Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Tue, 8 Jun 2021 19:09:35 -0700 Subject: [PATCH] Updated WolframAutomata's color related CLI flags to match README. --- hacks/WolframAutomata/WolframAutomata.c | 46 ++++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/hacks/WolframAutomata/WolframAutomata.c b/hacks/WolframAutomata/WolframAutomata.c index 5e0b725..e84ba44 100644 --- a/hacks/WolframAutomata/WolframAutomata.c +++ b/hacks/WolframAutomata/WolframAutomata.c @@ -279,26 +279,30 @@ WolframAutomata_init(Display * dpy, Window win) state->admiration_delay = 5000000; state->admiration_in_progress = False; - if (get_boolean_resource(state->dpy, "random-colors", "Boolean")) { - XColor fg, bg; - size_t rand_i = random() % sizeof(color_list)/sizeof(color_list[0]); - fg.red = color_list[rand_i].fg_red; - fg.green = color_list[rand_i].fg_green; - fg.blue = color_list[rand_i].fg_blue; - bg.red = color_list[rand_i].bg_red; - bg.green = color_list[rand_i].bg_green; - bg.blue = color_list[rand_i].bg_blue; - /* TODO: Since I 'alloc', presumably I must also 'free' these colors */ - /* at some point. Where/how? I don't want to eventually crash my */ - /* X server after months of use. */ - XAllocColor(state->dpy, xgwa.colormap, &fg); - XAllocColor(state->dpy, xgwa.colormap, &bg); - state->fg = gcv.foreground = fg.pixel; - state->bg = gcv.background = bg.pixel; - } else { - state->fg = gcv.foreground = get_pixel_resource(state->dpy, xgwa.colormap, "foreground", "Foreground"); - state->bg = gcv.background = get_pixel_resource(state->dpy, xgwa.colormap, "background", "Background"); + /* Set foreground and background colors for active/inactive cells. Either */ + /* the user provided an index into the pre-defined color_list[] or a */ + /* random entry from that same array should be selected. */ + size_t color_index = get_integer_resource(state->dpy, "color-index", "Integer"); + if (color_index == -1) { + color_index = random() % sizeof(color_list)/sizeof(color_list[0]); + } else if (color_index >= sizeof(color_list)/sizeof(color_list[0])) { + fprintf(stderr, "WARNING: Color index out of range.\n"); + color_index = 0; } + XColor fg, bg; + fg.red = color_list[color_index].fg_red; + fg.green = color_list[color_index].fg_green; + fg.blue = color_list[color_index].fg_blue; + bg.red = color_list[color_index].bg_red; + bg.green = color_list[color_index].bg_green; + bg.blue = color_list[color_index].bg_blue; + /* TODO: Since I 'alloc', presumably I must also 'free' these colors */ + /* at some point. Where/how? I don't want to eventually crash my */ + /* X server after months of use. */ + XAllocColor(state->dpy, xgwa.colormap, &fg); + XAllocColor(state->dpy, xgwa.colormap, &bg); + state->fg = gcv.foreground = fg.pixel; + state->bg = gcv.background = bg.pixel; state->gc = XCreateGC(state->dpy, state->win, GCForeground, &gcv); @@ -501,10 +505,10 @@ static const char * WolframAutomata_defaults[] = { "*delay-usec: 25000", "*num-generations: 5000", "*pixel-size: 2", + "*color-index: -1", "*population-density: 50", "*population-single: False", "*random-cellsize: False", - "*random-color: False", "*random-delay: False", "*random-length: False", "*random-rule: False", @@ -516,10 +520,10 @@ static XrmOptionDescRec WolframAutomata_options[] = { { "-delay-usec", ".delay-usec", XrmoptionSepArg, 0 }, { "-num-generations", ".num-generations", XrmoptionSepArg, 0 }, { "-pixel-size", ".pixel-size", XrmoptionSepArg, 0 }, + { "-color-index", ".color-index", XrmoptionSepArg, 0 }, { "-population-density", ".population-density", XrmoptionSepArg, 0 }, { "-population-single", ".population-single", XrmoptionNoArg, "True" }, { "-random-cellsize", ".random-pixel-size", XrmoptionNoArg, "True" }, - { "-random-color", ".random-colors", XrmoptionNoArg, "True" }, { "-random-delay", ".random-delay", XrmoptionNoArg, "True" }, { "-random-length", ".random-num-generations", XrmoptionNoArg, "True" }, { "-random-rule", ".rule-random", XrmoptionNoArg, "True" }, -- 2.20.1