[yelp/new-aday-design] yelp-settings: Port color code to GtkStyle



commit b6ac5341ff28e7eff4030596af86ea2a8949a6c2
Author: Shaun McCance <shaunm gnome org>
Date:   Tue Jun 17 18:44:31 2014 -0400

    yelp-settings: Port color code to GtkStyle

 libyelp/yelp-settings.c |  137 ++++++++++++++++-------------------------------
 1 files changed, 47 insertions(+), 90 deletions(-)
---
diff --git a/libyelp/yelp-settings.c b/libyelp/yelp-settings.c
index 1cf5a6b..cb8f253 100644
--- a/libyelp/yelp-settings.c
+++ b/libyelp/yelp-settings.c
@@ -101,9 +101,7 @@ static void           gtk_font_changed           (GtkSettings          *gtk_sett
 static void           icon_theme_changed         (GtkIconTheme         *theme,
                                                  YelpSettings         *settings);
 
-static void           rgb_to_hsv                 (gdouble  r,
-                                                 gdouble  g,
-                                                 gdouble  b,
+static void           rgb_to_hsv                 (GdkRGBA  color,
                                                  gdouble *h,
                                                  gdouble *s,
                                                  gdouble *v);
@@ -889,127 +887,86 @@ gtk_theme_changed (GtkSettings  *gtk_settings,
                   GParamSpec   *pspec,
                   YelpSettings *settings)
 {
-    GtkWidget *widget;
-    GtkStyle  *style;
-    GdkColor  *color;
-    GdkColor   blue = { 0, 0x1E1E, 0x3E3E, 0xE7E7 };
+    GtkStyleContext *context;
+    GtkWidget *tmpwin, *tmpview;
+    GdkRGBA base, text, link;
     gdouble    base_h, base_s, base_v;
     gdouble    text_h, text_s, text_v;
-    gint i;
 
     g_mutex_lock (&settings->priv->mutex);
 
-    style = gtk_rc_get_style_by_paths (gtk_settings,
-                                       "GtkTextView", "GtkTextView",
-                                       GTK_TYPE_TEXT_VIEW);
-    if (style)
-        g_object_ref (G_OBJECT (style));
-    else
-        style = gtk_style_new ();
+    tmpwin = gtk_offscreen_window_new ();
+    tmpview = gtk_text_view_new ();
+    gtk_container_add (GTK_CONTAINER (tmpwin), tmpview);
+    gtk_widget_show_all (tmpwin);
+    context = gtk_widget_get_style_context (tmpview);
+    /* I have to do this for some reason. Don't ask me why. Ain't in the docs. */
+    gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
 
+    gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &text);
+    gtk_style_context_get_background_color (context, GTK_STATE_FLAG_ACTIVE, &base);
 
-    rgb_to_hsv (style->text[GTK_STATE_NORMAL].red / 65535.0,
-                style->text[GTK_STATE_NORMAL].green / 65535.0,
-                style->text[GTK_STATE_NORMAL].blue / 65535.0,
-                &text_h, &text_s, &text_v);
-    rgb_to_hsv (style->base[GTK_STATE_NORMAL].red / 65535.0,
-                style->base[GTK_STATE_NORMAL].green / 65535.0,
-                style->base[GTK_STATE_NORMAL].blue / 65535.0,
-                &base_h, &base_s, &base_v);
+    rgb_to_hsv (text, &text_h, &text_s, &text_v);
+    rgb_to_hsv (base, &base_h, &base_s, &base_v);
 
     /* YELP_SETTINGS_COLOR_BASE */
-    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_BASE], 8,
-                "#%02X%02X%02X",
-                style->base[GTK_STATE_NORMAL].red >> 8,
-                style->base[GTK_STATE_NORMAL].green >> 8,
-                style->base[GTK_STATE_NORMAL].blue >> 8);
+    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_BASE], 8, "#%02X%02X%02X",
+                (gint) (base.red * 255), (gint) (base.green * 255), (gint) (base.blue * 255));
 
     /* YELP_SETTINGS_COLOR_TEXT */
-    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_TEXT], 8,
-                "#%02X%02X%02X",
-                style->text[GTK_STATE_NORMAL].red >> 8,
-                style->text[GTK_STATE_NORMAL].green >> 8,
-                style->text[GTK_STATE_NORMAL].blue >> 8);
+    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_TEXT], 8, "#%02X%02X%02X",
+                (gint) (text.red * 255), (gint) (text.green * 255), (gint) (text.blue * 255));
 
     /* YELP_SETTINGS_COLOR_LINK */
-    widget = gtk_link_button_new ("http://www.gnome.org";);
-    gtk_widget_style_get (widget, "link-color", &color, NULL);
-    if (!color)
-        color = &blue;
-    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_LINK], 8,
-                "#%02X%02X%02X",
-                color->red >> 8,
-                color->green >> 8,
-                color->blue >> 8);
-    if (color != &blue)
-        gdk_color_free (color);
+    gtk_style_context_get_color (context, GTK_STATE_FLAG_LINK, &link);
+    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_LINK], 8, "#%02X%02X%02X",
+                (gint) (text.red * 255), (gint) (text.green * 255), (gint) (text.blue * 255));
 
     /* YELP_SETTINGS_COLOR_LINK_VISITED */
-    color = NULL;
-    gtk_widget_style_get (widget, "visited-link-color", &color, NULL);
-    if (color) {
-        g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_LINK_VISITED], 8,
-                    "#%02X%02X%02X",
-                    color->red >> 8,
-                    color->green >> 8,
-                    color->blue >> 8);
-        gdk_color_free (color);
-    }
-
-    g_object_ref_sink (widget);
-    g_object_unref (widget);
+    gtk_style_context_get_color (context, GTK_STATE_FLAG_VISITED, &link);
+    g_snprintf (settings->priv->colors[YELP_SETTINGS_COLOR_LINK_VISITED], 8, "#%02X%02X%02X",
+                (gint) (text.red * 255), (gint) (text.green * 255), (gint) (text.blue * 255));
 
     /* YELP_SETTINGS_COLOR_TEXT_LIGHT */
-    hsv_to_hex (text_h, 
-                text_s,
-                text_v - ((text_v - base_v) * 0.25),
+    hsv_to_hex (text_h, text_s, text_v - ((text_v - base_v) * 0.25),
                 settings->priv->colors[YELP_SETTINGS_COLOR_TEXT_LIGHT]);
 
     /* YELP_SETTINGS_COLOR_GRAY */
-    hsv_to_hex (base_h, 
-                base_s,
+    hsv_to_hex (base_h, base_s,
                 base_v - ((base_v - text_v) * 0.05),
                 settings->priv->colors[YELP_SETTINGS_COLOR_GRAY_BASE]);
-    hsv_to_hex (base_h, 
-                base_s,
+    hsv_to_hex (base_h, base_s,
                 base_v - ((base_v - text_v) * 0.1),
                 settings->priv->colors[YELP_SETTINGS_COLOR_DARK_BASE]);
-    hsv_to_hex (base_h, 
-                base_s,
+    hsv_to_hex (base_h, base_s,
                 base_v - ((base_v - text_v) * 0.26),
                 settings->priv->colors[YELP_SETTINGS_COLOR_GRAY_BORDER]);
 
     /* YELP_SETTINGS_COLOR_BLUE */
-    hsv_to_hex (211,
-                0.1,
+    hsv_to_hex (211, 0.1,
                 base_v - ((base_v - text_v) * 0.01),
                 settings->priv->colors[YELP_SETTINGS_COLOR_BLUE_BASE]);
-    hsv_to_hex (211,
-                0.45,
+    hsv_to_hex (211, 0.45,
                 base_v - ((base_v - text_v) * 0.19),
                 settings->priv->colors[YELP_SETTINGS_COLOR_BLUE_BORDER]);
 
     /* YELP_SETTINGS_COLOR_RED */
-    hsv_to_hex (0,
-                0.13,
+    hsv_to_hex (0, 0.13,
                 base_v - ((base_v - text_v) * 0.01),
                 settings->priv->colors[YELP_SETTINGS_COLOR_RED_BASE]);
-    hsv_to_hex (0,
-                0.83,
+    hsv_to_hex (0, 0.83,
                 base_v - ((base_v - text_v) * 0.06),
                 settings->priv->colors[YELP_SETTINGS_COLOR_RED_BORDER]);
 
     /* YELP_SETTINGS_COLOR_YELLOW */
-    hsv_to_hex (60,
-                0.25,
+    hsv_to_hex (60, 0.25,
                 base_v - ((base_v - text_v) * 0.01),
                 settings->priv->colors[YELP_SETTINGS_COLOR_YELLOW_BASE]);
-    hsv_to_hex (60,
-                1.0,
+    hsv_to_hex (60, 1.0,
                 base_v - ((base_v - text_v) * 0.07),
                 settings->priv->colors[YELP_SETTINGS_COLOR_YELLOW_BORDER]);
 
-    g_object_unref (G_OBJECT (style));
+    gtk_widget_destroy (tmpwin);
 
     g_mutex_unlock (&settings->priv->mutex);
 
@@ -1115,14 +1072,14 @@ yelp_settings_cmp_icons (const gchar *icon1,
 /******************************************************************************/
 
 static void
-rgb_to_hsv (gdouble r, gdouble g, gdouble b, gdouble *h, gdouble *s, gdouble *v)
+rgb_to_hsv (GdkRGBA color, gdouble *h, gdouble *s, gdouble *v)
 {
     gdouble min, max, delta;
 
-    max = (r > g) ? r : g;
-    max = (max > b) ? max : b;
-    min = (r < g) ? r : g;
-    min = (min < b) ? min : b;
+    max = (color.red > color.green) ? color.red : color.green;
+    max = (max > color.blue) ? max : color.blue;
+    min = (color.red < color.green) ? color.red : color.green;
+    min = (min < color.blue) ? min : color.blue;
 
     delta = max - min;
 
@@ -1133,12 +1090,12 @@ rgb_to_hsv (gdouble r, gdouble g, gdouble b, gdouble *h, gdouble *s, gdouble *v)
     if (max != min) {
        *s = delta / *v;
 
-       if (r == max)
-           *h = (g - b) / delta;
-       else if (g == max)
-           *h = 2 + (b - r) / delta;
-       else if (b == max)
-           *h = 4 + (r - g) / delta;
+       if (color.red == max)
+           *h = (color.green - color.blue) / delta;
+       else if (color.green == max)
+           *h = 2 + (color.blue - color.red) / delta;
+       else if (color.blue == max)
+           *h = 4 + (color.red - color.green) / delta;
 
        *h *= 60;
        if (*h < 0.0)


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