Files
2026-08-31 00:44:14 +05:30

129 lines
3.2 KiB
Python

import sys
from pathlib import Path
from string import Template
from coloraide import Color
'''
===
Get absolute path to any resource, you need this to run directly from source, or as a standalone
executable generated using PyInstaller.
===
'''
def get_resource_path(relative_path: str) -> Path:
try:
# PyInstaller creates a temporary folder and stores its path in sys._MEIPASS
base_path = Path(sys._MEIPASS)
except AttributeError:
# Fall back to standard filesystem directory during local development
base_path = Path(__file__).resolve().parent
return base_path / relative_path
'''
===
Get the lightness of a color in OKLab color space
===
'''
def get_lightness(hex_str):
col = Color(f"#{hex_str}")
oklab_col = col.convert("oklab")
return oklab_col.get("l")
'''
===
Generate a lower color based on the base color and a factor
===
'''
def gen_lower_color(rgb_base, factor):
col_base = Color(f"#{rgb_base}")
oklab_base = col_base.convert("oklab")
light_base = oklab_base.get("l")
light_out = 1 -((1 - light_base) * factor)
oklab_out = oklab_base.set("l", light_out)
return oklab_out.convert("srgb").to_string(hex=True)
'''
===
Generate an upper color based on the base color and a factor
===
'''
def gen_upper_color(rgb_base, factor):
col_base = Color(f"#{rgb_base}")
oklab_base = col_base.convert("oklab")
light_base = oklab_base.get("l")
light_out = light_base * (1.00 - factor)
oklab_out = oklab_base.set("l", light_out)
return oklab_out.convert("srgb").to_string(hex=True)
'''
===
Write an output file with the generated palette colors
===
'''
def write_palette(color_list, tpl_file_path, output_file_path):
tpath = get_resource_path(tpl_file_path)
with open(tpath, 'r', encoding='utf-8') as file:
tplcnt = file.read()
template = Template(tplcnt)
# Create a dictionary for the template substitution
subst_dict = {}
for i, (name, hex_val, lightness) in enumerate(color_list):
subst_dict[name] = hex_val.upper()
subst_dict[f"{name}_light"] = f"{lightness:.2f}"
# Convert hex to RGB format
hex_clean = hex_val.lstrip('#')
r = int(hex_clean[0:2], 16)
g = int(hex_clean[2:4], 16)
b = int(hex_clean[4:6], 16)
subst_dict[f"{name}_rgb"] = f"rgb({r}, {g}, {b})"
# Substitute the values in the template
outcnt = template.safe_substitute(subst_dict)
with open(output_file_path, "w") as output_file:
output_file.write(outcnt)
'''
===
The main function to process the input color and generate the palette
===
'''
def main():
if len(sys.argv) < 2:
print("Usage: quadricolor.py <base_color> [output_file]")
print(" base_color: Hex color code (e.g., FF6B6B or #FF6B6B)")
print(" output_file: Output file name (default: output.html)")
sys.exit(1)
rgb_base = sys.argv[1].lstrip('#')
output_file = sys.argv[2] if len(sys.argv) > 2 else "output.html"
color1 = gen_lower_color(rgb_base, 0.05)
color2 = gen_lower_color(rgb_base, 0.5)
color3 = gen_upper_color(rgb_base, 0.4)
gencolors = [
("color1", color1, get_lightness(color1.lstrip('#'))),
("color2", color2, get_lightness(color2.lstrip('#'))),
("color_base", f"#{rgb_base}", get_lightness(rgb_base)),
("color3", color3, get_lightness(color3.lstrip('#'))),
]
write_palette(gencolors, "resources/quadcol.tpl", output_file)
if __name__ == "__main__":
main()