Skip to content

Commit 9643689

Browse files
committed
Allow to use bracket operator
1 parent 31c9f4f commit 9643689

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/modulino/pixels.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ def clear_all(self) -> 'ModulinoPixels':
249249
"""
250250
self.data = bytearray([0xE0] * NUM_LEDS * 4)
251251
return self
252+
253+
def __setitem__(self, idx: int, color: tuple | ModulinoColor) -> None:
254+
"""
255+
Sets the color of the given LED index to the given color.
256+
This allows to use the object like an array, e.g. pixels[0] = (255, 0, 0, 50)
257+
258+
Parameters:
259+
idx (int): The index of the LED (0..7).
260+
color (tuple | ModulinoColor): A tuple of three/four integers representing the RGB values (0-255) plus optional brightness (0-100).
261+
Alternatively, a ModulinoColor object can be provided.
262+
If None, the LED will be turned off.
263+
"""
264+
if color is None:
265+
self.clear(idx)
266+
return
267+
268+
if isinstance(color, ModulinoColor):
269+
self.set_color(idx, color)
270+
return
271+
272+
if not isinstance(color, tuple) or len(color) < 3:
273+
raise ValueError("Color must be a tuple of three or four integers representing the RGBA values.")
274+
brightness = 100 if len(color) == 3 else color[3]
275+
self.set_rgb(idx, color[0], color[1], color[2], brightness)
252276

253277
def show(self) -> None:
254278
"""

0 commit comments

Comments
 (0)