From f290fd9bcf0e0197501d7998569651a28ae981ff Mon Sep 17 00:00:00 2001 From: vantu5z Date: Mon, 15 Jun 2020 13:59:33 +0300 Subject: [PATCH] Use a contrasting text color in pedigree view White text shows up better in boxes with a dark background color. Fixes #11799 --- gramps/gui/test/utils_test.py | 25 +++++++++++++++++++++++++ gramps/gui/utils.py | 9 +++++++++ gramps/plugins/view/pedigreeview.py | 6 ++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 gramps/gui/test/utils_test.py diff --git a/gramps/gui/test/utils_test.py b/gramps/gui/test/utils_test.py new file mode 100644 index 000000000..d01c3a70f --- /dev/null +++ b/gramps/gui/test/utils_test.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" Unittest for utils.py """ + +import unittest +from ..utils import get_contrast_color + + +class Test_get_contrast_color(unittest.TestCase): + def setUp(self): + self.black = (0, 0, 0) + self.white = (1, 1, 1) + + def test_contrast_black_returns_white(self): + contrast_color = get_contrast_color(self.black) + self.assertEqual(contrast_color, self.white, + "Contrasting color for black did not return white") + + def test_contrast_white_returns_black(self): + contrast_color = get_contrast_color(self.white) + self.assertEqual(contrast_color, self.black, + "Contrasting color for white did not return black") + +if __name__ == "__main__": + unittest.main() diff --git a/gramps/gui/utils.py b/gramps/gui/utils.py index 758c93298..d6346fb9d 100644 --- a/gramps/gui/utils.py +++ b/gramps/gui/utils.py @@ -545,6 +545,15 @@ def rgb_to_hex(rgb): rgbint = (int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)) return '#%02x%02x%02x' % rgbint +def get_contrast_color(color): + """ + Choose contrast text color (white or black) for provided background. + """ + yiq = (color[0]*299)+(color[1]*587)+(color[2]*114) + if (yiq < 500): + return (1, 1, 1) # 'white' + return (0, 0, 0) # 'black' + def get_link_color(context): """ Find the link color for the current theme. diff --git a/gramps/plugins/view/pedigreeview.py b/gramps/plugins/view/pedigreeview.py index a199b7d65..ed1e5be74 100644 --- a/gramps/plugins/view/pedigreeview.py +++ b/gramps/plugins/view/pedigreeview.py @@ -65,7 +65,8 @@ from gramps.gen.config import config from gramps.gui.views.bookmarks import PersonBookmarks from gramps.gen.const import CUSTOM_FILTERS, URL_MANUAL_PAGE, URL_WIKISTRING from gramps.gui.dialog import RunDatabaseRepair, ErrorDialog -from gramps.gui.utils import color_graph_box, hex_to_rgb_float, is_right_click +from gramps.gui.utils import (color_graph_box, hex_to_rgb_float, + is_right_click, get_contrast_color) from gramps.gen.constfunc import lin from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext @@ -337,7 +338,8 @@ class PersonBoxWidgetCairo(_PersonWidgetBase): # text context.move_to(5, 4) - context.set_source_rgb(0, 0, 0) + fg_color = get_contrast_color(self.bgcolor) + context.set_source_rgb(*fg_color[:3]) PangoCairo.show_layout(context, self.textlayout) context.restore() context.get_target().flush()