[gnome-software] Always use the specified 0..1 scale when adding GdkRGBA key colors



commit a3287793f03ec969a50cfca3a2fe778b7647fa19
Author: Richard Hughes <richard hughsie com>
Date:   Fri Jun 24 14:44:53 2016 +0100

    Always use the specified 0..1 scale when adding GdkRGBA key colors
    
    The key-colors plugin was returning values 0..255 which is fine for CSS, but
    the GTK docs say that the value has to be 1.0 -- which we were already doing
    for the custom categories color anyway.

 src/gs-common.c                    |    6 +++---
 src/gs-self-test.c                 |   18 +++++++++++++++++-
 src/plugins/gs-plugin-key-colors.c |   21 ++++++++++++++-------
 3 files changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index c6b9422..ac5047e 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -720,9 +720,9 @@ gs_utils_widget_set_css_app (GsApp *app,
                g_autofree gchar *value = NULL;
                key = g_strdup_printf ("@keycolor-%02i@", i);
                value = g_strdup_printf ("rgb(%.0f,%.0f,%.0f)",
-                                        color->red,
-                                        color->green,
-                                        color->blue);
+                                        color->red * 255.f,
+                                        color->green * 255.f,
+                                        color->blue * 255.f);
                as_utils_string_replace (css_str, key, value);
        }
 
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index fefd69f..f31947c 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -332,7 +332,9 @@ gs_plugin_loader_refine_func (GsPluginLoader *plugin_loader)
 static void
 gs_plugin_loader_key_colors_func (GsPluginLoader *plugin_loader)
 {
+       GPtrArray *array;
        gboolean ret;
+       guint i;
        g_autoptr(GsApp) app = NULL;
        g_autoptr(GError) error = NULL;
 
@@ -344,7 +346,21 @@ gs_plugin_loader_key_colors_func (GsPluginLoader *plugin_loader)
                                           &error);
        g_assert_no_error (error);
        g_assert (ret);
-       g_assert_cmpint (gs_app_get_key_colors(app)->len, >=, 3);
+       array = gs_app_get_key_colors (app);
+       g_assert_cmpint (array->len, >=, 3);
+
+       /* check values are in range */
+       for (i = 0; i < array->len; i++) {
+               GdkRGBA *kc = g_ptr_array_index (array, i);
+               g_assert_cmpfloat (kc->red, >=, 0.f);
+               g_assert_cmpfloat (kc->red, <=, 1.f);
+               g_assert_cmpfloat (kc->green, >=, 0.f);
+               g_assert_cmpfloat (kc->green, <=, 1.f);
+               g_assert_cmpfloat (kc->blue, >=, 0.f);
+               g_assert_cmpfloat (kc->blue, <=, 1.f);
+               g_assert_cmpfloat (kc->alpha, >=, 0.f);
+               g_assert_cmpfloat (kc->alpha, <=, 1.f);
+       }
 }
 
 static void
diff --git a/src/plugins/gs-plugin-key-colors.c b/src/plugins/gs-plugin-key-colors.c
index bb2ad9d..af17913 100644
--- a/src/plugins/gs-plugin-key-colors.c
+++ b/src/plugins/gs-plugin-key-colors.c
@@ -61,6 +61,13 @@ gs_color_bin_sort_cb (gconstpointer a, gconstpointer b)
        return 0;
 }
 
+/* convert range of 0..255 to 0..1 */
+static gdouble
+_convert_from_rgb8 (guchar val)
+{
+       return (gdouble) val / 255.f;
+}
+
 static void
 gs_plugin_key_colors_set_for_pixbuf (GsApp *app, GdkPixbuf *pb, guint number)
 {
@@ -99,18 +106,18 @@ gs_plugin_key_colors_set_for_pixbuf (GsApp *app, GdkPixbuf *pb, guint number)
                                key = GUINT_TO_POINTER (cd_color_rgb8_to_uint32 (&tmp));
                                s = g_hash_table_lookup (hash, key);
                                if (s != NULL) {
-                                       s->color.red += p[0];
-                                       s->color.green += p[1];
-                                       s->color.blue += p[2];
+                                       s->color.red += _convert_from_rgb8 (p[0]);
+                                       s->color.green += _convert_from_rgb8 (p[1]);
+                                       s->color.blue += _convert_from_rgb8 (p[2]);
                                        s->cnt++;
                                        continue;
                                }
 
                                /* add to hash table */
                                s = g_new0 (GsColorBin, 1);
-                               s->color.red = p[0];
-                               s->color.green = p[1];
-                               s->color.blue = p[2];
+                               s->color.red = _convert_from_rgb8 (p[0]);
+                               s->color.green = _convert_from_rgb8 (p[1]);
+                               s->color.blue = _convert_from_rgb8 (p[2]);
                                s->color.alpha = 1.0;
                                s->cnt = 1;
                                g_hash_table_insert (hash, key, s);
@@ -140,7 +147,7 @@ gs_plugin_key_colors_set_for_pixbuf (GsApp *app, GdkPixbuf *pb, guint number)
        /* the algorithm failed, so just return a monochrome ramp */
        for (i = 0; i < 3; i++) {
                g_autofree GdkRGBA *color = g_new0 (GdkRGBA, 1);
-               color->red = 255 * i / 3;
+               color->red = (gdouble) i / 3.f;
                color->green = color->red;
                color->blue = color->red;
                color->alpha = 1.0f;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]