From d9196304cae3c4b901e1fb7e2d966c6cefb525c3 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Fri, 7 Mar 2025 17:43:15 +1100 Subject: scheme: use material colours for layers & accents --- scheme/autoadjust.py | 53 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'scheme/autoadjust.py') diff --git a/scheme/autoadjust.py b/scheme/autoadjust.py index 991ac7e..03ca248 100755 --- a/scheme/autoadjust.py +++ b/scheme/autoadjust.py @@ -1,8 +1,8 @@ #!/bin/python3 import sys +from colorsys import hls_to_rgb, rgb_to_hls from math import sqrt -from colorsys import rgb_to_hls, hls_to_rgb light_colours = [ "dc8a78", @@ -38,27 +38,50 @@ dark_colours = [ "b4befe", ] -def hex_to_rgb(hex: str) -> tuple[int, int, int]: +colour_names = [ + "rosewater", + "flamingo", + "pink", + "mauve", + "red", + "maroon", + "peach", + "yellow", + "green", + "teal", + "sky", + "sapphire", + "blue", + "lavender", +] + + +def hex_to_rgb(hex: str) -> tuple[float, float, float]: """Convert a hex string to an RGB tuple in the range [0, 1].""" - return tuple(int(hex[i:i+2], 16) / 255 for i in (0, 2, 4)) + return tuple(int(hex[i : i + 2], 16) / 255 for i in (0, 2, 4)) + -def rgb_to_hex(rgb: tuple[int, int, int]) -> str: +def rgb_to_hex(rgb: tuple[float, float, float]) -> str: """Convert an RGB tuple in the range [0, 1] to a hex string.""" return "".join(f"{round(i * 255):02X}" for i in rgb) + def hex_to_hls(hex: str) -> tuple[float, float, float]: return rgb_to_hls(*hex_to_rgb(hex)) + def adjust(hex: str, light: float = 0, sat: float = 0) -> str: h, l, s = hex_to_hls(hex) l = max(0, min(1, l + light)) s = max(0, min(1, s + sat)) return rgb_to_hex(hls_to_rgb(h, l, s)) + def distance(colour: str, base: str) -> float: r1, g1, b1 = hex_to_rgb(colour) r2, g2, b2 = hex_to_rgb(base) - return sqrt((r2 - r1)**2 + (g2 - g1)**2 + (b2 - b1)**2) + return sqrt((r2 - r1) ** 2 + (g2 - g1) ** 2 + (b2 - b1) ** 2) + def smart_sort(colours: list[str], base: list[str]) -> list[str]: sorted_colours = [None] * len(colours) @@ -85,15 +108,20 @@ def smart_sort(colours: list[str], base: list[str]) -> list[str]: return [i[0] for i in sorted_colours] + def mix(a: str, b: str, w: float) -> str: r1, g1, b1 = hex_to_rgb(a) r2, g2, b2 = hex_to_rgb(b) - return rgb_to_hex((r1 * (1 - w) + r2 * w, g1 * (1 - w) + g2 * w, b1 * (1 - w) + b2 * w)) + return rgb_to_hex( + (r1 * (1 - w) + r2 * w, g1 * (1 - w) + g2 * w, b1 * (1 - w) + b2 * w) + ) + def mix_colours(colours: list[str], base: list[str], amount: float) -> list[str]: for i, b in enumerate(base): colours[i] = mix(colours[i], b, amount) + if __name__ == "__main__": light = sys.argv[1] == "light" @@ -101,19 +129,16 @@ if __name__ == "__main__": base = light_colours if light else dark_colours colours = [] - for colour in sys.argv[3].split(" ")[1:]: + for colour in sys.argv[3].split(" "): colours.append(adjust(colour, sat=added_sat)) # TODO: optional adjust colours = smart_sort(colours, base) # TODO: optional smart sort mix_colours(colours, base, 0) # TODO: customize mixing from config - for colour in colours: - print(colour) - - for layer in sys.argv[4:]: - print(layer.split(" ")[0]) + for i, colour in enumerate(colours): + print(f"{colour_names[i]} {colour}") # Success and error colours # TODO: customize mixing - print(mix(colours[8], base[8], 0.9)) # Success (green) - print(mix(colours[4], base[4], 0.9)) # Error (red) + print(f"success {mix(colours[8], base[8], 0.9)}") # Success (green) + print(f"error {mix(colours[4], base[4], 0.9)}") # Error (red) -- cgit v1.2.3-freya