Skip to content

Commit bf6c5d7

Browse files
committed
fix: add chroma scaling, so colors generated with monochrome, neutral and vibrant scheme actually works
1 parent 25c8fc2 commit bf6c5d7

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

scripts/harmonizer.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,23 @@ def luminance(h):
6262
# ── HCT harmonization ─────────────────────────────────────────────────
6363

6464

65-
def harmonize(from_hct, to_hct, tone_boost):
65+
def harmonize(from_hct, to_hct, tone_boost, chroma_scale=1.0):
6666
diff = difference_degrees(from_hct.hue, to_hct.hue)
6767
rot = min(diff * 0.8, 100)
6868
out_hue = sanitize_degrees_double(
6969
from_hct.hue + rot * rotation_direction(from_hct.hue, to_hct.hue)
7070
)
71-
return Hct.from_hct(out_hue, from_hct.chroma, from_hct.tone * (1 + tone_boost))
71+
out_chroma = from_hct.chroma * chroma_scale
72+
return Hct.from_hct(out_hue, out_chroma, from_hct.tone * (1 + tone_boost))
7273

7374

74-
def harmonize_palette(base, primary_hex, is_dark):
75+
def harmonize_palette(base, primary_hex, is_dark, source_hex=None):
7576
primary_hct = Hct.from_int(hex_to_argb(primary_hex))
77+
if source_hex:
78+
source_hct = Hct.from_int(hex_to_argb(source_hex))
79+
chroma_scale = min(primary_hct.chroma / max(source_hct.chroma, 1), 1.0)
80+
else:
81+
chroma_scale = 1.0
7682
colors = {}
7783
for name, hex_val in base.items():
7884
idx = int(name.replace("term", ""))
@@ -82,7 +88,7 @@ def harmonize_palette(base, primary_hex, is_dark):
8288
else:
8389
boost = 0.35 if idx < 8 else 0.20
8490
tone_boost = boost * (-1 if not is_dark else 1)
85-
h = harmonize(from_hct, primary_hct, tone_boost)
91+
h = harmonize(from_hct, primary_hct, tone_boost, chroma_scale)
8692
colors[name] = argb_to_hex(h.to_int())
8793
return colors
8894

@@ -115,8 +121,9 @@ def harmonize_from_matugen(
115121
mode = "dark" if is_dark else "light"
116122
base = palette[mode]
117123

118-
accent = mc.get("source_color") or mc.get("primary", "#888888")
124+
primary = mc.get("primary") or mc.get("source_color", "#888888")
125+
source = mc.get("source_color")
119126

120-
term = harmonize_palette(base, accent, is_dark)
127+
term = harmonize_palette(base, primary, is_dark, source)
121128

122-
return term, mc, mode, accent
129+
return term, mc, mode, primary

0 commit comments

Comments
 (0)