From 611f89656b27ca567e6657b1d37edd39755437bd Mon Sep 17 00:00:00 2001 From: lelgenio Date: Wed, 2 Dec 2020 23:56:16 -0300 Subject: [PATCH] format and add lighter/darker filters --- funcs.py | 61 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/funcs.py b/funcs.py index 70bb64b..620af52 100644 --- a/funcs.py +++ b/funcs.py @@ -18,23 +18,6 @@ def ordered_path(): return ':'.join(newPATH) -def hex2rgb(e): - assert e.startswith("#") - e = e.strip("#").lower() - assert len(e) == 6 - for i in e: - assert (i in "0123456789abcdef") - - def h2r(i): - return str(eval('0x{}'.format(i))) - - r = e[0:2] - g = e[2:4] - b = e[4:6] - - return ", ".join([h2r(i) for i in (r, g, b)]) - - def rclone_obscure(pass_name): try: fPath = os.path.expanduser("~/.config/rclone/rclone.conf") @@ -50,3 +33,47 @@ def rclone_obscure(pass_name): return check_output(args).decode().strip() return sh("rclone", "obscure", sh("_get-pass", pass_name)) + + +#################################################################### +# color +#################################################################### + +def split_hex(e): + e = e.lstrip("#").lower() + return e[0:2], e[2:4], e[4:6] + + +def hex2rgb(e): + return ", ".join([str(int(s, base=16)) + for s in split_hex(e)]) + + +def color_mult(e: str, amount: float): + """\ + Multiply e hex rgb colorCode with amount number + Usefull for lightening/darkening + """ + + def segment(s): + base256 = int(s, base=16) + result_val = base256 * amount + + hex_result = hex(int(result_val))[2:] + + return str(hex_result) + + prefix = "#" * e.startswith("#") + return prefix + "".join(map(segment, split_hex(e))) + + +#################################################################### +# Filters +#################################################################### + +def darker(arg1, amount=0.75): + return color_mult(arg1, amount) + + +def lighter(arg1, amount=1.25): + return color_mult(arg1, amount)