diff --git a/common/util.c b/common/util.c index 84ebab99..c7ef2ac4 100644 --- a/common/util.c +++ b/common/util.c @@ -31,6 +31,13 @@ bool parse_color(const char *color, uint32_t *result) { return true; } +void color_to_rgba(float dest[static 4], uint32_t color) { + dest[0] = ((color >> 24) & 0xff) / 255.0; + dest[1] = ((color >> 16) & 0xff) / 255.0; + dest[2] = ((color >> 8) & 0xff) / 255.0; + dest[3] = (color & 0xff) / 255.0; +} + bool parse_boolean(const char *boolean, bool current) { if (strcasecmp(boolean, "1") == 0 || strcasecmp(boolean, "yes") == 0 diff --git a/include/util.h b/include/util.h index 931ac691..867eb0a4 100644 --- a/include/util.h +++ b/include/util.h @@ -17,6 +17,8 @@ int wrap(int i, int max); */ bool parse_color(const char *color, uint32_t *result); +void color_to_rgba(float dest[static 4], uint32_t color); + /** * Given a string that represents a boolean, return the boolean value. This * function also takes in the current boolean value to support toggling. If diff --git a/sway/commands/client.c b/sway/commands/client.c index f5c7d90f..e4282341 100644 --- a/sway/commands/client.c +++ b/sway/commands/client.c @@ -3,56 +3,13 @@ #include "sway/config.h" #include "sway/output.h" #include "sway/tree/container.h" +#include "util.h" static void rebuild_textures_iterator(struct sway_container *con, void *data) { container_update_marks_textures(con); container_update_title_textures(con); } -/** - * Parse the hex string into an integer. - */ -static bool parse_color_int(char *hexstring, uint32_t *dest) { - if (hexstring[0] != '#') { - return false; - } - - if (strlen(hexstring) != 7 && strlen(hexstring) != 9) { - return false; - } - - ++hexstring; - char *end; - uint32_t decimal = strtol(hexstring, &end, 16); - - if (*end != '\0') { - return false; - } - - if (strlen(hexstring) == 6) { - // Add alpha - decimal = (decimal << 8) | 0xff; - } - - *dest = decimal; - return true; -} - -/** - * Parse the hex string into a float value. - */ -static bool parse_color_float(char *hexstring, float dest[static 4]) { - uint32_t decimal; - if (!parse_color_int(hexstring, &decimal)) { - return false; - } - dest[0] = ((decimal >> 24) & 0xff) / 255.0; - dest[1] = ((decimal >> 16) & 0xff) / 255.0; - dest[2] = ((decimal >> 8) & 0xff) / 255.0; - dest[3] = (decimal & 0xff) / 255.0; - return true; -} - static struct cmd_results *handle_command(int argc, char **argv, struct border_colors *class, char *cmd_name) { struct cmd_results *error = NULL; @@ -60,30 +17,28 @@ static struct cmd_results *handle_command(int argc, char **argv, return error; } - if (!parse_color_float(argv[0], class->border)) { - return cmd_results_new(CMD_INVALID, - "Unable to parse border color '%s'", argv[0]); + struct border_colors colors = {0}; + + struct { + const char *name; + float *rgba[4]; + } properties[] = { + { "border", colors.border }, + { "background", colors.background }, + { "text", colors.text }, + { "indicator", colors.indicator }, + { "child_border", colors.child_border } + }; + for (size_t i = 0; i < sizeof(properties) / sizeof(properties[0]); i++) { + uint32_t color; + if (!parse_color(argv[i], &color)) { + return cmd_results_new(CMD_INVALID, + "Invalid %s color %s", properties[i].name, argv[i]); + } + color_to_rgba(*properties[i].rgba, color); } - if (!parse_color_float(argv[1], class->background)) { - return cmd_results_new(CMD_INVALID, - "Unable to parse background color '%s'", argv[1]); - } - - if (!parse_color_float(argv[2], class->text)) { - return cmd_results_new(CMD_INVALID, - "Unable to parse text color '%s'", argv[2]); - } - - if (!parse_color_float(argv[3], class->indicator)) { - return cmd_results_new(CMD_INVALID, - "Unable to parse indicator color '%s'", argv[3]); - } - - if (!parse_color_float(argv[4], class->child_border)) { - return cmd_results_new(CMD_INVALID, - "Unable to parse child border color '%s'", argv[4]); - } + memcpy(class, &colors, sizeof(struct border_colors)); if (config->active) { root_for_each_container(rebuild_textures_iterator, NULL); diff --git a/sway/config.c b/sway/config.c index 34704277..6d730f46 100644 --- a/sway/config.c +++ b/sway/config.c @@ -31,6 +31,7 @@ #include "stringop.h" #include "list.h" #include "log.h" +#include "util.h" struct sway_config *config = NULL; @@ -192,13 +193,6 @@ static void destroy_removed_seats(struct sway_config *old_config, } } -static void set_color(float dest[static 4], uint32_t color) { - dest[0] = ((color >> 16) & 0xff) / 255.0; - dest[1] = ((color >> 8) & 0xff) / 255.0; - dest[2] = (color & 0xff) / 255.0; - dest[3] = 1.0; -} - static void config_defaults(struct sway_config *config) { if (!(config->swaynag_command = strdup("swaynag"))) goto cleanup; config->swaynag_config_errors = (struct swaynag_instance){0}; @@ -300,37 +294,37 @@ static void config_defaults(struct sway_config *config) { config->hide_lone_tab = false; // border colors - set_color(config->border_colors.focused.border, 0x4C7899); - set_color(config->border_colors.focused.background, 0x285577); - set_color(config->border_colors.focused.text, 0xFFFFFFFF); - set_color(config->border_colors.focused.indicator, 0x2E9EF4); - set_color(config->border_colors.focused.child_border, 0x285577); + color_to_rgba(config->border_colors.focused.border, 0x4C7899FF); + color_to_rgba(config->border_colors.focused.background, 0x285577FF); + color_to_rgba(config->border_colors.focused.text, 0xFFFFFFFF); + color_to_rgba(config->border_colors.focused.indicator, 0x2E9EF4FF); + color_to_rgba(config->border_colors.focused.child_border, 0x285577FF); - set_color(config->border_colors.focused_inactive.border, 0x333333); - set_color(config->border_colors.focused_inactive.background, 0x5F676A); - set_color(config->border_colors.focused_inactive.text, 0xFFFFFFFF); - set_color(config->border_colors.focused_inactive.indicator, 0x484E50); - set_color(config->border_colors.focused_inactive.child_border, 0x5F676A); + color_to_rgba(config->border_colors.focused_inactive.border, 0x333333FF); + color_to_rgba(config->border_colors.focused_inactive.background, 0x5F676AFF); + color_to_rgba(config->border_colors.focused_inactive.text, 0xFFFFFFFF); + color_to_rgba(config->border_colors.focused_inactive.indicator, 0x484E50FF); + color_to_rgba(config->border_colors.focused_inactive.child_border, 0x5F676AFF); - set_color(config->border_colors.unfocused.border, 0x333333); - set_color(config->border_colors.unfocused.background, 0x222222); - set_color(config->border_colors.unfocused.text, 0x88888888); - set_color(config->border_colors.unfocused.indicator, 0x292D2E); - set_color(config->border_colors.unfocused.child_border, 0x222222); + color_to_rgba(config->border_colors.unfocused.border, 0x333333FF); + color_to_rgba(config->border_colors.unfocused.background, 0x222222FF); + color_to_rgba(config->border_colors.unfocused.text, 0x88888888); + color_to_rgba(config->border_colors.unfocused.indicator, 0x292D2EFF); + color_to_rgba(config->border_colors.unfocused.child_border, 0x222222FF); - set_color(config->border_colors.urgent.border, 0x2F343A); - set_color(config->border_colors.urgent.background, 0x900000); - set_color(config->border_colors.urgent.text, 0xFFFFFFFF); - set_color(config->border_colors.urgent.indicator, 0x900000); - set_color(config->border_colors.urgent.child_border, 0x900000); + color_to_rgba(config->border_colors.urgent.border, 0x2F343AFF); + color_to_rgba(config->border_colors.urgent.background, 0x900000FF); + color_to_rgba(config->border_colors.urgent.text, 0xFFFFFFFF); + color_to_rgba(config->border_colors.urgent.indicator, 0x900000FF); + color_to_rgba(config->border_colors.urgent.child_border, 0x900000FF); - set_color(config->border_colors.placeholder.border, 0x000000); - set_color(config->border_colors.placeholder.background, 0x0C0C0C); - set_color(config->border_colors.placeholder.text, 0xFFFFFFFF); - set_color(config->border_colors.placeholder.indicator, 0x000000); - set_color(config->border_colors.placeholder.child_border, 0x0C0C0C); + color_to_rgba(config->border_colors.placeholder.border, 0x000000FF); + color_to_rgba(config->border_colors.placeholder.background, 0x0C0C0CFF); + color_to_rgba(config->border_colors.placeholder.text, 0xFFFFFFFF); + color_to_rgba(config->border_colors.placeholder.indicator, 0x000000FF); + color_to_rgba(config->border_colors.placeholder.child_border, 0x0C0C0CFF); - set_color(config->border_colors.background, 0xFFFFFF); + color_to_rgba(config->border_colors.background, 0xFFFFFFFF); // Security if (!(config->command_policies = create_list())) goto cleanup;