Spaces:
Runtime error
Runtime error
Create a random color generator
Browse files
app.py
CHANGED
|
@@ -87,6 +87,18 @@ def sync_hls_to_rgb(key: str):
|
|
| 87 |
st.session_state[key] = f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}"
|
| 88 |
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def color_picker(label: str, key: str, default_color: str, l_only: bool) -> None:
|
| 91 |
col1, col2 = st.columns([1, 3])
|
| 92 |
with col1:
|
|
|
|
| 87 |
st.session_state[key] = f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}"
|
| 88 |
|
| 89 |
|
| 90 |
+
if st.button("🎨 Generate a random color scheme 🎲"):
|
| 91 |
+
primary_color, text_color, basic_background, secondary_background = util.generate_color_scheme()
|
| 92 |
+
st.session_state['primaryColor'] = primary_color
|
| 93 |
+
st.session_state['backgroundColor'] = basic_background
|
| 94 |
+
st.session_state['secondaryBackgroundColor'] = secondary_background
|
| 95 |
+
st.session_state['textColor'] = text_color
|
| 96 |
+
sync_rgb_to_hls('primaryColor')
|
| 97 |
+
sync_rgb_to_hls('backgroundColor')
|
| 98 |
+
sync_rgb_to_hls('secondaryBackgroundColor')
|
| 99 |
+
sync_rgb_to_hls('textColor')
|
| 100 |
+
|
| 101 |
+
|
| 102 |
def color_picker(label: str, key: str, default_color: str, l_only: bool) -> None:
|
| 103 |
col1, col2 = st.columns([1, 3])
|
| 104 |
with col1:
|
util.py
CHANGED
|
@@ -1,7 +1,58 @@
|
|
| 1 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
|
| 5 |
if not re.match(r"^#[0-9a-fA-F]{6}$", rgb_hex_str):
|
| 6 |
raise ValueError("Invalid hex color")
|
| 7 |
return tuple(int(rgb_hex_str[i:i+2], 16) / 255 for i in (1, 3, 5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
import random
|
| 3 |
+
from colorsys import hls_to_rgb
|
| 4 |
+
|
| 5 |
+
import wcag_contrast_ratio as contrast
|
| 6 |
|
| 7 |
|
| 8 |
def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
|
| 9 |
if not re.match(r"^#[0-9a-fA-F]{6}$", rgb_hex_str):
|
| 10 |
raise ValueError("Invalid hex color")
|
| 11 |
return tuple(int(rgb_hex_str[i:i+2], 16) / 255 for i in (1, 3, 5))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def random_hls():
|
| 15 |
+
h = random.random()
|
| 16 |
+
l = random.random()
|
| 17 |
+
s = random.random()
|
| 18 |
+
|
| 19 |
+
# Make sure the color is light or dark enough.
|
| 20 |
+
MAX_LIGHTNESS = 0.3
|
| 21 |
+
if l < 0.5:
|
| 22 |
+
# Make the color darker.
|
| 23 |
+
l = l * (MAX_LIGHTNESS / 0.5)
|
| 24 |
+
else:
|
| 25 |
+
# Make the color lighter.
|
| 26 |
+
l = 1 - l
|
| 27 |
+
l = l * (MAX_LIGHTNESS / 0.5)
|
| 28 |
+
l = 1 - l
|
| 29 |
+
return (h, l, s)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def high_contrast_color(color):
|
| 33 |
+
h, l, s = color
|
| 34 |
+
l = 1 - l
|
| 35 |
+
return (h, l, s)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def hls_to_hex(color):
|
| 39 |
+
r, g, b = hls_to_rgb(*color)
|
| 40 |
+
return "#{:02x}{:02x}{:02x}".format(round(r * 255), round(g * 255), round(b * 255))
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def find_color_with_contrast(base_color, min_contrast_ratio, max_attempts):
|
| 44 |
+
for _ in range(max_attempts):
|
| 45 |
+
candidate_color = random_hls()
|
| 46 |
+
if contrast.rgb(hls_to_rgb(*candidate_color), hls_to_rgb(*base_color)) > min_contrast_ratio:
|
| 47 |
+
return candidate_color
|
| 48 |
+
return high_contrast_color(base_color)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def generate_color_scheme():
|
| 52 |
+
primary_color = random_hls()
|
| 53 |
+
basic_background = high_contrast_color(primary_color)
|
| 54 |
+
|
| 55 |
+
text_color = find_color_with_contrast(basic_background, 7, 100)
|
| 56 |
+
secondary_background = find_color_with_contrast(primary_color, 7, 100)
|
| 57 |
+
|
| 58 |
+
return hls_to_hex(primary_color), hls_to_hex(text_color), hls_to_hex(basic_background), hls_to_hex(secondary_background)
|