led_control/controller/gui.py
2023-06-15 12:29:53 +02:00

76 lines
2.0 KiB
Python

def main():
import streamlit as st
import controller
import colorsys
import pickle
import os
from PIL import Image
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
def hex_to_rgb(hex):
r = int(hex[1:3],16)
g = int(hex[3:5],16)
b = int(hex[5:7],16)
return r,g,b
def rgb_to_hex(r,g,b):
return "#" + hex(r)[2:].zfill(2) + hex(g)[2:].zfill(2) + hex(b)[2:].zfill(2)
def load_config():
try:
path = os.path.expanduser("~/.config/lc/lc.dmp")
with open(path, 'rb') as f:
return pickle.load(f)
except FileNotFoundError:
return {'color': '000000', 'saved': []}
def save(conf):
path = os.path.expanduser("~/.config/lc/lc.dmp")
with open(path, 'wb') as f:
pickle.dump(conf, f)
def add_to_conf():
st.session_state.config['saved'].append(color)
if 'config' not in st.session_state:
init = 1
st.session_state.config = load_config()
color = st.session_state.config['color']
st.title("LED Control")
# r = st.slider("Red", 0, 255, 0)
# g = st.slider("Green", 0, 255, 0)
# b = st.slider("Blue", 0, 255, 0)
h = st.slider("Hue", 0.0, 1.0, 0.0, 0.01)
s = st.slider("Saturation", 0.0, 1.0, 0.0, 0.01)
v = st.slider("Value", 0.0, 1.0, 0.0, 0.01)
r,g,b = colorsys.hsv_to_rgb(h,s,v)
color = rgb_to_hex(int(r * 255),int(g * 255),int(b * 255))
st.session_state.config['color'] = color[1:]
save(st.session_state.config)
#controller.set_pixels(color[1:])
img = Image.new(mode="RGB", size=(30,30), color=(int(r * 255),int(g * 255),int(b * 255)))
st.subheader(color)
st.image(img)
st.button("Save Color", add_to_conf)
st.session_state.config['saved']
if __name__ == "__main__":
main()