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:
return Color.html_color_dict[self.color]
except KeyError:
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):
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))
result = 'digraph topydo {\n'
......@@ -89,13 +100,13 @@ class DotPrinter(Printer):
# print 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(
node_name(todo),
node_label(todo),
color,
'#000000',
background_color.as_html(),
foreground(background_color),
)
# 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