Color Fix Script update
This commit is contained in:
+104
-78
@@ -1,107 +1,133 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
blob_color_fixer.py — Detects monotone/bland wal color palettes and replaces
|
||||||
|
accent colors with vibrant, harmonically-spread alternatives.
|
||||||
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import math
|
||||||
import colorsys
|
import colorsys
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
def hex_to_rgb(hex_str):
|
MIN_SATURATION = 0.65
|
||||||
hex_str = hex_str.lstrip('#')
|
MIN_LIGHTNESS = 0.40
|
||||||
return tuple(int(hex_str[i:i+2], 16) / 255.0 for i in (0, 2, 4))
|
MAX_LIGHTNESS = 0.70
|
||||||
|
HUE_THRESHOLD = 0.15
|
||||||
|
SAT_THRESHOLD = 0.35
|
||||||
|
HUE_VARIANCE_THRESHOLD = 0.15
|
||||||
|
ACCENT_SLICE = slice(1, 7)
|
||||||
|
BRIGHT_OFFSET = 8
|
||||||
|
|
||||||
def rgb_to_hex(r, g, b):
|
HUE_SHIFTS: list[float] = [
|
||||||
return '#{:02x}{:02x}{:02x}'.format(int(r * 255), int(g * 255), int(b * 255))
|
0.0,
|
||||||
|
0.5,
|
||||||
|
0.083,
|
||||||
|
-0.083,
|
||||||
|
0.416,
|
||||||
|
-0.416,
|
||||||
|
]
|
||||||
|
|
||||||
def shift_hue_and_saturate(hex_str, shift_amount):
|
@dataclass(frozen=True)
|
||||||
r, g, b = hex_to_rgb(hex_str)
|
class HLS:
|
||||||
h, l, s = colorsys.rgb_to_hls(r, g, b)
|
h: float
|
||||||
h = (h + shift_amount) % 1.0
|
l: float
|
||||||
s = max(s, 0.65) # Force vibrancy
|
s: float
|
||||||
l = min(max(l, 0.4), 0.7) # Ensure it's not too dark or overly washed out
|
|
||||||
r, g, b = colorsys.hls_to_rgb(h, l, s)
|
|
||||||
return rgb_to_hex(r, g, b)
|
|
||||||
|
|
||||||
def get_hls_stats(hex_list):
|
|
||||||
hues = []
|
|
||||||
sats = []
|
|
||||||
for hex_str in hex_list:
|
|
||||||
r, g, b = hex_to_rgb(hex_str)
|
|
||||||
h, l, s = colorsys.rgb_to_hls(r, g, b)
|
|
||||||
hues.append(h)
|
|
||||||
sats.append(s)
|
|
||||||
return hues, sats
|
|
||||||
|
|
||||||
def is_monotone_or_bland(hues, sats, hue_threshold=0.15, sat_threshold=0.35):
|
def hex_to_rgb(hex_str: str) -> tuple[float, float, float]:
|
||||||
if not hues or not sats: return False
|
hex_str = hex_str.lstrip("#")
|
||||||
|
return tuple(int(hex_str[i : i + 2], 16) / 255.0 for i in (0, 2, 4)) # type: ignore[return-value]
|
||||||
|
|
||||||
# Calculate hue variance
|
|
||||||
min_h = min(hues)
|
|
||||||
max_h = max(hues)
|
|
||||||
hue_diff = min(max_h - min_h, 1.0 - (max_h - min_h))
|
|
||||||
|
|
||||||
# Calculate average saturation
|
def rgb_to_hex(r: float, g: float, b: float) -> str:
|
||||||
avg_s = sum(sats) / len(sats)
|
return "#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
|
||||||
|
|
||||||
return hue_diff < hue_threshold or avg_s < sat_threshold
|
|
||||||
|
|
||||||
def main():
|
def hex_to_hls(hex_str: str) -> HLS:
|
||||||
if len(sys.argv) < 2:
|
h, l, s = colorsys.rgb_to_hls(*hex_to_rgb(hex_str))
|
||||||
print("Usage: python3 blob_color_fixer.py <path_to_wal_colors>")
|
return HLS(h, l, s)
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
filepath = sys.argv[1]
|
|
||||||
|
|
||||||
|
def vibrant_shift(hex_str: str, hue_shift: float) -> str:
|
||||||
|
"""Return *hex_str* with its hue rotated by *hue_shift* and vibrancy enforced."""
|
||||||
|
hls = hex_to_hls(hex_str)
|
||||||
|
h = (hls.h + hue_shift) % 1.0
|
||||||
|
s = max(hls.s, MIN_SATURATION)
|
||||||
|
l = min(max(hls.l, MIN_LIGHTNESS), MAX_LIGHTNESS)
|
||||||
|
return rgb_to_hex(*colorsys.hls_to_rgb(h, l, s))
|
||||||
|
|
||||||
|
|
||||||
|
def palette_is_bland(accents: list[str]) -> bool:
|
||||||
|
"""Return True when accent colors are too similar, clustered, or too desaturated."""
|
||||||
|
stats = [hex_to_hls(c) for c in accents]
|
||||||
|
hues = [hls.h for hls in stats]
|
||||||
|
sats = [hls.s for hls in stats]
|
||||||
|
|
||||||
|
raw_spread = max(hues) - min(hues)
|
||||||
|
hue_spread = min(raw_spread, 1.0 - raw_spread)
|
||||||
|
avg_sat = sum(sats) / len(sats)
|
||||||
|
|
||||||
|
if hue_spread < HUE_THRESHOLD or avg_sat < SAT_THRESHOLD:
|
||||||
|
return True
|
||||||
|
|
||||||
|
mean_sin = sum(math.sin(2 * math.pi * h) for h in hues) / len(hues)
|
||||||
|
mean_cos = sum(math.cos(2 * math.pi * h) for h in hues) / len(hues)
|
||||||
|
hue_variance = 1.0 - math.sqrt(mean_sin ** 2 + mean_cos ** 2)
|
||||||
|
if hue_variance < HUE_VARIANCE_THRESHOLD:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def most_saturated(accents: list[str]) -> str:
|
||||||
|
return max(accents, key=lambda c: hex_to_hls(c).s)
|
||||||
|
|
||||||
|
|
||||||
|
def load_colors(filepath: str) -> list[str]:
|
||||||
try:
|
try:
|
||||||
with open(filepath, 'r') as f:
|
with open(filepath) as f:
|
||||||
colors = [line.strip() for line in f if line.strip()]
|
colors = [line.strip() for line in f if line.strip()]
|
||||||
except Exception as e:
|
except OSError as exc:
|
||||||
print(f"Error reading colors: {e}")
|
sys.exit(f"Error reading '{filepath}': {exc}")
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if len(colors) < 16:
|
if len(colors) < 16:
|
||||||
print("Not enough colors found in file.")
|
sys.exit(f"Expected ≥ 16 colors in '{filepath}', found {len(colors)}.")
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Check hue variance and saturation of accent colors (indices 1-6)
|
return colors
|
||||||
accents = colors[1:7]
|
|
||||||
hues, sats = get_hls_stats(accents)
|
|
||||||
|
|
||||||
if is_monotone_or_bland(hues, sats, hue_threshold=0.15, sat_threshold=0.35):
|
|
||||||
print("Detected monotone or bland palette. Generating vibrant complementary colors...")
|
|
||||||
|
|
||||||
# Pick the most saturated accent as the base
|
def save_colors(filepath: str, colors: list[str]) -> None:
|
||||||
base_accent = accents[-1]
|
try:
|
||||||
max_sat = 0
|
with open(filepath, "w") as f:
|
||||||
for i, sat in enumerate(sats):
|
f.write("\n".join(colors) + "\n")
|
||||||
if sat > max_sat:
|
except OSError as exc:
|
||||||
max_sat = sat
|
sys.exit(f"Error writing '{filepath}': {exc}")
|
||||||
base_accent = accents[i]
|
|
||||||
|
|
||||||
# Generate new colors with forced vibrancy
|
|
||||||
comp = shift_hue_and_saturate(base_accent, 0.5)
|
|
||||||
ana1 = shift_hue_and_saturate(base_accent, 0.083)
|
|
||||||
ana2 = shift_hue_and_saturate(base_accent, -0.083)
|
|
||||||
split1 = shift_hue_and_saturate(base_accent, 0.416)
|
|
||||||
split2 = shift_hue_and_saturate(base_accent, -0.416)
|
|
||||||
|
|
||||||
# The base accent itself also gets saturated if it was too bland
|
def main() -> None:
|
||||||
r, g, b = hex_to_rgb(base_accent)
|
if len(sys.argv) < 2:
|
||||||
h, l, s = colorsys.rgb_to_hls(r, g, b)
|
sys.exit("Usage: blob_color_fixer.py <path_to_wal_colors>")
|
||||||
if s < 0.65:
|
|
||||||
base_vibrant = shift_hue_and_saturate(base_accent, 0.0)
|
|
||||||
else:
|
|
||||||
base_vibrant = base_accent
|
|
||||||
|
|
||||||
new_accents = [base_vibrant, comp, ana1, ana2, split1, split2]
|
filepath = sys.argv[1]
|
||||||
|
colors = load_colors(filepath)
|
||||||
|
accents = colors[ACCENT_SLICE]
|
||||||
|
|
||||||
# Replace normal and bright accents
|
if not palette_is_bland(accents):
|
||||||
for i in range(6):
|
print("Palette is already vibrant and diverse — nothing to do.")
|
||||||
colors[i+1] = new_accents[i]
|
return
|
||||||
colors[i+9] = new_accents[i]
|
|
||||||
|
print("Monotone / bland palette detected. Generating vibrant harmony…")
|
||||||
|
|
||||||
|
base = most_saturated(accents)
|
||||||
|
new_accents = [vibrant_shift(base, shift) for shift in HUE_SHIFTS]
|
||||||
|
|
||||||
|
for i, color in enumerate(new_accents):
|
||||||
|
colors[i + 1] = color
|
||||||
|
colors[i + 1 + BRIGHT_OFFSET] = color
|
||||||
|
|
||||||
|
save_colors(filepath, colors)
|
||||||
|
print("Done — colors enhanced and written back to file.")
|
||||||
|
|
||||||
with open(filepath, 'w') as f:
|
|
||||||
for color in colors:
|
|
||||||
f.write(f"{color}\n")
|
|
||||||
print("Colors successfully enhanced.")
|
|
||||||
else:
|
|
||||||
print("Palette is already vibrant and diverse enough.")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user