From c4bae87c04c2bf0a4b5ab1b1d9a316af5b44ce05 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Tue, 22 Apr 2025 11:47:38 -0400 Subject: [PATCH] common/graphic-helpers: adopt lookup_named_color from xpm parser --- include/common/graphic-helpers.h | 3 ++- src/{img => common}/gen-color-table.pl | 6 ++--- src/common/graphic-helpers.c | 25 +++++++++++++++++++ src/{img => common}/rgb.txt | 0 .../xcolor-table.h} | 4 +-- src/img/img-xpm.c | 24 +----------------- 6 files changed, 33 insertions(+), 29 deletions(-) rename src/{img => common}/gen-color-table.pl (86%) rename src/{img => common}/rgb.txt (100%) rename src/{img/xpm-color-table.h => common/xcolor-table.h} (99%) diff --git a/include/common/graphic-helpers.h b/include/common/graphic-helpers.h index e1986d4e..1b751b25 100644 --- a/include/common/graphic-helpers.h +++ b/include/common/graphic-helpers.h @@ -15,6 +15,7 @@ void set_cairo_color(cairo_t *cairo, const float *color); /* Draws a border with a specified line width */ void draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width); -struct lab_data_buffer; +/* Converts X11 color name to ARGB32 (with alpha = 255) */ +bool lookup_named_color(const char *name, uint32_t *argb); #endif /* LABWC_GRAPHIC_HELPERS_H */ diff --git a/src/img/gen-color-table.pl b/src/common/gen-color-table.pl similarity index 86% rename from src/img/gen-color-table.pl rename to src/common/gen-color-table.pl index e6ffa49a..91ccddea 100755 --- a/src/img/gen-color-table.pl +++ b/src/common/gen-color-table.pl @@ -1,10 +1,10 @@ #!/usr/bin/perl -w -# Generates xpm-color-table.h from X11's rgb.txt +# Generates xcolor-table.h from X11's rgb.txt # Adapted from gdk-pixbuf (LGPL-2.0-or-later) if (@ARGV != 1) { - die "Usage: gen-color-table.pl rgb.txt > xpm-color-table.h\n"; + die "Usage: gen-color-table.pl rgb.txt > xcolor-table.h\n"; } open IN, '<', $ARGV[0] or die "Cannot open $ARGV[0]: $!\n"; @@ -29,7 +29,7 @@ $date = gmtime; print < +#include /* g_ascii_strcasecmp */ #include #include "common/graphic-helpers.h" +#include "common/macros.h" +#include "xcolor-table.h" /* Draws a border with a specified line width */ void @@ -40,3 +43,25 @@ set_cairo_color(cairo_t *cairo, const float *c) cairo_set_source_rgba(cairo, c[0] / alpha, c[1] / alpha, c[2] / alpha, alpha); } + +static int +compare_xcolor_entry(const void *a, const void *b) +{ + /* using ASCII version to avoid locale-dependent ordering */ + return g_ascii_strcasecmp((const char *)a, + color_names + ((const struct xcolor_entry *)b)->name_offset); +} + +bool +lookup_named_color(const char *name, uint32_t *argb) +{ + struct xcolor_entry *found = bsearch(name, xcolors, ARRAY_SIZE(xcolors), + sizeof(struct xcolor_entry), compare_xcolor_entry); + if (!found) { + return false; + } + + *argb = 0xFF000000u | ((uint32_t)found->red << 16) + | ((uint32_t)found->green << 8) | found->blue; + return true; +} diff --git a/src/img/rgb.txt b/src/common/rgb.txt similarity index 100% rename from src/img/rgb.txt rename to src/common/rgb.txt diff --git a/src/img/xpm-color-table.h b/src/common/xcolor-table.h similarity index 99% rename from src/img/xpm-color-table.h rename to src/common/xcolor-table.h index cf3bfc1d..c0db0ca6 100644 --- a/src/img/xpm-color-table.h +++ b/src/common/xcolor-table.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.0-or-later */ -/* xpm-color-table.h: Generated by gen-color-table.pl from rgb.txt +/* xcolor-table.h: Generated by gen-color-table.pl from rgb.txt * - * Date: Mon Sep 30 09:04:01 2024 + * Date: Tue Apr 22 14:56:18 2025 * * Do not edit. */ diff --git a/src/img/img-xpm.c b/src/img/img-xpm.c index beb025bc..ad50bc5e 100644 --- a/src/img/img-xpm.c +++ b/src/img/img-xpm.c @@ -19,12 +19,10 @@ #include "buffer.h" #include "common/buf.h" -#include "common/macros.h" +#include "common/graphic-helpers.h" #include "common/mem.h" #include "img/img-xpm.h" -#include "xpm-color-table.h" - enum buf_op { op_header, op_cmap, op_body }; struct xpm_color { @@ -43,26 +41,6 @@ make_argb(uint8_t a, uint8_t r, uint8_t g, uint8_t b) return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; } -static int -compare_xcolor_entries(const void *a, const void *b) -{ - return g_ascii_strcasecmp((const char *)a, - color_names + ((const struct xcolor_entry *)b)->name_offset); -} - -static bool -lookup_named_color(const char *name, uint32_t *argb) -{ - struct xcolor_entry *found = bsearch(name, xcolors, ARRAY_SIZE(xcolors), - sizeof(struct xcolor_entry), compare_xcolor_entries); - if (!found) { - return false; - } - - *argb = make_argb(0xFF, found->red, found->green, found->blue); - return true; -} - static bool parse_color(const char *spec, uint32_t *argb) {