1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Extend foreground_color() utility to support custom dark/light colors

This commit is contained in:
jeremystretch
2021-07-15 13:59:14 -04:00
parent 89662124e5
commit c5178fd90e
2 changed files with 8 additions and 5 deletions

View File

@ -44,16 +44,19 @@ def csv_format(data):
return ','.join(csv)
def foreground_color(bg_color):
def foreground_color(bg_color, dark='000000', light='ffffff'):
"""
Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
Return the ideal foreground color (dark or light) for a given background color in hexadecimal RGB format.
:param dark: RBG color code for dark text
:param light: RBG color code for light text
"""
bg_color = bg_color.strip('#')
r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
if r * 0.299 + g * 0.587 + b * 0.114 > 186:
return '000000'
return dark
else:
return 'ffffff'
return light
def dynamic_import(name):