From f4a22e5af37027fb16d86a8337f1e52828b9ec35 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 19 Dec 2018 12:17:40 -0500 Subject: [PATCH] Introduced fgcolor template filter to render ideal foreground color for any background color --- netbox/dcim/tables.py | 3 ++- netbox/utilities/templatetags/helpers.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/netbox/dcim/tables.py b/netbox/dcim/tables.py index 0c88852f067..0ab1d594c42 100644 --- a/netbox/dcim/tables.py +++ b/netbox/dcim/tables.py @@ -29,7 +29,8 @@ """ COLOR_LABEL = """ - +{% load helpers %} + """ DEVICE_LINK = """ diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index e7bc408465c..11fa789ec0f 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -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() @@ -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 #