diff options
author | Manuel Stoeckl <code@mstoeckl.com> | 2023-12-17 10:54:46 -0500 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2023-12-20 16:17:23 +0100 |
commit | eabc06e7eb9e5cb08c8177f49753dedc505076b4 (patch) | |
tree | c4b8b8520fe82d1bf84ac421ad19df74b62090fa /main.c | |
parent | 435be14610a4b4538adc6a926160ed434ff630fa (diff) | |
download | swaybg-eabc06e7eb9e5cb08c8177f49753dedc505076b4.tar.gz swaybg-eabc06e7eb9e5cb08c8177f49753dedc505076b4.tar.bz2 |
Unify color validation and parsing functions
Before this change, parse_color() and is_valid_color() behaved slightly
differently: parse_color() accepted both colors with and without alpha,
with optional leading #, while is_valid_color() forbade alpha and required
a leading # character. This commit merges the two functions into one
simpler function that forbids alpha and allows an optional leading #.
(Alpha values are forbidden because backgrounds should be opaque; a
leading # is optional to make shell scripts easier to write.)
Diffstat (limited to '')
-rw-r--r-- | main.c | 49 |
1 files changed, 19 insertions, 30 deletions
@@ -16,22 +16,29 @@ #include "viewporter-client-protocol.h" #include "single-pixel-buffer-v1-client-protocol.h" -static uint32_t parse_color(const char *color) { +/* + * If `color` is a hexadecimal string of the form 'rrggbb' or '#rrggbb', + * `*result` will be set to the uint32_t version of the color. Otherwise, + * return false and leave `*result` unmodified. + */ +static bool parse_color(const char *color, uint32_t *result) { if (color[0] == '#') { ++color; } int len = strlen(color); - if (len != 6 && len != 8) { - swaybg_log(LOG_DEBUG, "Invalid color %s, defaulting to 0xFFFFFFFF", - color); - return 0xFFFFFFFF; + if (len != 6) { + return false; } - uint32_t res = (uint32_t)strtoul(color, NULL, 16); - if (strlen(color) == 6) { - res = (res << 8) | 0xFF; + for (int i = 0; i < len; ++i) { + if (!isxdigit(color[i])) { + return false; + } } - return res; + + uint32_t val = (uint32_t)strtoul(color, NULL, 16); + *result = (val << 8) | 0xFF; + return true; } struct swaybg_state { @@ -84,24 +91,6 @@ struct swaybg_output { struct wl_list link; }; -bool is_valid_color(const char *color) { - int len = strlen(color); - if (len != 7 || color[0] != '#') { - swaybg_log(LOG_ERROR, "%s is not a valid color for swaybg. " - "Color should be specified as #rrggbb (no alpha).", color); - return false; - } - - int i; - for (i = 1; i < len; ++i) { - if (!isxdigit(color[i])) { - return false; - } - } - - return true; -} - static void render_frame(struct swaybg_output *output, cairo_surface_t *surface) { int buffer_width = output->width * output->scale, buffer_height = output->height * output->scale; @@ -469,11 +458,11 @@ static void parse_command_line(int argc, char **argv, } switch (c) { case 'c': // color - if (!is_valid_color(optarg)) { - swaybg_log(LOG_ERROR, "Invalid color: %s", optarg); + if (!parse_color(optarg, &config->color)) { + swaybg_log(LOG_ERROR, "%s is not a valid color for swaybg. " + "Color should be specified as rrggbb or #rrggbb (no alpha).", optarg); continue; } - config->color = parse_color(optarg); break; case 'i': // image config->image_path = optarg; |