From 916df3c614aeecd976b01bda429f6a52f44e6ff1 Mon Sep 17 00:00:00 2001 From: Nate Butler Date: Mon, 13 Nov 2023 15:28:20 -0500 Subject: [PATCH] Add color converter util --- crates/theme2/util/hex_to_hsla.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 crates/theme2/util/hex_to_hsla.py diff --git a/crates/theme2/util/hex_to_hsla.py b/crates/theme2/util/hex_to_hsla.py new file mode 100644 index 0000000000000000000000000000000000000000..f741b9336589f35f9f7b8371579344a58b6ddfd3 --- /dev/null +++ b/crates/theme2/util/hex_to_hsla.py @@ -0,0 +1,20 @@ +import colorsys +import sys + +def hex_to_rgb(hex): + hex = hex.lstrip('#') + return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)) + +def rgb_to_hsla(rgb): + h, l, s = colorsys.rgb_to_hls(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0) + return (round(h * 360, 1), round(s * 100, 1), round(l * 100, 1), 1.0) + +def hex_to_hsla(hex): + return rgb_to_hsla(hex_to_rgb(hex)) + +if len(sys.argv) != 2: + print("Usage: python util/hex_to_hsla.py ") +else: + hex_color = sys.argv[1] + h, s, l, a = hex_to_hsla(hex_color) + print(f"hsla({h} / 360., {s} / 100., {l} / 100., {a})")