summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Stoeckl <code@mstoeckl.com>2023-12-17 10:54:46 -0500
committerSimon Ser <contact@emersion.fr>2023-12-20 16:17:23 +0100
commiteabc06e7eb9e5cb08c8177f49753dedc505076b4 (patch)
treec4b8b8520fe82d1bf84ac421ad19df74b62090fa
parent435be14610a4b4538adc6a926160ed434ff630fa (diff)
downloadswaybg-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.)
-rw-r--r--main.c49
-rw-r--r--swaybg.1.scd2
2 files changed, 20 insertions, 31 deletions
diff --git a/main.c b/main.c
index 211a874..3787cc7 100644
--- a/main.c
+++ b/main.c
@@ -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;
diff --git a/swaybg.1.scd b/swaybg.1.scd
index adf172f..4175225 100644
--- a/swaybg.1.scd
+++ b/swaybg.1.scd
@@ -16,7 +16,7 @@ these options.
# OPTIONS
-*-c, --color* <#rrggbb>
+*-c, --color* <[#]rrggbb>
Set the background color.
*-h, --help*