8000 allow tuple or list for Palette color · pdp7/circuitpython@a4ebd2f · GitHub
[go: up one dir, main page]

Skip to content

Commit a4ebd2f

Browse files
committed
allow tuple or list for Palette color
1 parent 67440ac commit a4ebd2f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ports/atmel-samd/boards/pewpew_m4/mpconfigboard.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ INTERNAL_FLASH_FILESYSTEM = 1
1010
LONGINT_IMPL = NONE
1111

1212
CIRCUITPY_SMALL_BUILD = 1
13+
# TODO: Turn off analogio for now for space reasons, but restore it
14+
# when frozen module gets smaller.
15+
CIRCUITPY_ANALOGIO = 0
1316
CIRCUITPY_AUDIOBUSIO = 0
1417
CIRCUITPY_BITBANGIO = 0
1518
CIRCUITPY_FREQUENCYIO = 0

shared-bindings/displayio/Palette.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
8686
//| 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
//|
8888
//| 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.
9091
//|
9192
//| This allows you to::
9293
//|
9394
//| palette[0] = 0xFFFFFF # set using an integer
9495
//| palette[1] = b'\xff\xff\x00' # set using 3 bytes
9596
//| palette[2] = b'\xff\xff\x00\x00' # set using 4 bytes
9697
//| 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
9799
//|
98100
STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
99101
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
111113
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_color(self, index));
112114
}
113115

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+
114122
uint32_t color;
115123
mp_int_t int_value;
116124
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
130138
}
131139
color = int_value;
132140
} 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"));
134142
}
135143
common_hal_displayio_palette_set_color(self, index, color);
136144
return mp_const_none;

0 commit comments

Comments
 (0)
0