commit d1b9de736fa553f03a6081fe6aabb4561864cde5 Author: Jimmy Date: Fri Aug 30 17:23:16 2024 +1200 Initial commit diff --git a/color_utils.py b/color_utils.py new file mode 100644 index 0000000..831e6d1 --- /dev/null +++ b/color_utils.py @@ -0,0 +1,23 @@ +def adjust_brightness(color, brightness): + """Adjust brightness of an RGB color.""" + r, g, b = color + return (int(r * brightness/255), int(g * brightness/255), int(b * brightness/255)) + +def rgb_to_hex(color): + """Convert an RGB color to hex format.""" + return '#{:02x}{:02x}{:02x}'.format(color[0], color[1], color[2]) + + +def generate_color_transition(start_color, end_color, steps): + """Generate a list of colors transitioning from start_color to end_color.""" + r1, g1, b1 = start_color + r2, g2, b2 = end_color + + transition_colors = [] + for i in range(steps): + r = r1 + (r2 - r1) * i // (steps - 1) + g = g1 + (g2 - g1) * i // (steps - 1) + b = b1 + (b2 - b1) * i // (steps - 1) + transition_colors.append((r, g, b)) + + return transition_colors diff --git a/main.py b/main.py new file mode 100644 index 0000000..79e37a5 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +import asyncio +import tkinter as tk +from tkinter import ttk + +from async_tkinter_loop import async_handler, async_mainloop +from networking import send_to_server +import color_utils + +class App: + def __init__(self) -> None: + self.root = tk.Tk() + self.root.attributes('-fullscreen', True) + + # Red Slider + self.red_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Red") + self.red_slider.set(0) + self.red_slider.pack(side=tk.LEFT, padx=10) + self.red_slider.bind("", self.update_colour) + + self.green_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Green") + self.green_slider.set(0) + self.green_slider.pack(side=tk.LEFT, padx=10) + self.green_slider.bind("", self.update_colour) + + + self.blue_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Blue") + self.blue_slider.set(0) + self.blue_slider.pack(side=tk.LEFT, padx=10) + self.blue_slider.bind("", self.update_colour) + + + self.brightness_slider = tk.Scale(self.root, from_=255, to=0, orient=tk.VERTICAL, length=500, width=50, label="Brighness") + self.brightness_slider.set(127) + self.brightness_slider.pack(side=tk.LEFT, padx=10) + self.brightness_slider.bind("", self.update_colour) + + async_mainloop(self.root) + + @async_handler + async def update_colour(self, event): + print(event) + r = self.red_slider.get() + g = self.green_slider.get() + b = self.blue_slider.get() + brightness = self.brightness_slider.get() + color = color_utils.adjust_brightness((r,b,g), brightness) + print(color, brightness) + + await send_to_server(bytes(color), ("192.168.0.206","80")) + + + +if __name__ == "__main__": + app = App() \ No newline at end of file diff --git a/networking.py b/networking.py new file mode 100644 index 0000000..4a6065a --- /dev/null +++ b/networking.py @@ -0,0 +1,20 @@ +import socket +import asyncio + +async def send_color_data(color_data, servers): + """Send color data to all configured servers.""" + tasks = [send_to_server(color_data, server) for server in servers] + await asyncio.gather(*tasks) + +async def send_to_server(color_data, server): + """Connect to a server, send color data, and close the connection.""" + server_ip, server_port = server + try: + reader, writer = await asyncio.open_connection(server_ip, server_port) + writer.write(color_data) + await writer.drain() + writer.close() + await writer.wait_closed() + print(f"Sent data to {server_ip}:{server_port}") + except (socket.error, ConnectionError) as e: + print(f"Error sending to {server_ip}:{server_port}: {e}")