← back to Greenland Onboard

scripts/compute-hue-style.py

50 lines

#!/usr/bin/env python3
# Add HUE (from hex HSL) + STYLE (interior-designer lexicon per material) to every SKU.
import json, colorsys, subprocess
DB="postgresql:///dw_unified?host=/tmp&user=stevestudio2"

def hue_name(hexv):
    try:
        h=hexv.lstrip('#'); r,g,b=[int(h[i:i+2],16)/255 for i in (0,2,4)]
    except Exception: return "Neutral"
    hh,l,s=colorsys.rgb_to_hls(r,g,b); deg=hh*360
    if s<0.12:
        return "Black" if l<0.20 else "White" if l>0.85 else "Greige" if l>0.5 else "Charcoal"
    if deg<15 or deg>=345: return "Red"
    if deg<45: return "Orange"
    if deg<70: return "Yellow"
    if deg<95: return "Chartreuse"
    if deg<160: return "Green"
    if deg<200: return "Teal"
    if deg<255: return "Blue"
    if deg<290: return "Purple"
    return "Magenta"

STYLE={  # material -> interior-design style (coastal-leaning luxury)
 "Silk":"Glam","Velvet":"Glam","Suede":"Transitional","Wool":"Transitional",
 "Wood":"Organic Modern","Cork":"Organic Modern","PE Weaving / PE Woven Material":"Organic Modern",
 "Sisal":"Coastal","Abaca":"Coastal","Hemp":"Coastal","Jute":"Coastal","Raffia":"Coastal",
 "Arrowroot":"Coastal","Paper Weave":"Coastal","Cotton Yarn":"Coastal","Water hyacinth":"Coastal",
 "Grass":"Coastal","Linen":"Coastal","Jacquard":"Traditional","Metal Leaf":"Art Deco",
 "Multi-material":"Eclectic","Specialty":"Contemporary",
}

d=json.load(open('data/enriched.json'))
for r in d:
    r['hue']=hue_name(r.get('hex',''))
    r['style']=STYLE.get(r.get('material',''),"Coastal")
json.dump(d,open('data/enriched.json','w'))
print("enriched.json updated with hue+style")

# add DB columns + backfill
subprocess.run(["psql",DB,"-tAc","alter table greenland_full_catalog add column if not exists hue text; alter table greenland_full_catalog add column if not exists style text"],check=True)
# write a tiny SQL values batch
vals=",".join("('%s','%s','%s')"%(r['dwSku'],r['hue'].replace("'","''"),r['style'].replace("'","''")) for r in d)
sql="update greenland_full_catalog g set hue=v.hue, style=v.style from (values %s) as v(dw_sku,hue,style) where g.dw_sku=v.dw_sku"%vals
subprocess.run(["psql",DB,"-tAc",sql],check=True)
# summary
import collections
hc=collections.Counter(r['hue'] for r in d); sc=collections.Counter(r['style'] for r in d)
print("HUES :",dict(hc.most_common()))
print("STYLES:",dict(sc.most_common()))