diff -r c6789ca79811 -r 766bbe6061ec pidgin/pidginstylecontext.c --- a/pidgin/pidginstylecontext.c Fri Jan 15 01:17:42 2021 -0600 +++ b/pidgin/pidginstylecontext.c Mon Jan 18 00:05:05 2021 -0600 @@ -29,48 +29,53 @@ /****************************************************************************** * Public API *****************************************************************************/ +void +pidgin_style_context_get_background_color(GdkRGBA *color) { + /* This value will leak, we could put a shutdown function in but right now + * that seems like a bit much for a few bytes. + */ + static GdkRGBA *background = NULL; + + if(g_once_init_enter(&background)) { + GdkRGBA *bg = NULL; + GtkStyleContext *context = NULL; + GtkWidget *window = NULL; + + /* We create a window to get its background color from its style + * context. This _is_ doable without creating a window, but you still + * need the window class and about four times as much code, so that's + * why we do it this way. + */ + window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + context = gtk_widget_get_style_context(window); + + gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, + GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &bg, + NULL); + g_object_unref(G_OBJECT(window)); + + g_once_init_leave(&background, bg); + } + + *color = *background; +} + gboolean -pidgin_style_context_is_dark(GtkStyleContext *context) { +pidgin_style_context_is_dark(void) { GdkRGBA bg; gdouble luminance = 0.0; - if(context == NULL) { - if(dark_mode_have_cache) { - return dark_mode_cached_value; - } - - context = gtk_style_context_new(); - } else { - g_object_ref(G_OBJECT(context)); + if(dark_mode_have_cache) { + return dark_mode_cached_value; } - gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, - GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &bg, - NULL); - g_object_unref(G_OBJECT(context)); + pidgin_style_context_get_background_color(&bg); - /* magic values are taken from https://en.wikipedia.org/wiki/Luma_(video) - * Rec._601_luma_versus_Rec._709_luma_coefficients. - */ - luminance = (0.299 * bg.red) + (0.587 * bg.green) + (0.114 * bg.blue); + /* 709 coefficients from https://en.wikipedia.org/wiki/Luma_(video) */ + luminance = (0.2126 * bg.red) + (0.7152 * bg.green) + (0.0722 * bg.blue); - dark_mode_cached_value = (luminance < 0x7FFF); + dark_mode_cached_value = (luminance < 0.5); dark_mode_have_cache = TRUE; return dark_mode_cached_value; } - -void -pidgin_style_context_adjust_contrast(GtkStyleContext *context, GdkRGBA *rgba) { - if(pidgin_style_context_is_dark(context)) { - gdouble h, s, v; - - gtk_rgb_to_hsv(rgba->red, rgba->green, rgba->blue, &h, &s, &v); - - v += 0.3; - v = v > 1.0 ? 1.0 : v; - s = 0.7; - - gtk_hsv_to_rgb(h, s, v, &rgba->red, &rgba->green, &rgba->blue); - } -}