Commit 71c70970 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Choose appropriate font color given a background color

Formula for color brightness obtained from
https://www.w3.org/TR/AERT#color-contrast
parent 851e2cdd
...@@ -178,3 +178,16 @@ class Color: ...@@ -178,3 +178,16 @@ class Color:
return Color.html_color_dict[self.color] return Color.html_color_dict[self.color]
except KeyError: except KeyError:
return '#ffffff' return '#ffffff'
def as_rgb(self):
"""
Returns a tuple (r, g, b) of the color.
"""
html = self.as_html()
return (
int(html[1:3], 16),
int(html[3:5], 16),
int(html[5:7], 16)
)
...@@ -82,6 +82,17 @@ class DotPrinter(Printer): ...@@ -82,6 +82,17 @@ class DotPrinter(Printer):
return node_result return node_result
def foreground(p_background):
"""
Chooses a suitable foreground color (black or white) given a
background color.
"""
(r, g, b) = p_background.as_rgb()
brightness = (r * 299 + g * 587 + b * 114) / ( 255 * 1000 )
return '#ffffff' if brightness < 0.5 else '#000000'
node_name = lambda t: '_' + str(self.todolist.number(t)) node_name = lambda t: '_' + str(self.todolist.number(t))
result = 'digraph topydo {\n' result = 'digraph topydo {\n'
...@@ -89,13 +100,13 @@ class DotPrinter(Printer): ...@@ -89,13 +100,13 @@ class DotPrinter(Printer):
# print todos # print todos
for todo in p_todos: for todo in p_todos:
color = progress_color(todo).as_html() background_color = progress_color(todo)
result += ' {} [label={} style=filled fillcolor="{}" fontcolor="{}"]\n'.format( result += ' {} [label={} style=filled fillcolor="{}" fontcolor="{}"]\n'.format(
node_name(todo), node_name(todo),
node_label(todo), node_label(todo),
color, background_color.as_html(),
'#000000', foreground(background_color),
) )
# print edges # print edges
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment