Initial commit

This commit is contained in:
Jimmy 2024-08-30 17:23:16 +12:00
commit d1b9de736f
3 changed files with 97 additions and 0 deletions

23
color_utils.py Normal file
View File

@ -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

54
main.py Normal file
View File

@ -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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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("<ButtonRelease-1>", 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()

20
networking.py Normal file
View File

@ -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}")