1
0
Fork 0
mirror of https://github.com/topydo/topydo.git synced 2024-05-20 13:58:33 +00:00

Choose appropriate font color given a background color

Formula for color brightness obtained from
https://www.w3.org/TR/AERT#color-contrast
This commit is contained in:
Bram Schoenmakers 2016-06-02 12:52:00 +02:00
parent 851e2cdd8b
commit 71c7097006
2 changed files with 27 additions and 3 deletions

View file

@ -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)
)

View file

@ -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