22Interactive Colour Picker - demonstrating event handling in PyScript.
33
44Shows:
5+
56- Multiple event types (input, click, change)
67- Custom events for decoupled logic
78- Working with form inputs
89- Dynamic UI updates
910"""
1011from pyscript import when , Event
11- from pyscript .web import page
12+ from pyscript .web import page , div
1213
1314
1415# Custom event for when colour changes.
15- colour_changed = Event ()
16-
16+ _colour_has_changed = Event ()
1717# Colour history (limited to 10).
18- history = []
18+ _HISTORY = []
1919
2020
2121def rgb_to_hex (r , g , b ):
2222 """
23- Convert RGB values to hex colour string.
23+ Convert RGB values to a hex colour string.
2424 """
2525 return f"#{ r :02X} { g :02X} { b :02X} "
2626
2727
2828def hex_to_rgb (hex_colour ):
2929 """
30- Convert hex colour string to RGB tuple.
30+ Convert a hex colour string to an RGB tuple.
3131 """
3232 hex_colour = hex_colour .lstrip ("#" )
3333 return tuple (int (hex_colour [i :i + 2 ], 16 ) for i in (0 , 2 , 4 ))
@@ -60,34 +60,28 @@ def update_controls(r, g, b):
6060 page ["#red-slider" ].value = r
6161 page ["#green-slider" ].value = g
6262 page ["#blue-slider" ].value = b
63-
6463 # Update number inputs.
6564 page ["#red-value" ].value = r
6665 page ["#green-value" ].value = g
6766 page ["#blue-value" ].value = b
68-
6967 # Update hex input.
7068 hex_colour = rgb_to_hex (r , g , b )
7169 page ["#hex-input" ].value = hex_colour
72-
7370 # Update display.
7471 update_display (hex_colour )
75-
7672 # Trigger custom event.
77- colour_changed .trigger (hex_colour )
73+ _colour_has_changed .trigger (hex_colour )
7874
7975
8076def add_to_history (hex_colour ):
8177 """
8278 Add colour to history, maintaining max of 10 items.
8379 """
84- if hex_colour in history :
80+ if hex_colour in _HISTORY :
8581 return
86-
87- history .insert (0 , hex_colour )
88- if len (history ) > 10 :
89- history .pop ()
90-
82+ _HISTORY .insert (0 , hex_colour )
83+ if len (_HISTORY ) > 10 :
84+ _HISTORY .pop ()
9185 # Update history display.
9286 render_history ()
9387
@@ -96,14 +90,11 @@ def render_history():
9690 """
9791 Render colour history.
9892 """
99- from pyscript .web import div
100-
10193 container = page ["#history-colours" ]
10294 container .clear ()
103-
104- for colour in history :
95+ for colour in _HISTORY :
10596 colour_div = div (Class = "history-colour" , title = colour )
106- colour_div .style . backgroundColor = colour
97+ colour_div .style [ " backgroundColor" ] = colour
10798 colour_div .dataset .colour = colour
10899 container .append (colour_div )
109100
@@ -129,12 +120,10 @@ def handle_number_change(event):
129120 r = int (page ["#red-value" ].value )
130121 g = int (page ["#green-value" ].value )
131122 b = int (page ["#blue-value" ].value )
132-
133123 # Clamp values.
134124 r = max (0 , min (255 , r ))
135125 g = max (0 , min (255 , g ))
136126 b = max (0 , min (255 , b ))
137-
138127 update_controls (r , g , b )
139128
140129
@@ -144,11 +133,9 @@ def handle_hex_change(event):
144133 Handle hex input changes.
145134 """
146135 hex_colour = event .target .value .strip ()
147-
148136 # Validate hex colour.
149137 if not hex_colour .startswith ("#" ):
150138 hex_colour = "#" + hex_colour
151-
152139 try :
153140 r , g , b = hex_to_rgb (hex_colour )
154141 update_controls (r , g , b )
@@ -177,7 +164,7 @@ def handle_history_click(event):
177164 update_controls (r , g , b )
178165
179166
180- @when (colour_changed )
167+ @when (_colour_has_changed )
181168def handle_colour_changed (hex_colour ):
182169 """
183170 Handle custom colour changed event.
0 commit comments