Introduced fgcolor template filter to render ideal foreground color for any background color

This commit is contained in:
Jeremy Stretch 2018-12-19 12:17:40 -05:00
parent aca57ec281
commit f4a22e5af3
2 changed files with 15 additions and 1 deletions

View File

@ -29,7 +29,8 @@ SITE_REGION_LINK = """
""" """
COLOR_LABEL = """ COLOR_LABEL = """
<label class="label" style="background-color: #{{ record.color }}">{{ record }}</label> {% load helpers %}
<label class="label" style="color: {{ record.color|fgcolor }}; background-color: #{{ record.color }}">{{ record }}</label>
""" """
DEVICE_LINK = """ DEVICE_LINK = """

View File

@ -1,11 +1,13 @@
import datetime import datetime
import json import json
import re
from django import template from django import template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from markdown import markdown from markdown import markdown
from utilities.forms import unpack_grouped_choices from utilities.forms import unpack_grouped_choices
from utilities.utils import foreground_color
register = template.Library() register = template.Library()
@ -152,6 +154,17 @@ def tzoffset(value):
return datetime.datetime.now(value).strftime('%z') return datetime.datetime.now(value).strftime('%z')
@register.filter()
def fgcolor(value):
"""
Return black (#000000) or white (#ffffff) given an arbitrary background color in RRGGBB format.
"""
value = value.lower().strip('#')
if not re.match('^[0-9a-f]{6}$', value):
return ''
return '#{}'.format(foreground_color(value))
# #
# Tags # Tags
# #