@@ -86,14 +86,16 @@ STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
86
86
//| Sets the pixel color at the given index. The index should be an integer in the range 0 to color_count-1.
87<
8000
/code>
87
//|
88
88
//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value).
89
- //| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), or bytearray.
89
+ //| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), bytearray,
90
+ //| or a tuple or list of 3 integers.
90
91
//|
91
92
//| This allows you to::
92
93
//|
93
94
//| palette[0] = 0xFFFFFF # set using an integer
94
95
//| palette[1] = b'\xff\xff\x00' # set using 3 bytes
95
96
//| palette[2] = b'\xff\xff\x00\x00' # set using 4 bytes
96
97
//| palette[3] = bytearray(b'\x00\x00\xFF') # set using a bytearay of 3 or 4 bytes
98
+ //| palette[4] = (10, 20, 30) # set using a tuple of 3 integers
97
99
//|
98
100
STATIC mp_obj_t palette_subscr (mp_obj_t self_in , mp_obj_t index_in , mp_obj_t value ) {
99
101
if (value == MP_OBJ_NULL ) {
@@ -111,6 +113,12 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
111
113
return MP_OBJ_NEW_SMALL_INT (common_hal_displayio_palette_get_color (self , index ));
112
114
}
113
115
116
+ // Convert a tuple or list to a bytearray.
117
+ if (MP_OBJ_IS_TYPE (value , & mp_type_tuple ) ||
118
+ MP_OBJ_IS_TYPE (value , & mp_type_list )) {
119
+ value = mp_type_bytes .make_new (& mp_type_bytes , 1 , & value , NULL );
120
+ }
121
+
114
122
uint32_t color ;
115
123
mp_int_t int_value ;
116
124
mp_buffer_info_t bufinfo ;
@@ -130,7 +138,7 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
130
138
}
131
139
color = int_value ;
132
140
} else {
133
- mp_raise_TypeError (translate ("color buffer must be a buffer or int" ));
141
+ mp_raise_TypeError (translate ("color buffer must be a buffer, tuple, list, or int" ));
134
142
}
135
143
common_hal_displayio_palette_set_color (self , index , color );
136
144
return mp_const_none ;
0 commit comments