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

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