]> git.ipfire.org Git - oddments/collecty.git/commitdiff
Move color functions into the color module
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 20 Sep 2020 12:50:46 +0000 (12:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 20 Sep 2020 12:50:46 +0000 (12:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/colours.py
src/collecty/plugins/latency.py
src/collecty/util.py

index 21299e43bef141c1e630d438bf65272d823a4c40..8d880a135f923c3fdd8dd04515ce93d50790ab6d 100644 (file)
 #                                                                             #
 ###############################################################################
 
-from . import util
+def _add(colour, amount):
+       """
+               Adds some value to colours
+       """
+       # Parse hex array
+       bytes = bytearray.fromhex(colour.lstrip("#"))
+
+       if not len(bytes) == 3:
+               raise ValueError("Invalid colour: %s" % colour)
+
+       ret = bytearray()
+
+       for byte in bytes:
+               byte = round(byte * amount)
+
+               # Ensure the result is within range
+               byte = min(byte, 255)
+               byte = max(byte, 0)
+
+               # Update the array
+               ret.append(byte)
+
+       return "#%s" % ret.hex()
+
+def lighten(colour, scale=0.1):
+       """
+               Takes a hexadecimal colour code
+               and brightens the colour.
+       """
+       return _add(colour, scale)
+
+def darken(colour, scale=0.1):
+       """
+               Takes a hexadecimal colour code
+               and darkens the colour.
+       """
+       return _add(colour, -scale)
+
+def transparency(colour, scale=0.1):
+       """
+               Adds transparency to the given colour code
+       """
+       return "%s%02X" % (colour, 0xff * scale)
 
 BLACK        = "#000000"
 WHITE        = "#FFFFFF"
@@ -51,7 +93,7 @@ COLOUR_OK       = LIGHT_GREEN
 COLOUR_CRITICAL = LIGHT_RED
 COLOUR_ERROR    = COLOUR_CRITICAL
 COLOUR_WARN     = LIGHT_YELLOW
-COLOUR_TEXT     = util.lighten(BLACK, 0.87) # 87% grey
+COLOUR_TEXT     = lighten(BLACK, 0.87) # 87% grey
 
 PRIMARY      = INDIGO
 ACCENT       = PINK
@@ -111,7 +153,7 @@ COLOURS_PROTOCOL_STATES = {
 
        # TCP
        "CLOSE"             : BLACK,
-       "CLOSE_WAIT"        : util.lighten(BLACK, 0.25),
+       "CLOSE_WAIT"        : lighten(BLACK, 0.25),
        "ESTABLISHED"       : LIGHT_GREEN,
        "FIN_WAIT"          : ORANGE,
        "LAST_ACK"          : PURPLE,
@@ -120,8 +162,8 @@ COLOURS_PROTOCOL_STATES = {
        "SYN_SENT2"         : AMBER,
 
        # DCCP
-       "CLOSEREQ"          : util.lighten(BLACK, 0.5),
-       "CLOSING"           : util.lighten(BLACK, 0.25),
+       "CLOSEREQ"          : lighten(BLACK, 0.5),
+       "CLOSING"           : lighten(BLACK, 0.25),
        "IGNORE"            : WHITE,
        "INVALID"           : RED,
        "OPEN"              : LIGHT_GREEN,
index 43ae475e51fc0d942b590a0f067486f6d6e9a668..89577ef3f04b84d26caae82afd12647d168397b6 100644 (file)
@@ -24,7 +24,6 @@ import socket
 import collecty._collecty
 from . import base
 
-from .. import util
 from ..colours import *
 from ..i18n import _
 
@@ -64,40 +63,40 @@ class GraphTemplateLatency(base.GraphTemplate):
                        "CDEF:l099=ploss,50,99,LIMIT,UN,UNKN,INF,IF",
 
                        "LINE2:latency6_avg%s:%s" % (
-                               util.transparency(COLOUR_IPV6, .5),
+                               transparency(COLOUR_IPV6, .5),
                                _("Average latency (IPv6)"),
                        ),
                        "LINE2:latency4_avg%s:%s\\r" % (
-                               util.transparency(COLOUR_IPV4, .5),
+                               transparency(COLOUR_IPV4, .5),
                                _("Average latency (IPv4)"),
                        ),
 
                        "COMMENT:%s" % _("Packet Loss"),
                        "AREA:l005%s:%s" % (
-                               util.transparency(colour_bg, .2), _("0-5%"),
+                               transparency(colour_bg, .2), _("0-5%"),
                        ),
                        "AREA:l010%s:%s" % (
-                               util.transparency(colour_bg, .4), _("5-10%"),
+                               transparency(colour_bg, .4), _("5-10%"),
                        ),
                        "AREA:l025%s:%s" % (
-                               util.transparency(colour_bg, .6), _("10-25%"),
+                               transparency(colour_bg, .6), _("10-25%"),
                        ),
                        "AREA:l050%s:%s" % (
-                               util.transparency(colour_bg, .8), _("25-50%"),
+                               transparency(colour_bg, .8), _("25-50%"),
                        ),
                        "AREA:l099%s:%s\\r" % (colour_bg, _("50-99%")),
 
                        "COMMENT: \\n", # empty line
 
                        "AREA:spacer4",
-                       "AREA:stddevarea4%s:STACK" % util.lighten(COLOUR_IPV4, STDDEV_OPACITY),
+                       "AREA:stddevarea4%s:STACK" % lighten(COLOUR_IPV4, STDDEV_OPACITY),
                        "LINE2:latency4%s:%s" % (COLOUR_IPV4, _("Latency (IPv4)")),
                        "GPRINT:latency4_max:%12s\:" % _("Maximum") + " %6.2lf",
                        "GPRINT:latency4_min:%12s\:" % _("Minimum") + " %6.2lf",
                        "GPRINT:latency4_avg:%12s\:" % _("Average") + " %6.2lf\\n",
 
                        "AREA:spacer6",
-                       "AREA:stddevarea6%s:STACK" % util.lighten(COLOUR_IPV6, STDDEV_OPACITY),
+                       "AREA:stddevarea6%s:STACK" % lighten(COLOUR_IPV6, STDDEV_OPACITY),
                        "LINE2:latency6%s:%s" % (COLOUR_IPV6, _("Latency (IPv6)")),
                        "GPRINT:latency6_max:%12s\:" % _("Maximum") + " %6.2lf",
                        "GPRINT:latency6_min:%12s\:" % _("Minimum") + " %6.2lf",
index 2cba52b833aa38cd7dc406dd11f0e7bfb775c4a0..325a4c328acc77fc3af41795de2a3c86cfd848b5 100644 (file)
@@ -27,42 +27,6 @@ log.propagate = 1
 
 from .constants import *
 
-def __add_colour(colour, amount):
-       colour = colour.strip("#")
-
-       colour = (
-               int(colour[0:2], 16),
-               int(colour[2:4], 16),
-               int(colour[4:6], 16),
-       )
-
-       # Scale the colour
-       colour = (e + amount for e in colour)
-       colour = (max(e, 0) for e in colour)
-       colour = (min(e, 255) for e in colour)
-
-       return "#%02x%02x%02x" % tuple(colour)
-
-def lighten(colour, scale=0.1):
-       """
-               Takes a hexadecimal colour code
-               and brightens the colour.
-       """
-       return __add_colour(colour, 0xff * scale)
-
-def darken(colour, scale=0.1):
-       """
-               Takes a hexadecimal colour code
-               and darkens the colour.
-       """
-       return __add_colour(colour, 0xff * -scale)
-
-def transparency(colour, scale=0.1):
-       """
-               Adds transparency to the given colour code
-       """
-       return "%s%02X" % (colour, 0xff * scale)
-
 def get_network_interfaces():
        """
                Returns all real network interfaces