Skip to content

Commit

Permalink
Change color format from #RRGGBBAA to RRGGBB[AA]
Browse files Browse the repository at this point in the history
Remove "#" character and make alpha channel optional.

closes #80
  • Loading branch information
francma committed Jun 12, 2022
1 parent 17578b6 commit c1f4985
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
32 changes: 17 additions & 15 deletions color.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define WOB_FILE "color.c"

#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -45,21 +46,26 @@ wob_color_premultiply_alpha(const struct wob_color color)
bool
wob_color_from_string(const char *restrict str, char **restrict str_end, struct wob_color *color)
{
if (str[0] != '#') {
return false;
}
str += 1;

char buffer[3] = {0};
uint8_t parts[4];
for (size_t i = 0; i < (sizeof(parts) / sizeof(uint8_t)); ++i) {
char *strtoul_end;
char buffer[3] = {0};
parts[3] = 0xFF;

int i;
for (i = 0; i < 4; ++i) {
strncpy(buffer, &str[i * 2], 2);
parts[i] = strtoul(buffer, &strtoul_end, 16);
if (strtoul_end != buffer + 2) {
return false;
if (!isxdigit(buffer[0]) || !isxdigit(buffer[1])) {
break;
}

parts[i] = strtoul(buffer, NULL, 16);
}

if (i < 3) {
return false;
}

if (str_end) {
*str_end = ((char *) str) + i * 2;
}

*color = (struct wob_color){
Expand All @@ -69,9 +75,5 @@ wob_color_from_string(const char *restrict str, char **restrict str_end, struct
.a = (float) parts[3] / UINT8_MAX,
};

if (str_end) {
*str_end = ((char *) str) + sizeof("FFFFFFFF") - 1;
}

return true;
}
12 changes: 6 additions & 6 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,47 +192,47 @@ handler(void *user, const char *section, const char *name, const char *value)
}
if (strcmp(name, "background_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Background color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Background color must be in RRGGBB[AA] format");
return 0;
}
config->colors.background = color;
return 1;
}
if (strcmp(name, "border_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Border color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Border color must be in RRGGBB[AA] format.");
return 0;
}
config->colors.border = color;
return 1;
}
if (strcmp(name, "bar_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Bar color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Bar color must be in RRGGBB[AA] format.");
return 0;
}
config->colors.value = color;
return 1;
}
if (strcmp(name, "overflow_background_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow background color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Overflow background color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.background = color;
return 1;
}
if (strcmp(name, "overflow_border_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow border color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Overflow border color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.border = color;
return 1;
}
if (strcmp(name, "overflow_bar_color") == 0) {
if (!parse_color(value, &color)) {
wob_log_error("Overflow bar color must be a value between #00000000 and #FFFFFFFF.");
wob_log_error("Overflow bar color must be in RRGGBB[AA] format.");
return 0;
}
config->overflow_colors.value = color;
Expand Down
3 changes: 2 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define MIN_PERCENTAGE_BAR_HEIGHT 1

// sizeof already includes NULL byte
#define INPUT_BUFFER_LENGTH (3 * sizeof(unsigned long) + sizeof(" #000000FF #FFFFFFFF #FFFFFFFF\n"))
#define INPUT_BUFFER_LENGTH (3 * sizeof(unsigned long) + sizeof(" 000000FF FFFFFFFF FFFFFFFF\n"))

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

Expand Down Expand Up @@ -509,6 +509,7 @@ main(int argc, char **argv)
wob_config_init(&app.wob_config);
if (wob_config_path != NULL) {
if (!wob_config_load(&app.wob_config, wob_config_path)) {
wob_config_destroy(&app.wob_config);
free(wob_config_path);
return EXIT_FAILURE;
}
Expand Down
15 changes: 11 additions & 4 deletions tests/wob_parse_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ main(int argc, char **argv)
bool result;

printf("running 1\n");
input = "25 #000000FF #FFFFFFFF #FFFFFFFF\n";
input = "25 000000FF FFFFFFFF FFFFFFFF\n";
result = wob_parse_input(input, &percentage, &background, &border, &bar);
if (!result || percentage != 25 || wob_color_to_argb(background) != 0xFF000000 || wob_color_to_argb(border) != 0xFFFFFFFF || wob_color_to_argb(bar) != 0xFFFFFFFF) {
return EXIT_FAILURE;
}

printf("running 2\n");
input = "25 #000000FF\n";
input = "25 000000FF\n";
result = wob_parse_input(input, &percentage, &background, &border, &bar);
if (result) {
return EXIT_FAILURE;
Expand All @@ -36,18 +36,25 @@ main(int argc, char **argv)
}

printf("running 4\n");
input = "25 #000000FF #FFFFFFFF #FFFFFFFF \n";
input = "25 000000FF FFFFFFFF FFFFFFFF \n";
result = wob_parse_input(input, &percentage, &background, &border, &bar);
if (result) {
return EXIT_FAILURE;
}

printf("running 5\n");
input = "25 #000000FF #16a085FF #FF0000FF\n";
input = "25 000000FF 16a085FF FF0000FF\n";
result = wob_parse_input(input, &percentage, &background, &border, &bar);
if (!result || percentage != 25 || wob_color_to_argb(background) != 0xFF000000 || wob_color_to_argb(border) != 0xFF16a085 || wob_color_to_argb(bar) != 0xFFFF0000) {
return EXIT_FAILURE;
}

printf("running 1\n");
input = "25 000000 FFFFFFFF FFFFFF\n";
result = wob_parse_input(input, &percentage, &background, &border, &bar);
if (!result || percentage != 25 || wob_color_to_argb(background) != 0xFF000000 || wob_color_to_argb(border) != 0xFFFFFFFF || wob_color_to_argb(bar) != 0xFFFFFFFF) {
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
4 changes: 2 additions & 2 deletions wob.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ wob reads values to display from standard input in the following formats:

or

<value> <#background_color> <#border_color> <#bar_color>
<value> <background_color> <border_color> <bar_color>

Where <value> is number in interval from 0 to *max* and <#\*color> is color in #RRGGBBAA format.
Where <value> is number in interval from 0 to *max* and <\*color> is color in RRGGBB[AA] format.

# CONFIGURATION

Expand Down
13 changes: 7 additions & 6 deletions wob.ini.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ You can run `wob -vv` to find the default values.
Anchor margin, in pixels.

*border_color*
Border color, in #RRGGBBAA format.
Border color, in RRGGBB[AA] format.

*background_color*
Background color, in #RRGGBBAA format.
Background color, in RRGGBB[AA] format.

*bar_color*
Bar color, in #RRGGBBAA format.
Bar color, in RRGGBB[AA] format.

*overflow_mode*
Overflow mode, one of *wrap*, and *nowrap*.
Expand All @@ -54,13 +54,13 @@ You can run `wob -vv` to find the default values.
*nowrap* values > *max* will be displayed as just *max*

*overflow_bar_color*
Overflow bar color, in #RRGGBBAA format.
Overflow bar color, in RRGGBB[AA] format.

*overflow_background_color*
Overflow background color, in #RRGGBBAA format.
Overflow background color, in RRGGBB[AA] format.

*overflow_border_color*
Overflow border color, in #RRGGBBAA format.
Overflow border color, in RRGGBB[AA] format.

*output_mode*
Output mode, one of *whitelist*, *all*, *focused*.
Expand Down Expand Up @@ -89,6 +89,7 @@ Replace *\** with user friendly name of your choosing.
timeout = 1000
max = 100
output_mode = whitelist
bar_color = FFFFFF
[output.left]
name = DP-1
Expand Down

0 comments on commit c1f4985

Please sign in to comment.