From 80eb8d10f62aa582789fa387588c45bcd4821dbb Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 20 Sep 2020 12:50:46 +0000 Subject: [PATCH] Move color functions into the color module Signed-off-by: Michael Tremer --- src/collecty/colours.py | 52 +++++++++++++++++++++++++++++---- src/collecty/plugins/latency.py | 17 +++++------ src/collecty/util.py | 36 ----------------------- 3 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/collecty/colours.py b/src/collecty/colours.py index 21299e4..8d880a1 100644 --- a/src/collecty/colours.py +++ b/src/collecty/colours.py @@ -19,7 +19,49 @@ # # ############################################################################### -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, diff --git a/src/collecty/plugins/latency.py b/src/collecty/plugins/latency.py index 43ae475..89577ef 100644 --- a/src/collecty/plugins/latency.py +++ b/src/collecty/plugins/latency.py @@ -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", diff --git a/src/collecty/util.py b/src/collecty/util.py index 2cba52b..325a4c3 100644 --- a/src/collecty/util.py +++ b/src/collecty/util.py @@ -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 -- 2.39.2