[evolution/gnome-3-30] I#136 - Choose contrasting font color for the calendar data



commit 1b97ba92a45c42914f6f37474b454b191706f487
Author: Milan Crha <mcrha redhat com>
Date:   Tue Sep 25 15:03:56 2018 +0200

    I#136 - Choose contrasting font color for the calendar data
    
    Closes https://gitlab.gnome.org/GNOME/evolution/issues/136

 src/calendar/gui/e-day-view-main-item.c   | 19 +++++++++----------
 src/calendar/gui/e-day-view-top-item.c    |  2 +-
 src/calendar/gui/e-day-view.c             | 13 ++-----------
 src/calendar/gui/e-to-do-pane.c           | 13 ++-----------
 src/calendar/gui/e-week-view-event-item.c | 16 +++-------------
 src/calendar/gui/e-week-view.c            | 25 ++++++++-----------------
 src/calendar/gui/print.c                  | 25 +++++++++++++++++--------
 src/e-util/e-cell-text.c                  | 13 +------------
 src/e-util/e-misc-utils.c                 | 28 ++++++++++++++++++++++++++++
 src/e-util/e-misc-utils.h                 |  3 +++
 10 files changed, 74 insertions(+), 83 deletions(-)
---
diff --git a/src/calendar/gui/e-day-view-main-item.c b/src/calendar/gui/e-day-view-main-item.c
index 4edb2d8c84..05f9f2e795 100644
--- a/src/calendar/gui/e-day-view-main-item.c
+++ b/src/calendar/gui/e-day-view-main-item.c
@@ -546,6 +546,8 @@ day_view_main_item_draw_day_event (EDayViewMainItem *main_item,
                        bar_y1 = item_y + 1;
 
                else if (day_view->resize_drag_pos == E_CALENDAR_VIEW_POS_BOTTOM_EDGE) {
+                       GdkRGBA fg_rgba;
+
                        bar_y2 = item_y + item_h - 1;
 
                        end_minute = event->end_minute;
@@ -583,10 +585,8 @@ day_view_main_item_draw_day_event (EDayViewMainItem *main_item,
 
                        layout = gtk_widget_create_pango_layout (GTK_WIDGET (GNOME_CANVAS_ITEM 
(main_item)->canvas), end_regsizeime);
                        cairo_set_font_size (cr, 13);
-                       if ((bg_rgba.red > 0.7) || (bg_rgba.green > 0.7) || (bg_rgba.blue > 0.7))
-                               cairo_set_source_rgb (cr, 0, 0, 0);
-                       else
-                               cairo_set_source_rgb (cr, 1, 1, 1);
+                       fg_rgba = e_utils_get_text_color_for_background (&bg_rgba);
+                       gdk_cairo_set_source_rgba (cr, &fg_rgba);
                        pango_cairo_update_layout (cr, layout);
                        pango_cairo_show_layout (cr, layout);
                        g_object_unref (layout);
@@ -743,8 +743,9 @@ day_view_main_item_draw_day_event (EDayViewMainItem *main_item,
                g_slist_free (categories_pixbufs);
        }
 
-       if (!short_event)
-       {
+       if (!short_event) {
+               GdkRGBA fg_rgba;
+
                if (event->start_minute % time_divisions != 0
                        || (day_view->show_event_end_times
                        && event->end_minute % time_divisions != 0)) {
@@ -820,10 +821,8 @@ day_view_main_item_draw_day_event (EDayViewMainItem *main_item,
                if (icon_x_inc == 0)
                        icon_x += 14;
 
-               if ((bg_rgba.red > 0.7) || (bg_rgba.green > 0.7) || (bg_rgba.blue > 0.7))
-                       cairo_set_source_rgb (cr, 0, 0, 0);
-               else
-                       cairo_set_source_rgb (cr, 1, 1, 1);
+               fg_rgba = e_utils_get_text_color_for_background (&bg_rgba);
+               gdk_cairo_set_source_rgba (cr, &fg_rgba);
 
                layout = gtk_widget_create_pango_layout (GTK_WIDGET (GNOME_CANVAS_ITEM (main_item)->canvas), 
text);
                if (resize_flag)
diff --git a/src/calendar/gui/e-day-view-top-item.c b/src/calendar/gui/e-day-view-top-item.c
index 8a31a14749..0586681335 100644
--- a/src/calendar/gui/e-day-view-top-item.c
+++ b/src/calendar/gui/e-day-view-top-item.c
@@ -311,7 +311,7 @@ day_view_top_item_draw_long_event (EDayViewTopItem *top_item,
 
        time_width = e_day_view_get_time_string_width (day_view);
 
-       e_utils_get_theme_color (GTK_WIDGET (day_view), "theme_fg_color", E_UTILS_DEFAULT_THEME_FG_COLOR, 
&rgba);
+       rgba = e_utils_get_text_color_for_background (&bg_rgba);
        gdk_cairo_set_source_rgba (cr, &rgba);
 
        if (event->start > day_view->day_starts[start_day]) {
diff --git a/src/calendar/gui/e-day-view.c b/src/calendar/gui/e-day-view.c
index 7dd7e88f99..2525e139ff 100644
--- a/src/calendar/gui/e-day-view.c
+++ b/src/calendar/gui/e-day-view.c
@@ -679,17 +679,8 @@ e_day_view_get_text_color (EDayView *day_view,
                rgba.alpha = 1.0;
        }
 
-       if ((rgba.red > 0.7) || (rgba.green > 0.7) || (rgba.blue > 0.7)) {
-               color.red = 0.0;
-               color.green = 0.0;
-               color.blue = 0.0;
-       } else {
-               color.red = 65535.0f;
-               color.green = 65535.0f;
-               color.blue = 65535.0f;
-       }
-
-       color.pixel = 0;
+       rgba = e_utils_get_text_color_for_background (&rgba);
+       e_rgba_to_color (&rgba, &color);
 
        return color;
 }
diff --git a/src/calendar/gui/e-to-do-pane.c b/src/calendar/gui/e-to-do-pane.c
index 9047fd2cd7..c1375f51ae 100644
--- a/src/calendar/gui/e-to-do-pane.c
+++ b/src/calendar/gui/e-to-do-pane.c
@@ -546,17 +546,8 @@ etdp_get_fgcolor_for_bgcolor (const GdkRGBA *bgcolor)
 {
        GdkRGBA fgcolor = { 1.0, 1.0, 1.0, 1.0 };
 
-       if (bgcolor) {
-               if ((bgcolor->red > 0.7) || (bgcolor->green > 0.7) || (bgcolor->blue > 0.7)) {
-                       fgcolor.red = 0.0;
-                       fgcolor.green = 0.0;
-                       fgcolor.blue = 0.0;
-               } else {
-                       fgcolor.red = 1.0;
-                       fgcolor.green = 1.0;
-                       fgcolor.blue = 1.0;
-               }
-       }
+       if (bgcolor)
+               fgcolor = e_utils_get_text_color_for_background (bgcolor);
 
        return fgcolor;
 }
diff --git a/src/calendar/gui/e-week-view-event-item.c b/src/calendar/gui/e-week-view-event-item.c
index cab9b5394c..b651b8a278 100644
--- a/src/calendar/gui/e-week-view-event-item.c
+++ b/src/calendar/gui/e-week-view-event-item.c
@@ -305,19 +305,9 @@ week_view_draw_time (EWeekView *week_view,
        PangoLayout *layout;
        PangoFontDescription *small_font_desc;
        PangoContext *pango_context;
-       GdkColor color;
+       GdkRGBA fg_rgba;
 
-       color.pixel = 0;
-
-       if ((bg_rgba.red > 0.7) || (bg_rgba.green > 0.7) || (bg_rgba.blue > 0.7)) {
-               color.red = 0.0;
-               color.green = 0.0;
-               color.blue = 0.0;
-       } else {
-               color.red = 65535.0f;
-               color.green = 65535.0f;
-               color.blue = 65535.0f;
-       }
+       fg_rgba = e_utils_get_text_color_for_background (&bg_rgba);
 
        cairo_save (cr);
 
@@ -325,7 +315,7 @@ week_view_draw_time (EWeekView *week_view,
 
        small_font_desc = week_view->small_font_desc;
 
-       gdk_cairo_set_source_color (cr, &color);
+       gdk_cairo_set_source_rgba (cr, &fg_rgba);
 
        layout = gtk_widget_create_pango_layout (GTK_WIDGET (week_view), NULL);
        pango_context = gtk_widget_create_pango_context (GTK_WIDGET (week_view));
diff --git a/src/calendar/gui/e-week-view.c b/src/calendar/gui/e-week-view.c
index 69f37a344d..b69010ac33 100644
--- a/src/calendar/gui/e-week-view.c
+++ b/src/calendar/gui/e-week-view.c
@@ -965,31 +965,22 @@ e_week_view_get_text_color (EWeekView *week_view,
                             EWeekViewEvent *event)
 {
        GdkColor color;
-       GdkRGBA bg_rgba;
+       GdkRGBA rgba;
 
        if (is_comp_data_valid (event) &&
-           e_cal_model_get_rgba_for_component (e_calendar_view_get_model (E_CALENDAR_VIEW (week_view)), 
event->comp_data, &bg_rgba)) {
+           e_cal_model_get_rgba_for_component (e_calendar_view_get_model (E_CALENDAR_VIEW (week_view)), 
event->comp_data, &rgba)) {
        } else {
                gdouble cc = 65535.0;
 
-               bg_rgba.red = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].red / cc;
-               bg_rgba.green = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].green / cc;
-               bg_rgba.blue = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].blue / cc;
-               bg_rgba.alpha = 1.0;
+               rgba.red = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].red / cc;
+               rgba.green = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].green / cc;
+               rgba.blue = week_view->colors[E_WEEK_VIEW_COLOR_EVENT_BACKGROUND].blue / cc;
+               rgba.alpha = 1.0;
 
        }
 
-       color.pixel = 0;
-
-       if ((bg_rgba.red > 0.7) || (bg_rgba.green > 0.7) || (bg_rgba.blue > 0.7)) {
-               color.red = 0.0;
-               color.green = 0.0;
-               color.blue = 0.0;
-       } else {
-               color.red = 65535.0f;
-               color.green = 65535.0f;
-               color.blue = 65535.0f;
-       }
+       rgba = e_utils_get_text_color_for_background (&rgba);
+       e_rgba_to_color (&rgba, &color);
 
        return color;
 }
diff --git a/src/calendar/gui/print.c b/src/calendar/gui/print.c
index db0538921f..68fd533c4a 100644
--- a/src/calendar/gui/print.c
+++ b/src/calendar/gui/print.c
@@ -493,6 +493,21 @@ shrink_text_to_line (PangoLayout *layout,
        return layout;
 }
 
+static void
+print_set_fg_for_bg (cairo_t *cr,
+                    const GdkRGBA *bg_rgba)
+{
+       GdkRGBA fg_rgba;
+
+       if (!bg_rgba) {
+               cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+               return;
+       }
+
+       fg_rgba = e_utils_get_text_color_for_background (bg_rgba);
+       gdk_cairo_set_source_rgba (cr, &fg_rgba);
+}
+
 /* Prints 1 line of aligned text in a box. It is centered vertically, and
  * the horizontal alignment can be either PANGO_ALIGN_LEFT, PANGO_ALIGN_RIGHT,
  * or PANGO_ALIGN_CENTER. Text is truncated if too long for cell. */
@@ -538,10 +553,7 @@ print_text_line (GtkPrintContext *context,
        cairo_clip (cr);
 
        cairo_new_path (cr);
-       if (!bg_rgba || (bg_rgba->red > 0.7) || (bg_rgba->green > 0.7) || (bg_rgba->blue > 0.7))
-               cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
-       else
-               cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+       print_set_fg_for_bg (cr, bg_rgba);
 
        cairo_move_to (cr, x1, y1);
        pango_cairo_show_layout (cr, layout);
@@ -993,10 +1005,7 @@ bound_text (GtkPrintContext *context,
                cairo_clip (cr);
                cairo_new_path (cr);
 
-               if (!bg_rgba || (bg_rgba->red > 0.7) || (bg_rgba->green > 0.7) || (bg_rgba->blue > 0.7))
-                       cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
-               else
-                       cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+               print_set_fg_for_bg (cr, bg_rgba);
 
                cairo_move_to (cr, x1, y1);
                pango_cairo_show_layout (cr, layout);
diff --git a/src/e-util/e-cell-text.c b/src/e-util/e-cell-text.c
index 64aa840135..6366fc1088 100644
--- a/src/e-util/e-cell-text.c
+++ b/src/e-util/e-cell-text.c
@@ -782,18 +782,7 @@ ect_draw (ECellView *ecell_view,
                                ect->bg_color_column, row);
 
                        if (color_spec && gdk_rgba_parse (&bg_rgba, color_spec)) {
-                               bg_rgba.alpha = 1.0;
-
-                               if ((bg_rgba.red > 0.7) || (bg_rgba.green > 0.7) || (bg_rgba.blue > 0.7)) {
-                                       bg_rgba.red = 0.0;
-                                       bg_rgba.green = 0.0;
-                                       bg_rgba.blue = 0.0;
-                               } else {
-                                       bg_rgba.red = 1.0;
-                                       bg_rgba.green = 1.0;
-                                       bg_rgba.blue = 1.0;
-                               }
-
+                               bg_rgba = e_utils_get_text_color_for_background (&bg_rgba);
                                gdk_cairo_set_source_rgba (cr, &bg_rgba);
                        }
 
diff --git a/src/e-util/e-misc-utils.c b/src/e-util/e-misc-utils.c
index 3801337fc2..fffd9207bf 100644
--- a/src/e-util/e-misc-utils.c
+++ b/src/e-util/e-misc-utils.c
@@ -1632,6 +1632,34 @@ e_utils_shade_color (const GdkRGBA *a,
        b->alpha = a->alpha;
 }
 
+GdkRGBA
+e_utils_get_text_color_for_background (const GdkRGBA *bg_rgba)
+{
+       GdkRGBA text_rgba = { 0.0, 0.0, 0.0, 1.0 };
+       gdouble brightness;
+
+       g_return_val_if_fail (bg_rgba != NULL, text_rgba);
+
+       brightness =
+               (0.2109 * 255.0 * bg_rgba->red) +
+               (0.5870 * 255.0 * bg_rgba->green) +
+               (0.1021 * 255.0 * bg_rgba->blue);
+
+       if (brightness <= 140.0) {
+               text_rgba.red = 1.0;
+               text_rgba.green = 1.0;
+               text_rgba.blue = 1.0;
+       } else {
+               text_rgba.red = 0.0;
+               text_rgba.green = 0.0;
+               text_rgba.blue = 0.0;
+       }
+
+       text_rgba.alpha = 1.0;
+
+       return text_rgba;
+}
+
 static gint
 epow10 (gint number)
 {
diff --git a/src/e-util/e-misc-utils.h b/src/e-util/e-misc-utils.h
index d6ce3ba2b0..e170e1993c 100644
--- a/src/e-util/e-misc-utils.h
+++ b/src/e-util/e-misc-utils.h
@@ -142,6 +142,9 @@ void                e_utils_shade_color             (const GdkRGBA *a,
                                                 GdkRGBA *b,
                                                 gdouble mult);
 
+GdkRGBA                e_utils_get_text_color_for_background
+                                               (const GdkRGBA *bg_rgba);
+
 /* This only makes a filename safe for usage as a filename.
  * It still may have shell meta-characters in it. */
 gchar *                e_format_number                 (gint number);


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