whitphx HF staff commited on
Commit
d76edd7
β€’
1 Parent(s): 6c3e735

Refactoring

Browse files
Files changed (1) hide show
  1. app.py +30 -28
app.py CHANGED
@@ -51,27 +51,8 @@ if 'theme_from_initial_config' in st.session_state:
51
  default_color = preset_colors[0][1]
52
 
53
 
54
- if 'preset_color' not in st.session_state or 'backgroundColor' not in st.session_state or 'secondaryBackgroundColor' not in st.session_state or 'textColor' not in st.session_state:
55
- st.session_state['primaryColor'] = default_color.primaryColor
56
- st.session_state['backgroundColor'] = default_color.backgroundColor
57
- st.session_state['secondaryBackgroundColor'] = default_color.secondaryBackgroundColor
58
- st.session_state['textColor'] = default_color.textColor
59
-
60
-
61
- st.title("Streamlit color theme editor")
62
-
63
- def on_preset_color_selected():
64
- _, color = preset_colors[st.session_state.preset_color]
65
- st.session_state['primaryColor'] = color.primaryColor
66
- st.session_state['backgroundColor'] = color.backgroundColor
67
- st.session_state['secondaryBackgroundColor'] = color.secondaryBackgroundColor
68
- st.session_state['textColor'] = color.textColor
69
-
70
-
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
  def sync_rgb_to_hls(key: str):
 
75
  rgb = util.parse_hex(st.session_state[key])
76
  hls = colorsys.rgb_to_hls(rgb[0], rgb[1], rgb[2])
77
  st.session_state[f"{key}H"] = round(hls[0] * 360)
@@ -87,16 +68,37 @@ 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
  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:
 
51
  default_color = preset_colors[0][1]
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def sync_rgb_to_hls(key: str):
55
+ # HLS states are necessary for the HLS sliders.
56
  rgb = util.parse_hex(st.session_state[key])
57
  hls = colorsys.rgb_to_hls(rgb[0], rgb[1], rgb[2])
58
  st.session_state[f"{key}H"] = round(hls[0] * 360)
 
68
  st.session_state[key] = f"#{round(r * 255):02x}{round(g * 255):02x}{round(b * 255):02x}"
69
 
70
 
71
+ def set_color(key: str, color: str):
72
+ st.session_state[key] = color
73
+ sync_rgb_to_hls(key)
74
+
75
+
76
+ if 'preset_color' not in st.session_state or 'backgroundColor' not in st.session_state or 'secondaryBackgroundColor' not in st.session_state or 'textColor' not in st.session_state:
77
+ set_color('primaryColor', default_color.primaryColor)
78
+ set_color('backgroundColor', default_color.backgroundColor)
79
+ set_color('secondaryBackgroundColor', default_color.secondaryBackgroundColor)
80
+ set_color('textColor', default_color.textColor)
81
+
82
+
83
+ st.title("Streamlit color theme editor")
84
+
85
+
86
+ def on_preset_color_selected():
87
+ _, color = preset_colors[st.session_state.preset_color]
88
+ set_color('primaryColor', color.primaryColor)
89
+ set_color('backgroundColor', color.backgroundColor)
90
+ set_color('secondaryBackgroundColor', color.secondaryBackgroundColor)
91
+ set_color('textColor', color.textColor)
92
+
93
+
94
+ 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)
95
+
96
  if st.button("🎨 Generate a random color scheme 🎲"):
97
  primary_color, text_color, basic_background, secondary_background = util.generate_color_scheme()
98
+ set_color('primaryColor', primary_color)
99
+ set_color('backgroundColor', basic_background)
100
+ set_color('secondaryBackgroundColor', secondary_background)
101
+ set_color('textColor', text_color)
 
 
 
 
102
 
103
 
104
  def color_picker(label: str, key: str, default_color: str, l_only: bool) -> None: