Spaces:
Runtime error
Runtime error
Add HLS color sliders
Browse files- app.py +7 -5
- fragments.py +30 -0
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
-
import
|
2 |
from typing import NamedTuple
|
3 |
|
4 |
import streamlit as st
|
5 |
|
6 |
import fragments
|
7 |
|
|
|
|
|
8 |
class ThemeColor(NamedTuple):
|
9 |
primaryColor: str
|
10 |
backgroundColor: str
|
@@ -69,10 +71,10 @@ def on_preset_color_selected():
|
|
69 |
st.selectbox("Preset colors", key="preset_color", options=range(len(preset_colors)), format_func=lambda idx: preset_colors[idx][0], on_change=on_preset_color_selected)
|
70 |
|
71 |
|
72 |
-
primary_color =
|
73 |
-
text_color =
|
74 |
-
background_color =
|
75 |
-
secondary_background_color =
|
76 |
|
77 |
|
78 |
def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
|
|
|
1 |
+
import colorsys
|
2 |
from typing import NamedTuple
|
3 |
|
4 |
import streamlit as st
|
5 |
|
6 |
import fragments
|
7 |
|
8 |
+
import util
|
9 |
+
|
10 |
class ThemeColor(NamedTuple):
|
11 |
primaryColor: str
|
12 |
backgroundColor: str
|
|
|
71 |
st.selectbox("Preset colors", key="preset_color", options=range(len(preset_colors)), format_func=lambda idx: preset_colors[idx][0], on_change=on_preset_color_selected)
|
72 |
|
73 |
|
74 |
+
primary_color = fragments.color_picker('Primary color', key="primaryColor", default_color=default_color.primaryColor)
|
75 |
+
text_color = fragments.color_picker('Text color', key="textColor", default_color=default_color.textColor)
|
76 |
+
background_color = fragments.color_picker('Background color', key="backgroundColor", default_color=default_color.backgroundColor)
|
77 |
+
secondary_background_color = fragments.color_picker('Secondary background color', key="secondaryBackgroundColor", default_color=default_color.secondaryBackgroundColor)
|
78 |
|
79 |
|
80 |
def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
|
fragments.py
CHANGED
@@ -1,9 +1,39 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import wcag_contrast_ratio as contrast
|
3 |
|
4 |
import util
|
5 |
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def contrast_summary(label: str, foreground_rgb_hex: str, background_rgb_hex: str) -> None:
|
8 |
rgb_foreground = util.parse_hex(foreground_rgb_hex)
|
9 |
rgb_background = util.parse_hex(background_rgb_hex)
|
|
|
1 |
+
import colorsys
|
2 |
+
|
3 |
import streamlit as st
|
4 |
import wcag_contrast_ratio as contrast
|
5 |
|
6 |
import util
|
7 |
|
8 |
|
9 |
+
def color_picker(label: str, key: str, default_color: str) -> None:
|
10 |
+
def on_color_change():
|
11 |
+
rgb = util.parse_hex(st.session_state[key])
|
12 |
+
hls = colorsys.rgb_to_hls(rgb[0], rgb[1], rgb[2])
|
13 |
+
st.session_state[f"{key}H"] = round(hls[0] * 360)
|
14 |
+
st.session_state[f"{key}L"] = round(hls[1] * 100)
|
15 |
+
st.session_state[f"{key}S"] = round(hls[2] * 100)
|
16 |
+
|
17 |
+
def on_hls_change():
|
18 |
+
h = st.session_state[f"{key}H"]
|
19 |
+
l = st.session_state[f"{key}L"]
|
20 |
+
s = st.session_state[f"{key}S"]
|
21 |
+
r, g, b = colorsys.hls_to_rgb(h / 360, l / 100, s / 100)
|
22 |
+
st.session_state[key] = f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}"
|
23 |
+
|
24 |
+
col1, col2 = st.columns([1, 3])
|
25 |
+
with col1:
|
26 |
+
color = st.color_picker(label, key=key, on_change=on_color_change)
|
27 |
+
with col2:
|
28 |
+
r,g,b = util.parse_hex(default_color)
|
29 |
+
h,l,s = colorsys.rgb_to_hls(r,g,b)
|
30 |
+
st.slider(f"H for {label}", key=f"{key}H", min_value=0, max_value=360, value=round(h * 360), format="%d°", label_visibility="collapsed", on_change=on_hls_change)
|
31 |
+
st.slider(f"L for {label}", key=f"{key}L", min_value=0, max_value=100, value=round(l * 100), format="%d%%", label_visibility="collapsed", on_change=on_hls_change)
|
32 |
+
st.slider(f"S for {label}", key=f"{key}S", min_value=0, max_value=100, value=round(s * 100), format="%d%%", label_visibility="collapsed", on_change=on_hls_change)
|
33 |
+
|
34 |
+
return color
|
35 |
+
|
36 |
+
|
37 |
def contrast_summary(label: str, foreground_rgb_hex: str, background_rgb_hex: str) -> None:
|
38 |
rgb_foreground = util.parse_hex(foreground_rgb_hex)
|
39 |
rgb_background = util.parse_hex(background_rgb_hex)
|