From 8b3e0d4cab09f91aceee64a6bd133301de23dba2 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 15 Nov 2023 18:24:11 +1100 Subject: [PATCH] feat: Add more colour control --- main.py | 11 ++++++++++- sites/slds.py | 4 ++-- sites/website.py | 25 ++++++++++++------------- templates/city.html | 1 + templates/edit.html | 4 +++- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 2450206..63b2010 100644 --- a/main.py +++ b/main.py @@ -104,6 +104,8 @@ def edit(): location = "" avatar = "" bg_colour = "" + fg_colour = "" + text_colour = "" if 'data' in data: html = data['data'].encode('utf-8').decode('unicode-escape') @@ -121,10 +123,15 @@ def edit(): avatar = data['avatar'] if 'bg_colour' in data: bg_colour = data['bg_colour'] + if 'fg_colour' in data: + fg_colour = data['fg_colour'] + if 'text_colour' in data: + text_colour = data['text_colour'] return render_template('edit.html',account=user['email'],account_link="account",account_link_name="Account",data=html, hns=hns,btc=btc,eth=eth,hnschat=hnschat,location=location,avatar=avatar, - bg_colour=bg_colour,CITY_DOMAIN=CITY_DOMAIN,domain=user['domain']) + bg_colour=bg_colour,fg_colour=fg_colour,text_colour=text_colour, + CITY_DOMAIN=CITY_DOMAIN,domain=user['domain']) @app.route('/edit', methods=['POST']) @@ -150,6 +157,8 @@ def send_edit(): data['location'] = request.form['location'] data['avatar'] = request.form['avatar'] data['bg_colour'] = request.form['bg_colour'] + data['fg_colour'] = request.form['fg_colour'] + data['text_colour'] = request.form['text_colour'] # Convert to json data = json.dumps(data) diff --git a/sites/slds.py b/sites/slds.py index a56035b..291b3bd 100644 --- a/sites/slds.py +++ b/sites/slds.py @@ -59,9 +59,9 @@ def clean_template(): with open('templates/city.html') as f: data = f.read() - data = data.replace('#f1ffff', '{{bg_colour}}') + data = data.replace('#f1ffff', '{{fg_colour}}') data = data.replace('#1fffff', '{{text_colour}}') - data = data.replace('#000000', '{{text_colour}}') + data = data.replace('#000000', '{{bg_colour}}') # Save with open('templates/city.html', 'w') as f: f.write(data) diff --git a/sites/website.py b/sites/website.py index cf480e4..b5fab0a 100644 --- a/sites/website.py +++ b/sites/website.py @@ -23,14 +23,10 @@ def render(data,db_object): return "

Invalid HTML


" + str(e) bg_colour = db_object['bg_colour'] - hex_colour = bg_colour.lstrip('#') - text_colour = generate_foreground_color(tuple(int(hex_colour[i:i+2], 16) for i in (0, 2, 4))) - text_colour = rgb_to_hex(text_colour) + fg_colour = db_object['fg_colour'] + text_colour = db_object['text_colour'] - if (text_colour == "#000000"): - hns_icon = "assets/img/HNS.png" - else: - hns_icon = "assets/img/HNSW.png" + try: avatar = db_object['avatar'] @@ -39,13 +35,17 @@ def render(data,db_object): hns = db_object['HNS'] btc = db_object['BTC'] eth = db_object['ETH'] + if (rgb_to_hex(generate_foreground_color(fg_colour)) == ""): + hns_icon = "assets/img/HNSW.png" + else: + hns_icon = "assets/img/HNS.png" except Exception as e: return "

Invalid data


Please contact support


" + str(e) return render_template('city.html',html=html,bg_colour=bg_colour,text_colour=text_colour, - avatar=avatar,main_domain=main_domain, + fg_colour=fg_colour, avatar=avatar,main_domain=main_domain, hnschat=hnschat,location=location, hns_icon=hns_icon, hns=hns,btc=btc,eth=eth, data=html) @@ -75,15 +75,14 @@ def calculate_contrast_ratio(color1, color2): return contrast_ratio def generate_foreground_color(background_color): - # A color with a high contrast ratio against the background color - contrast_color = (255, 255, 255) # White + # Convert to RGB tuple + background_color=background_color.lstrip('#') + background_color = tuple(int(background_color[i:i+2], 16) for i in (0, 2, 4)) - # Calculate the contrast ratio + contrast_color = (255, 255, 255) # White ratio = calculate_contrast_ratio(background_color, contrast_color) - # Adjust the contrast color based on the contrast ratio if ratio < 4.5: - # If the contrast ratio is below the threshold, use black as the foreground color return (0, 0, 0) # Black else: return contrast_color diff --git a/templates/city.html b/templates/city.html index 8ffa78f..1c0be14 100644 --- a/templates/city.html +++ b/templates/city.html @@ -52,6 +52,7 @@

Powered by ShakeCities

+
diff --git a/templates/edit.html b/templates/edit.html index be19111..0555074 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -34,7 +34,9 @@
-
+
+
+