st/icon-theme: Stop using GdkRGBA for coloring symbolics

We already have ClutterColor to represent a color, and StIconColors
to hold color information for symbolics from CSS. Now that we moved
GtkIconTheme in-tree, we can make use of those types instead of
translating back and forth from GdkRGBA.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2620>
This commit is contained in:
Florian Müllner 2023-01-25 19:29:25 +01:00 committed by Marge Bot
parent bf00a7957b
commit 69caefc541
2 changed files with 68 additions and 205 deletions

View File

@ -123,10 +123,7 @@ typedef struct _SymbolicPixbufCache SymbolicPixbufCache;
struct _SymbolicPixbufCache { struct _SymbolicPixbufCache {
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
GdkPixbuf *proxy_pixbuf; GdkPixbuf *proxy_pixbuf;
GdkRGBA fg; StIconColors *colors;
GdkRGBA success_color;
GdkRGBA warning_color;
GdkRGBA error_color;
SymbolicPixbufCache *next; SymbolicPixbufCache *next;
}; };
@ -1188,60 +1185,26 @@ remove_from_lru_cache (StIconTheme *icon_theme,
static SymbolicPixbufCache * static SymbolicPixbufCache *
symbolic_pixbuf_cache_new (GdkPixbuf *pixbuf, symbolic_pixbuf_cache_new (GdkPixbuf *pixbuf,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color,
SymbolicPixbufCache *next) SymbolicPixbufCache *next)
{ {
SymbolicPixbufCache *cache; SymbolicPixbufCache *cache;
cache = g_new0 (SymbolicPixbufCache, 1); cache = g_new0 (SymbolicPixbufCache, 1);
cache->pixbuf = g_object_ref (pixbuf); cache->pixbuf = g_object_ref (pixbuf);
if (fg) if (colors)
cache->fg = *fg; cache->colors = st_icon_colors_ref (colors);
if (success_color)
cache->success_color = *success_color;
if (warning_color)
cache->warning_color = *warning_color;
if (error_color)
cache->error_color = *error_color;
cache->next = next; cache->next = next;
return cache; return cache;
} }
static gboolean
rgba_matches (const GdkRGBA *a,
const GdkRGBA *b)
{
GdkRGBA transparent = { 0 };
/* For matching we treat unset colors as transparent rather
than default, which works as well, because transparent
will never be used for real symbolic icon colors */
if (a == NULL)
a = &transparent;
return
fabs(a->red - b->red) < 0.0001 &&
fabs(a->green - b->green) < 0.0001 &&
fabs(a->blue - b->blue) < 0.0001 &&
fabs(a->alpha - b->alpha) < 0.0001;
}
static SymbolicPixbufCache * static SymbolicPixbufCache *
symbolic_pixbuf_cache_matches (SymbolicPixbufCache *cache, symbolic_pixbuf_cache_matches (SymbolicPixbufCache *cache,
const GdkRGBA *fg, StIconColors *colors)
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color)
{ {
while (cache != NULL) while (cache != NULL)
{ {
if (rgba_matches (fg, &cache->fg) && if (st_icon_colors_equal (colors, cache->colors))
rgba_matches (success_color, &cache->success_color) &&
rgba_matches (warning_color, &cache->warning_color) &&
rgba_matches (error_color, &cache->error_color))
return cache; return cache;
cache = cache->next; cache = cache->next;
@ -1259,6 +1222,7 @@ symbolic_pixbuf_cache_free (SymbolicPixbufCache *cache)
{ {
next = cache->next; next = cache->next;
g_object_unref (cache->pixbuf); g_object_unref (cache->pixbuf);
g_clear_pointer (&cache->colors, st_icon_colors_unref);
g_free (cache); g_free (cache);
cache = next; cache = next;
@ -3691,32 +3655,27 @@ symbolic_cache_get_proxy (SymbolicPixbufCache *symbolic_cache,
} }
static char * static char *
rgba_to_string_noalpha (const GdkRGBA *rgba) color_to_string_noalpha (const ClutterColor *color)
{ {
GdkRGBA color; return g_strdup_printf ("rgb(%d,%d,%d)",
CLAMP (color->red, 0, 255),
color = *rgba; CLAMP (color->green, 0, 255),
color.alpha = 1.0; CLAMP (color->blue, 0, 255));
return gdk_rgba_to_string (&color);
} }
static void static void
rgba_to_pixel(const GdkRGBA *rgba, color_to_pixel(const ClutterColor *color,
uint8_t pixel[4]) uint8_t pixel[4])
{ {
pixel[0] = rgba->red * 255; pixel[0] = color->red;
pixel[1] = rgba->green * 255; pixel[1] = color->green;
pixel[2] = rgba->blue * 255; pixel[2] = color->blue;
pixel[3] = 255; pixel[3] = 255;
} }
static GdkPixbuf * static GdkPixbuf *
color_symbolic_pixbuf (GdkPixbuf *symbolic, color_symbolic_pixbuf (GdkPixbuf *symbolic,
const GdkRGBA *fg_color, StIconColors *colors)
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color)
{ {
int width, height, x, y, src_stride, dst_stride; int width, height, x, y, src_stride, dst_stride;
guchar *src_data, *dst_data; guchar *src_data, *dst_data;
@ -3725,12 +3684,12 @@ color_symbolic_pixbuf (GdkPixbuf *symbolic,
GdkPixbuf *colored; GdkPixbuf *colored;
uint8_t fg_pixel[4], success_pixel[4], warning_pixel[4], error_pixel[4]; uint8_t fg_pixel[4], success_pixel[4], warning_pixel[4], error_pixel[4];
alpha = fg_color->alpha * 255; alpha = colors->foreground.alpha;
rgba_to_pixel (fg_color, fg_pixel); color_to_pixel (&colors->foreground, fg_pixel);
rgba_to_pixel (success_color, success_pixel); color_to_pixel (&colors->success, success_pixel);
rgba_to_pixel (warning_color, warning_pixel); color_to_pixel (&colors->warning, warning_pixel);
rgba_to_pixel (error_color, error_pixel); color_to_pixel (&colors->error, error_pixel);
width = gdk_pixbuf_get_width (symbolic); width = gdk_pixbuf_get_width (symbolic);
height = gdk_pixbuf_get_height (symbolic); height = gdk_pixbuf_get_height (symbolic);
@ -3796,18 +3755,10 @@ color_symbolic_pixbuf (GdkPixbuf *symbolic,
} }
static GdkPixbuf * static GdkPixbuf *
st_icon_info_load_symbolic_png (StIconInfo *icon_info, st_icon_info_load_symbolic_png (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color, GError **error)
const GdkRGBA *warning_color,
const GdkRGBA *error_color,
GError **error)
{ {
GdkRGBA fg_default = { 0.7450980392156863, 0.7450980392156863, 0.7450980392156863, 1.0};
GdkRGBA success_default = { 0.3046921492332342,0.6015716792553597, 0.023437857633325704, 1.0};
GdkRGBA warning_default = {0.9570458533607996, 0.47266346227206835, 0.2421911955443656, 1.0 };
GdkRGBA error_default = { 0.796887159533074, 0 ,0, 1.0 };
if (!icon_info_ensure_scale_and_pixbuf (icon_info)) if (!icon_info_ensure_scale_and_pixbuf (icon_info))
{ {
if (icon_info->load_error) if (icon_info->load_error)
@ -3826,20 +3777,13 @@ st_icon_info_load_symbolic_png (StIconInfo *icon_info,
return NULL; return NULL;
} }
return color_symbolic_pixbuf (icon_info->pixbuf, return color_symbolic_pixbuf (icon_info->pixbuf, colors);
fg ? fg : &fg_default,
success_color ? success_color : &success_default,
warning_color ? warning_color : &warning_default,
error_color ? error_color : &error_default);
} }
static GdkPixbuf * static GdkPixbuf *
st_icon_info_load_symbolic_svg (StIconInfo *icon_info, st_icon_info_load_symbolic_svg (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color, GError **error)
const GdkRGBA *warning_color,
const GdkRGBA *error_color,
GError **error)
{ {
GInputStream *stream; GInputStream *stream;
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
@ -3857,26 +3801,13 @@ st_icon_info_load_symbolic_svg (StIconInfo *icon_info,
double alpha; double alpha;
char alphastr[G_ASCII_DTOSTR_BUF_SIZE]; char alphastr[G_ASCII_DTOSTR_BUF_SIZE];
alpha = fg->alpha; alpha = colors->foreground.alpha;
css_fg = rgba_to_string_noalpha (fg); css_fg = color_to_string_noalpha (&colors->foreground);
css_success = css_warning = css_error = NULL; css_warning = color_to_string_noalpha (&colors->warning);
css_error = color_to_string_noalpha (&colors->error);
if (warning_color) css_success = color_to_string_noalpha (&colors->success);
css_warning = rgba_to_string_noalpha (warning_color);
else
css_warning = g_strdup ("rgb(245,121,62)");
if (error_color)
css_error = rgba_to_string_noalpha (error_color);
else
css_error = g_strdup ("rgb(204,0,0)");
if (success_color)
css_success = rgba_to_string_noalpha (success_color);
else
css_success = g_strdup ("rgb(78,154,6)");
if (!g_file_load_contents (icon_info->icon_file, NULL, &file_data, &file_len, NULL, error)) if (!g_file_load_contents (icon_info->icon_file, NULL, &file_data, &file_len, NULL, error))
return NULL; return NULL;
@ -3961,12 +3892,9 @@ st_icon_info_load_symbolic_svg (StIconInfo *icon_info,
static GdkPixbuf * static GdkPixbuf *
st_icon_info_load_symbolic_internal (StIconInfo *icon_info, st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color, gboolean use_cache,
const GdkRGBA *warning_color, GError **error)
const GdkRGBA *error_color,
gboolean use_cache,
GError **error)
{ {
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
SymbolicPixbufCache *symbolic_cache; SymbolicPixbufCache *symbolic_cache;
@ -3974,8 +3902,7 @@ st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
if (use_cache) if (use_cache)
{ {
symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache, symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache, colors);
fg, success_color, warning_color, error_color);
if (symbolic_cache) if (symbolic_cache)
return symbolic_cache_get_proxy (symbolic_cache, icon_info); return symbolic_cache_get_proxy (symbolic_cache, icon_info);
} }
@ -3983,13 +3910,13 @@ st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
/* css_fg can't possibly have failed, otherwise /* css_fg can't possibly have failed, otherwise
* that would mean we have a broken style * that would mean we have a broken style
*/ */
g_return_val_if_fail (fg != NULL, NULL); g_return_val_if_fail (colors != NULL, NULL);
icon_uri = g_file_get_uri (icon_info->icon_file); icon_uri = g_file_get_uri (icon_info->icon_file);
if (g_str_has_suffix (icon_uri, ".symbolic.png")) if (g_str_has_suffix (icon_uri, ".symbolic.png"))
pixbuf = st_icon_info_load_symbolic_png (icon_info, fg, success_color, warning_color, error_color, error); pixbuf = st_icon_info_load_symbolic_png (icon_info, colors, error);
else else
pixbuf = st_icon_info_load_symbolic_svg (icon_info, fg, success_color, warning_color, error_color, error); pixbuf = st_icon_info_load_symbolic_svg (icon_info, colors, error);
if (pixbuf != NULL) if (pixbuf != NULL)
{ {
@ -4005,8 +3932,7 @@ st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
if (use_cache) if (use_cache)
{ {
icon_info->symbolic_pixbuf_cache = icon_info->symbolic_pixbuf_cache =
symbolic_pixbuf_cache_new (pixbuf, fg, success_color, warning_color, error_color, symbolic_pixbuf_cache_new (pixbuf, colors, icon_info->symbolic_pixbuf_cache);
icon_info->symbolic_pixbuf_cache);
g_object_unref (pixbuf); g_object_unref (pixbuf);
return symbolic_cache_get_proxy (icon_info->symbolic_pixbuf_cache, icon_info); return symbolic_cache_get_proxy (icon_info->symbolic_pixbuf_cache, icon_info);
} }
@ -4020,13 +3946,7 @@ st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
/** /**
* st_icon_info_load_symbolic: * st_icon_info_load_symbolic:
* @icon_info: a #StIconInfo * @icon_info: a #StIconInfo
* @fg: a #GdkRGBA representing the foreground color of the icon * @colors: a #StIconColors representing the foreground, warning and error colors
* @success_color: (allow-none): a #GdkRGBA representing the warning color
* of the icon or %NULL to use the default color
* @warning_color: (allow-none): a #GdkRGBA representing the warning color
* of the icon or %NULL to use the default color
* @error_color: (allow-none): a #GdkRGBA representing the error color
* of the icon or %NULL to use the default color (allow-none)
* @was_symbolic: (out) (allow-none): a #gboolean, returns whether the * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
* loaded icon was a symbolic one and whether the @fg color was * loaded icon was a symbolic one and whether the @fg color was
* applied to it. * applied to it.
@ -4053,18 +3973,15 @@ st_icon_info_load_symbolic_internal (StIconInfo *icon_info,
* Returns: (transfer full): a #GdkPixbuf representing the loaded icon * Returns: (transfer full): a #GdkPixbuf representing the loaded icon
*/ */
GdkPixbuf * GdkPixbuf *
st_icon_info_load_symbolic (StIconInfo *icon_info, st_icon_info_load_symbolic (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color, gboolean *was_symbolic,
const GdkRGBA *warning_color, GError **error)
const GdkRGBA *error_color,
gboolean *was_symbolic,
GError **error)
{ {
gboolean is_symbolic; gboolean is_symbolic;
g_return_val_if_fail (icon_info != NULL, NULL); g_return_val_if_fail (icon_info != NULL, NULL);
g_return_val_if_fail (fg != NULL, NULL); g_return_val_if_fail (colors != NULL, NULL);
is_symbolic = st_icon_info_is_symbolic (icon_info); is_symbolic = st_icon_info_is_symbolic (icon_info);
@ -4075,8 +3992,7 @@ st_icon_info_load_symbolic (StIconInfo *icon_info,
return st_icon_info_load_icon (icon_info, error); return st_icon_info_load_icon (icon_info, error);
return st_icon_info_load_symbolic_internal (icon_info, return st_icon_info_load_symbolic_internal (icon_info,
fg, success_color, colors,
warning_color, error_color,
TRUE, TRUE,
error); error);
} }
@ -4084,14 +4000,7 @@ st_icon_info_load_symbolic (StIconInfo *icon_info,
typedef struct { typedef struct {
gboolean is_symbolic; gboolean is_symbolic;
StIconInfo *dup; StIconInfo *dup;
GdkRGBA fg; StIconColors *colors;
gboolean fg_set;
GdkRGBA success_color;
gboolean success_color_set;
GdkRGBA warning_color;
gboolean warning_color_set;
GdkRGBA error_color;
gboolean error_color_set;
} AsyncSymbolicData; } AsyncSymbolicData;
static void static void
@ -4099,6 +4008,7 @@ async_symbolic_data_free (AsyncSymbolicData *data)
{ {
if (data->dup) if (data->dup)
g_object_unref (data->dup); g_object_unref (data->dup);
g_clear_pointer (&data->colors, st_icon_colors_unref);
g_free (data); g_free (data);
} }
@ -4132,10 +4042,7 @@ load_symbolic_icon_thread (GTask *task,
error = NULL; error = NULL;
pixbuf = st_icon_info_load_symbolic_internal (data->dup, pixbuf = st_icon_info_load_symbolic_internal (data->dup,
data->fg_set ? &data->fg : NULL, data->colors,
data->success_color_set ? &data->success_color : NULL,
data->warning_color_set ? &data->warning_color : NULL,
data->error_color_set ? &data->error_color : NULL,
FALSE, FALSE,
&error); &error);
if (pixbuf == NULL) if (pixbuf == NULL)
@ -4147,13 +4054,8 @@ load_symbolic_icon_thread (GTask *task,
/** /**
* st_icon_info_load_symbolic_async: * st_icon_info_load_symbolic_async:
* @icon_info: a #StIconInfo from st_icon_theme_lookup_icon() * @icon_info: a #StIconInfo from st_icon_theme_lookup_icon()
* @fg: a #GdkRGBA representing the foreground color of the icon * @colors: an #StIconColors representing the foreground, error and
* @success_color: (allow-none): a #GdkRGBA representing the warning color * success colors of the icon
* of the icon or %NULL to use the default color
* @warning_color: (allow-none): a #GdkRGBA representing the warning color
* of the icon or %NULL to use the default color
* @error_color: (allow-none): a #GdkRGBA representing the error color
* of the icon or %NULL to use the default color (allow-none)
* @cancellable: (allow-none): optional #GCancellable object, * @cancellable: (allow-none): optional #GCancellable object,
* %NULL to ignore * %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call when the * @callback: (scope async): a #GAsyncReadyCallback to call when the
@ -4168,10 +4070,7 @@ load_symbolic_icon_thread (GTask *task,
*/ */
void void
st_icon_info_load_symbolic_async (StIconInfo *icon_info, st_icon_info_load_symbolic_async (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
@ -4182,7 +4081,7 @@ st_icon_info_load_symbolic_async (StIconInfo *icon_info,
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
g_return_if_fail (icon_info != NULL); g_return_if_fail (icon_info != NULL);
g_return_if_fail (fg != NULL); g_return_if_fail (colors != NULL);
task = g_task_new (icon_info, cancellable, callback, user_data); task = g_task_new (icon_info, cancellable, callback, user_data);
@ -4197,8 +4096,7 @@ st_icon_info_load_symbolic_async (StIconInfo *icon_info,
} }
else else
{ {
symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache, symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache, colors);
fg, success_color, warning_color, error_color);
if (symbolic_cache) if (symbolic_cache)
{ {
pixbuf = symbolic_cache_get_proxy (symbolic_cache, icon_info); pixbuf = symbolic_cache_get_proxy (symbolic_cache, icon_info);
@ -4206,31 +4104,8 @@ st_icon_info_load_symbolic_async (StIconInfo *icon_info,
} }
else else
{ {
if (fg)
{
data->fg = *fg;
data->fg_set = TRUE;
}
if (success_color)
{
data->success_color = *success_color;
data->success_color_set = TRUE;
}
if (warning_color)
{
data->warning_color = *warning_color;
data->warning_color_set = TRUE;
}
if (error_color)
{
data->error_color = *error_color;
data->error_color_set = TRUE;
}
data->dup = icon_info_dup (icon_info); data->dup = icon_info_dup (icon_info);
data->colors = st_icon_colors_ref (colors);
g_task_run_in_thread (task, load_symbolic_icon_thread); g_task_run_in_thread (task, load_symbolic_icon_thread);
} }
} }
@ -4275,19 +4150,13 @@ st_icon_info_load_symbolic_finish (StIconInfo *icon_info,
g_assert (pixbuf != NULL); /* we checked for !had_error above */ g_assert (pixbuf != NULL); /* we checked for !had_error above */
symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache, symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache,
data->fg_set ? &data->fg : NULL, data->colors);
data->success_color_set ? &data->success_color : NULL,
data->warning_color_set ? &data->warning_color : NULL,
data->error_color_set ? &data->error_color : NULL);
if (symbolic_cache == NULL) if (symbolic_cache == NULL)
{ {
symbolic_cache = icon_info->symbolic_pixbuf_cache = symbolic_cache = icon_info->symbolic_pixbuf_cache =
symbolic_pixbuf_cache_new (pixbuf, symbolic_pixbuf_cache_new (pixbuf,
data->fg_set ? &data->fg : NULL, data->colors,
data->success_color_set ? &data->success_color : NULL,
data->warning_color_set ? &data->warning_color : NULL,
data->error_color_set ? &data->error_color : NULL,
icon_info->symbolic_pixbuf_cache); icon_info->symbolic_pixbuf_cache);
} }

View File

@ -22,7 +22,7 @@
#pragma once #pragma once
#include <gdk-pixbuf/gdk-pixbuf.h> #include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdk.h> #include "st-icon-colors.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -191,19 +191,13 @@ GdkPixbuf * st_icon_info_load_icon_finish (StIconInfo *icon_info,
GAsyncResult *res, GAsyncResult *res,
GError **error); GError **error);
GdkPixbuf * st_icon_info_load_symbolic (StIconInfo *icon_info, GdkPixbuf * st_icon_info_load_symbolic (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color, gboolean *was_symbolic,
const GdkRGBA *warning_color, GError **error);
const GdkRGBA *error_color,
gboolean *was_symbolic,
GError **error);
void st_icon_info_load_symbolic_async (StIconInfo *icon_info, void st_icon_info_load_symbolic_async (StIconInfo *icon_info,
const GdkRGBA *fg, StIconColors *colors,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);