]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/graphic-helpers: adopt lookup_named_color from xpm parser
authorJohn Lindgren <john@jlindgren.net>
Tue, 22 Apr 2025 15:47:38 +0000 (11:47 -0400)
committerJohn Lindgren <john@jlindgren.net>
Wed, 18 Jun 2025 19:48:24 +0000 (15:48 -0400)
include/common/graphic-helpers.h
src/common/gen-color-table.pl [moved from src/img/gen-color-table.pl with 86% similarity]
src/common/graphic-helpers.c
src/common/rgb.txt [moved from src/img/rgb.txt with 100% similarity]
src/common/xcolor-table.h [moved from src/img/xpm-color-table.h with 99% similarity]
src/img/img-xpm.c

index e1986d4ec0701373efcb3077ed4f38868ffe221b..1b751b2516a3664de2e873d68a0a52d534635b43 100644 (file)
@@ -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 */
similarity index 86%
rename from src/img/gen-color-table.pl
rename to src/common/gen-color-table.pl
index e6ffa49aa395cc7b00cb2d80940198d91e5f0736..91ccddead2764fd94ad9dd9adfcdb78061445912 100755 (executable)
@@ -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 <<EOT;
 /* 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: $date
  *
index 29ba8ddbcbd2534f158fa4511b4e6eaa249e3cb5..08817cb50559ec7cbe585ac423e6d2ea4fd9df4d 100644 (file)
@@ -1,8 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <cairo.h>
+#include <glib.h> /* g_ascii_strcasecmp */
 #include <wlr/types/wlr_scene.h>
 #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;
+}
similarity index 100%
rename from src/img/rgb.txt
rename to src/common/rgb.txt
similarity index 99%
rename from src/img/xpm-color-table.h
rename to src/common/xcolor-table.h
index cf3bfc1dfebadce8fc56a8b3307632a4a2d6d8f8..c0db0ca6acf152d171d22a6f95dd70d0bacc1225 100644 (file)
@@ -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.
  */
index beb025bc36c8e26f2a1b726f5c8e16989063b610..ad50bc5eac3b4b1aa1f4fd278fadec42c3c777f7 100644 (file)
 
 #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)
 {