scheme: use materialyoucolor blend

Harmonize colours
Also use harmonize for success and error
This commit is contained in:
2 * r + 2 * t
2025-04-22 19:53:45 +10:00
parent ce6fdf0ab9
commit 1d60df6aa1
+44 -31
View File
@@ -4,6 +4,7 @@ import sys
from colorsys import hls_to_rgb, rgb_to_hls from colorsys import hls_to_rgb, rgb_to_hls
from pathlib import Path from pathlib import Path
from materialyoucolor.blend import Blend
from materialyoucolor.dynamiccolor.material_dynamic_colors import ( from materialyoucolor.dynamiccolor.material_dynamic_colors import (
DynamicScheme, DynamicScheme,
MaterialDynamicColors, MaterialDynamicColors,
@@ -93,6 +94,14 @@ def hls_to_hex(h: str, l: str, s: str) -> str:
return rgb_to_hex(hls_to_rgb(h, l, s)) return rgb_to_hex(hls_to_rgb(h, l, s))
def hex_to_argb(hex: str) -> int:
return int(f"0xFF{hex}", 16)
def argb_to_hls(argb: int) -> HLS:
return hex_to_hls(f"{argb:08X}"[2:])
def grayscale(hls: HLS, light: bool) -> HLS: def grayscale(hls: HLS, light: bool) -> HLS:
h, l, s = hls h, l, s = hls
return h, 0.5 - l / 2 if light else l / 2 + 0.5, 0 return h, 0.5 - l / 2 if light else l / 2 + 0.5, 0
@@ -106,6 +115,10 @@ def mix(a: HLS, b: HLS, w: float) -> HLS:
) )
def harmonize(a: str, b: int) -> HLS:
return argb_to_hls(Blend.harmonize(hex_to_argb(a), b))
def darken(colour: HLS, amount: float) -> HLS: def darken(colour: HLS, amount: float) -> HLS:
h, l, s = colour h, l, s = colour
return h, max(0, l - amount), s return h, max(0, l - amount), s
@@ -176,21 +189,20 @@ if __name__ == "__main__":
out_path = sys.argv[5] out_path = sys.argv[5]
base = light_colours if light else dark_colours base = light_colours if light else dark_colours
chroma_mult = 1.5 if light else 1.2
# Convert to HLS # Convert to HLS
colours = [hex_to_hls(c) for c in colours_in] base_colours = [hex_to_hls(c) for c in colours_in]
# Sort colours and turn into dict # Sort colours and turn into dict
colours = smart_sort(colours, base) base_colours = smart_sort(base_colours, base)
# Adjust colours # Adjust colours
MatScheme = get_scheme(scheme) MatScheme = get_scheme(scheme)
for name, hls in colours.items(): for name, hls in base_colours.items():
if scheme == "monochrome": if scheme == "monochrome":
colours[name] = grayscale(hls, light) base_colours[name] = grayscale(hls, light)
else: else:
argb = int(f"0xFF{hls_to_hex(*hls)}", 16) argb = hex_to_argb(hls_to_hex(*hls))
mat_scheme = MatScheme(Hct.from_int(argb), not light, 0) mat_scheme = MatScheme(Hct.from_int(argb), not light, 0)
colour = MaterialDynamicColors.primary.get_hct(mat_scheme) colour = MaterialDynamicColors.primary.get_hct(mat_scheme)
@@ -199,43 +211,44 @@ if __name__ == "__main__":
if scheme == "neutral": if scheme == "neutral":
colour.chroma += 10 colour.chroma += 10
colour.chroma *= chroma_mult base_colours[name] = hex_to_hls(
colours[name] = hex_to_hls(
"{:02X}{:02X}{:02X}".format(*colour.to_rgba()[:3]) "{:02X}{:02X}{:02X}".format(*colour.to_rgba()[:3])
) )
# Success and error colours
colours["success"] = mix(colours["green"], hex_to_hls(base[8]), 0.8)
colours["error"] = mix(colours["red"], hex_to_hls(base[4]), 0.8)
# Layers and accents # Layers and accents
for i, primary in enumerate(primaries): for i, primary in enumerate(primaries):
material = {} material = {}
primary_scheme = MatScheme(
Hct.from_int(int(f"0xFF{primary}", 16)), not light, 0 primary_argb = hex_to_argb(primary)
) primary_scheme = MatScheme(Hct.from_int(primary_argb), not light, 0)
for colour in vars(MaterialDynamicColors).keys(): for colour in vars(MaterialDynamicColors).keys():
colour_name = getattr(MaterialDynamicColors, colour) colour_name = getattr(MaterialDynamicColors, colour)
if hasattr(colour_name, "get_hct"): if hasattr(colour_name, "get_hct"):
rgb = colour_name.get_hct(primary_scheme).to_rgba()[:3] rgb = colour_name.get_hct(primary_scheme).to_rgba()[:3]
material[colour] = hex_to_hls("{:02X}{:02X}{:02X}".format(*rgb)) material[colour] = hex_to_hls("{:02X}{:02X}{:02X}".format(*rgb))
colours["primary"] = material["primary"] colours = {
colours["secondary"] = material["secondary"] "primary": material["primary"],
colours["tertiary"] = material["tertiary"] "secondary": material["secondary"],
colours["text"] = material["onBackground"] "tertiary": material["tertiary"],
colours["subtext1"] = material["onSurfaceVariant"] "text": material["onBackground"],
colours["subtext0"] = material["outline"] "subtext1": material["onSurfaceVariant"],
colours["overlay2"] = mix(material["surface"], material["outline"], 0.86) "subtext0": material["outline"],
colours["overlay1"] = mix(material["surface"], material["outline"], 0.71) "overlay2": mix(material["surface"], material["outline"], 0.86),
colours["overlay0"] = mix(material["surface"], material["outline"], 0.57) "overlay1": mix(material["surface"], material["outline"], 0.71),
colours["surface2"] = mix(material["surface"], material["outline"], 0.43) "overlay0": mix(material["surface"], material["outline"], 0.57),
colours["surface1"] = mix(material["surface"], material["outline"], 0.29) "surface2": mix(material["surface"], material["outline"], 0.43),
colours["surface0"] = mix(material["surface"], material["outline"], 0.14) "surface1": mix(material["surface"], material["outline"], 0.29),
colours["base"] = material["surface"] "surface0": mix(material["surface"], material["outline"], 0.14),
colours["mantle"] = darken(material["surface"], 0.03) "base": material["surface"],
colours["crust"] = darken(material["surface"], 0.05) "mantle": darken(material["surface"], 0.03),
"crust": darken(material["surface"], 0.05),
"success": harmonize(base[8], primary_argb),
"error": harmonize(base[4], primary_argb),
}
for name, hls in base_colours.items():
colours[name] = harmonize(hls_to_hex(*hls), primary_argb)
out_file = Path(f"{out_path}/{scheme}/{get_alt(i)}/{sys.argv[1]}.txt") out_file = Path(f"{out_path}/{scheme}/{get_alt(i)}/{sys.argv[1]}.txt")
out_file.parent.mkdir(parents=True, exist_ok=True) out_file.parent.mkdir(parents=True, exist_ok=True)