From d5549db3847598e696f8a5d424986a19c8e67499 Mon Sep 17 00:00:00 2001 From: QC Date: Sat, 10 Oct 2015 13:43:28 +0200 Subject: [PATCH] Strip color, should fix #233 --- source/textbox.c | 36 ++++++------------------------------ source/x11-helper.c | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/source/textbox.c b/source/textbox.c index 4230d8a8..15ce45a6 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -574,38 +574,14 @@ int textbox_keypress ( textbox *tb, XIC xic, XEvent *ev ) /*** * Font setup. */ -extern Colormap map; static void parse_color ( Display *display, char *bg, Color *col ) { - unsigned int val = 0; - char *endptr = NULL; - if ( bg == NULL ) { - return; - } - if ( strncmp ( bg, "argb:", 5 ) == 0 ) { - val = strtoul ( &bg[5], &endptr, 16 ); - col->alpha = ( ( val & 0xFF000000 ) >> 24 ) / 256.0; - col->red = ( ( val & 0x00FF0000 ) >> 16 ) / 256.0; - col->green = ( ( val & 0x0000FF00 ) >> 8 ) / 256.0; - col->blue = ( ( val & 0x000000FF ) ) / 256.0; - } - else if ( bg[0] == '#' ) { - val = strtoul ( &bg[1], &endptr, 16 ); - col->alpha = 1; - col->red = ( ( val & 0x00FF0000 ) >> 16 ) / 256.0; - col->green = ( ( val & 0x0000FF00 ) >> 8 ) / 256.0; - col->blue = ( ( val & 0x000000FF ) ) / 256.0; - } - else { - XColor def, color = { 0, 0, 0, 0, 0, 0 }; - Status st = XAllocNamedColor ( display, map, bg, &color, &def ); - if ( st != None ) { - col->alpha = 1; - col->red = ( ( def.pixel & 0x00FF0000 ) >> 16 ) / 256.0; - col->green = ( ( def.pixel & 0x0000FF00 ) >> 8 ) / 256.0; - col->blue = ( ( def.pixel & 0x000000FF ) ) / 256.0; - } - } + unsigned int val = 0; + val = color_get ( display, bg, "white" ); + col->alpha = ( ( val & 0xFF000000 ) >> 24 ) / 256.0; + col->red = ( ( val & 0x00FF0000 ) >> 16 ) / 256.0; + col->green = ( ( val & 0x0000FF00 ) >> 8 ) / 256.0; + col->blue = ( ( val & 0x000000FF ) ) / 256.0; } static void textbox_parse_string ( Display *display, const char *str, RowColor *color ) { diff --git a/source/x11-helper.c b/source/x11-helper.c index b762b3fb..1a8c8dfe 100644 --- a/source/x11-helper.c +++ b/source/x11-helper.c @@ -500,11 +500,13 @@ cairo_format_t get_format ( void ) unsigned int color_get ( Display *display, const char *const name, const char * const defn ) { - XColor color = { 0, 0, 0, 0, 0, 0 }; + char *copy = g_strdup ( name ); + char *cname = g_strstrip ( copy ); + XColor color = { 0, 0, 0, 0, 0, 0 }; XColor def; // Special format. - if ( strncmp ( name, "argb:", 5 ) == 0 ) { - color.pixel = strtoul ( &name[5], NULL, 16 ); + if ( strncmp ( cname, "argb:", 5 ) == 0 ) { + color.pixel = strtoul ( &cname[5], NULL, 16 ); color.red = ( ( color.pixel & 0x00FF0000 ) >> 16 ) * 256; color.green = ( ( color.pixel & 0x0000FF00 ) >> 8 ) * 256; color.blue = ( ( color.pixel & 0x000000FF ) ) * 256; @@ -512,29 +514,28 @@ unsigned int color_get ( Display *display, const char *const name, const char * // This will drop alpha part. Status st = XAllocColor ( display, map, &color ); if ( st == None ) { - fprintf ( stderr, "Failed to parse color: '%s'\n", name ); + fprintf ( stderr, "Failed to parse color: '%s'\n", cname ); st = XAllocNamedColor ( display, map, defn, &color, &def ); if ( st == None ) { fprintf ( stderr, "Failed to allocate fallback color\n" ); exit ( EXIT_FAILURE ); } } - return color.pixel; } - return color.pixel; } else { - Status st = XAllocNamedColor ( display, map, name, &color, &def ); + Status st = XAllocNamedColor ( display, map, cname, &color, &def ); if ( st == None ) { - fprintf ( stderr, "Failed to parse color: '%s'\n", name ); + fprintf ( stderr, "Failed to parse color: '%s'\n", cname ); st = XAllocNamedColor ( display, map, defn, &color, &def ); if ( st == None ) { fprintf ( stderr, "Failed to allocate fallback color\n" ); exit ( EXIT_FAILURE ); } } - return color.pixel; } + g_free ( copy ); + return color.pixel; } void x11_helper_set_cairo_rgba ( cairo_t *d, unsigned int pixel ) { @@ -554,7 +555,7 @@ void color_background ( Display *display, cairo_t *d ) else { gchar **vals = g_strsplit ( config.color_window, ",", 3 ); if ( vals != NULL && vals[0] != NULL ) { - pixel = color_get ( display, g_strstrip ( vals[0] ), "black" ); + pixel = color_get ( display, vals[0], "black" ); } g_strfreev ( vals ); }