Просмотр исходного кода

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

jeremystretch 4 лет назад
Родитель
Сommit
c5178fd90e
2 измененных файлов с 8 добавлено и 5 удалено
  1. 1 1
      netbox/dcim/svg.py
  2. 7 4
      netbox/utilities/utils.py

+ 1 - 1
netbox/dcim/svg.py

@@ -342,7 +342,7 @@ class CableTraceSVG:
         for i, label in enumerate(labels):
         for i, label in enumerate(labels):
             self.cursor += LINE_HEIGHT
             self.cursor += LINE_HEIGHT
             text_coords = (self.center, self.cursor - LINE_HEIGHT / 2)
             text_coords = (self.center, self.cursor - LINE_HEIGHT / 2)
-            text_color = f'#{foreground_color(color)}'
+            text_color = f'#{foreground_color(color, dark="303030")}'
             text = Text(label, insert=text_coords, fill=text_color, class_='bold' if not i else [])
             text = Text(label, insert=text_coords, fill=text_color, class_='bold' if not i else [])
             link.add(text)
             link.add(text)
 
 

+ 7 - 4
netbox/utilities/utils.py

@@ -44,16 +44,19 @@ def csv_format(data):
     return ','.join(csv)
     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('#')
     bg_color = bg_color.strip('#')
     r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
     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:
     if r * 0.299 + g * 0.587 + b * 0.114 > 186:
-        return '000000'
+        return dark
     else:
     else:
-        return 'ffffff'
+        return light
 
 
 
 
 def dynamic_import(name):
 def dynamic_import(name):