[evince] libview: make contrast functions available in all libview/



commit 2ac0d7096f4d940ee2796afc3037c4941e74a5f6
Author: vanadiae <vanadiae35 gmail com>
Date:   Tue Jun 9 13:13:17 2020 +0200

    libview: make contrast functions available in all libview/
    
    As the two functions in ev-color-contrast.h might be
    used elsewhere later in libview.
    
    Signed-off-by: Germán Poo-Caamaño <gpoo gnome org>

 libview/ev-annotation-window.c | 61 +---------------------------
 libview/ev-color-contrast.c    | 92 ++++++++++++++++++++++++++++++++++++++++++
 libview/ev-color-contrast.h    | 36 +++++++++++++++++
 libview/meson.build            |  1 +
 4 files changed, 131 insertions(+), 59 deletions(-)
---
diff --git a/libview/ev-annotation-window.c b/libview/ev-annotation-window.c
index 4b5011ca..c4a973f9 100644
--- a/libview/ev-annotation-window.c
+++ b/libview/ev-annotation-window.c
@@ -25,6 +25,7 @@
 #include <math.h>
 
 #include "ev-annotation-window.h"
+#include "ev-color-contrast.h"
 #include "ev-stock-icons.h"
 #include "ev-view-marshal.h"
 #include "ev-document-misc.h"
@@ -119,64 +120,6 @@ ev_annotation_window_sync_contents (EvAnnotationWindow *window)
        g_free (contents);
 }
 
-static double
-get_srgb (const double color_component)
-{
-       /* calculation of sRGB color is based on note 1 of 
https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef */
-       if (color_component <= 0.03928)
-               return color_component / 12.92;
-       else
-               return powf (((color_component + 0.055) / 1.055), 2.4);
-}
-
-static double
-get_relative_luminance (const GdkRGBA *color)
-{
-       /* calculation of relative luminance is based on note 1 of 
https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef */
-       return get_srgb (color->red) * 0.2126 + get_srgb (color->blue) * 0.0722 + get_srgb (color->green) * 
0.7152;
-}
-
-static double
-get_contrast_level (const GdkRGBA *bg_color,
-                   const GdkRGBA *fg_color)
-{
-       /* the contrast level calculus is based on WCAG 2.0 guideline 1.4  */
-       /* https://www.w3.org/WAI/GL/UNDERSTANDING-WCAG20/visual-audio-contrast7.html#key-terms */
-       const double bg_luminance = get_relative_luminance (bg_color);
-       const double fg_luminance = get_relative_luminance (fg_color);
-       return (fmax (bg_luminance, fg_luminance) + 0.05) / (fmin (bg_luminance, fg_luminance) + 0.05);
-}
-
-/**
- * get_most_readable_color:
- *
- * Returns: (transfer none): the most readable color on bg_color between first_color and second_color
- */
-static GdkRGBA *
-get_most_readable_color (const GdkRGBA *bg_color,
-                        GdkRGBA *first_color,
-                        GdkRGBA *second_color)
-{
-       const double first_contrast = get_contrast_level (bg_color, first_color);
-       const double second_contrast = get_contrast_level (bg_color, second_color);
-       /* higher is more readable (more contrast) */
-       return first_contrast > second_contrast ? first_color : second_color;
-}
-
-/**
- * get_best_foreground_color:
- *
- * Returns: (transfer full): the most readable foreground color on bg_color between black #000000 and white 
#FFFFFF
- */
-static GdkRGBA *
-get_best_foreground_color (const GdkRGBA *bg_color)
-{
-       GdkRGBA black, white;
-       gdk_rgba_parse (&black, "#000000");
-       gdk_rgba_parse (&white, "#FFFFFF");
-       return gdk_rgba_copy (get_most_readable_color (bg_color, &black, &white));
-}
-
 static void
 ev_annotation_window_set_color (EvAnnotationWindow *window,
                                GdkRGBA            *color)
@@ -185,7 +128,7 @@ ev_annotation_window_set_color (EvAnnotationWindow *window,
        g_autofree char    *rgba_str = gdk_rgba_to_string (color);
        g_autofree char    *css_data = NULL;
        g_autoptr (GError)  error = NULL;
-       g_autoptr (GdkRGBA) icon_color = get_best_foreground_color (color);
+       g_autoptr (GdkRGBA) icon_color = ev_color_contrast_get_best_foreground_color (color);
        g_autofree char    *icon_color_str = gdk_rgba_to_string (icon_color);
        css_data = g_strdup_printf ("button {border-color: %1$s; color: %2$s; -gtk-icon-shadow:0 0; 
box-shadow:0 0;}\n\
                                     button:hover {background: lighter(%1$s); border-color: darker(%1$s);}\n\
diff --git a/libview/ev-color-contrast.c b/libview/ev-color-contrast.c
new file mode 100644
index 00000000..947f7983
--- /dev/null
+++ b/libview/ev-color-contrast.c
@@ -0,0 +1,92 @@
+/* ev-color-contrast.c
+ * This file is part of Evince, a GNOME document viewer
+ *
+ * Copyright (C) 2020 Vanadiae <vanadiae35 gmail com>
+ *
+ * Evince is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Evince is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <math.h>
+
+#include "ev-color-contrast.h"
+
+static double
+get_srgb (const double color_component)
+{
+       /* calculation of sRGB color is based on note 1 of
+         * https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
+        */
+       if (color_component <= 0.03928)
+               return color_component / 12.92;
+       else
+               return powf (((color_component + 0.055) / 1.055), 2.4);
+}
+
+static double
+get_relative_luminance (const GdkRGBA *color)
+{
+       /* calculation of relative luminance is based on note 1 of
+        * https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
+        */
+       return get_srgb (color->red) * 0.2126 + get_srgb (color->blue) * 0.0722 +
+              get_srgb (color->green) * 0.7152;
+}
+
+static double
+get_contrast_level (const GdkRGBA *bg_color,
+                   const GdkRGBA *fg_color)
+{
+       /* the contrast level calculus is based on WCAG 2.0 guideline 1.4  */
+       /* https://www.w3.org/WAI/GL/UNDERSTANDING-WCAG20/visual-audio-contrast7.html#key-terms
+        */
+       const double bg_luminance = get_relative_luminance (bg_color);
+       const double fg_luminance = get_relative_luminance (fg_color);
+       return (fmax (bg_luminance, fg_luminance) + 0.05) / (fmin (bg_luminance, fg_luminance) + 0.05);
+}
+
+/**
+ * ev_color_get_most_readable_color:
+ *
+ * Returns: (transfer none): the most readable color on bg_color between
+ *          first_color and second_color
+ */
+GdkRGBA *
+ev_color_contrast_get_most_readable_color (const GdkRGBA *bg_color,
+                                          GdkRGBA       *first_color,
+                                          GdkRGBA       *second_color)
+{
+       const double first_contrast = get_contrast_level (bg_color, first_color);
+       const double second_contrast = get_contrast_level (bg_color, second_color);
+
+       /* higher is more readable (more contrast) */
+       return first_contrast > second_contrast ? first_color : second_color;
+}
+
+/**
+ * ev_color_get_best_foreground_color:
+ *
+ * Returns: (transfer full): the most readable foreground color on bg_color
+ *          between black #000000 and white #FFFFFF
+ */
+GdkRGBA *
+ev_color_contrast_get_best_foreground_color (const GdkRGBA *bg_color)
+{
+       GdkRGBA black, white;
+
+       gdk_rgba_parse (&black, "#000000");
+       gdk_rgba_parse (&white, "#FFFFFF");
+
+       return gdk_rgba_copy (ev_color_contrast_get_most_readable_color (bg_color, &black, &white));
+}
diff --git a/libview/ev-color-contrast.h b/libview/ev-color-contrast.h
new file mode 100644
index 00000000..300215c2
--- /dev/null
+++ b/libview/ev-color-contrast.h
@@ -0,0 +1,36 @@
+/* ev-color-contrast.h
+ *  this file is part of evince, a gnome document viewer
+ *
+ * Copyright (C) 2020 Vanadiae <vanadiae35 gmail com>
+ *
+ * Evince is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Evince is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#if !defined (EVINCE_COMPILATION)
+#error "This is a private header."
+#endif
+
+#ifndef __EV_COLOR_CONTRAST_H__
+#define __EV_COLOR_CONTRAST_H__
+
+#include <gdk/gdk.h>
+
+GdkRGBA  *ev_color_contrast_get_most_readable_color   (const GdkRGBA *bg_color,
+                                                      GdkRGBA *first_color,
+                                                      GdkRGBA *second_color);
+
+GdkRGBA  *ev_color_contrast_get_best_foreground_color (const GdkRGBA *bg_color);
+
+#endif /* __EV_COLOR_CONTRAST_H__ */
diff --git a/libview/meson.build b/libview/meson.build
index 01b1a27c..20179aff 100644
--- a/libview/meson.build
+++ b/libview/meson.build
@@ -19,6 +19,7 @@ install_headers(
 
 sources = files(
   'ev-annotation-window.c',
+  'ev-color-contrast.c',
   'ev-document-model.c',
   'ev-form-field-accessible.c',
   'ev-image-accessible.c',


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