common/graphic-helpers: adopt lookup_named_color from xpm parser

This commit is contained in:
John Lindgren 2025-04-22 11:47:38 -04:00
parent 30248e1ba3
commit c4bae87c04
6 changed files with 33 additions and 29 deletions

View 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 */

View file

@ -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
*

View 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;
}

View 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.
*/

View file

@ -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)
{