Skip to content

Commit

Permalink
Introduced fgcolor template filter to render ideal foreground color f…
Browse files Browse the repository at this point in the history
…or any background color
  • Loading branch information
jeremystretch committed Dec 19, 2018
1 parent aca57ec commit f4a22e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion netbox/dcim/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"""

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 = """
Expand Down
13 changes: 13 additions & 0 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import datetime
import json
import re

from django import template
from django.utils.safestring import mark_safe
from markdown import markdown

from utilities.forms import unpack_grouped_choices
from utilities.utils import foreground_color


register = template.Library()
Expand Down Expand Up @@ -152,6 +154,17 @@ def tzoffset(value):
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
#
Expand Down

0 comments on commit f4a22e5

Please sign in to comment.