add color change if argv[1] is a color in #RRGGBB format master
authorgit <redacted>
Fri, 3 Jul 2026 08:42:35 +0000 (04:42 -0400)
committergit <redacted>
Fri, 3 Jul 2026 08:42:35 +0000 (04:42 -0400)
herbe.c

diff --git a/herbe.c b/herbe.c
index 5b33582f58e9e6cd09ef13cbe202e1dd5bcc05bb..be2411fb11b8a01c4fb22fa5bcc236f7b03136e9 100644 (file)
--- a/herbe.c
+++ b/herbe.c
@@ -1,5 +1,6 @@
 #include <X11/Xlib.h>
 #include <X11/Xft/Xft.h>
+#include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
@@ -52,6 +53,24 @@ char *read_stdin_walloc(void)
        return buff;
 }
 
+char *invert_color(const char *color)
+{
+       char c;
+       static char inverted[8];
+
+       strcpy(inverted, color);
+       for (int i = 1; i < 7; i++)
+       {
+               c = color[i];
+                       inverted[i] = (c < 'a') ?
+                       "0123456789ABCDEF"[15 - (c - '0' - (c > '9' ? 7 : 0))] :
+                       "0123456789abcdef"[15 - (c - 'a')];
+       }
+
+       inverted[7] = '\0';
+       return inverted;
+}
+
 int get_max_len(char *string, XftFont *font, int max_text_width)
 {
        int eol = strlen(string);
@@ -104,9 +123,12 @@ void expire(int sig)
 
 int main(int argc, char *argv[])
 {
-       int nargc = 0;
+       int nargc = 0, rstatus;
+       regex_t regex;
+       regmatch_t match;
        char **nargv = NULL;
        char *body = NULL, *bptr;
+       const char *pattern = "#[a-fA-F0-9]{6}";
 
        if (!isatty(STDIN_FILENO))
        {
@@ -155,6 +177,16 @@ int main(int argc, char *argv[])
 
        struct sigaction act_expire, act_ignore;
 
+       if ((rstatus = regcomp(&regex, pattern, REG_EXTENDED)))
+               die("%s: regex compilation failed", argv[0]);
+
+       if (!(rstatus = regexec(&regex, argv[1], 1, &match, 0)))
+       {
+               background_color = argv[1];
+               font_color = invert_color(argv[1]);
+               border_color = font_color;
+       }
+
        act_expire.sa_handler = expire;
        act_expire.sa_flags = SA_RESTART;
        sigemptyset(&act_expire.sa_mask);
@@ -281,6 +313,7 @@ int main(int argc, char *argv[])
        if (body)
                free(body);
        free(lines);
+       regfree(&regex);
        XftDrawDestroy(draw);
        XftColorFree(display, visual, colormap, &color);
        XftFontClose(display, font);