From 81766119f882cdd0ce253fbc5d53ec619560131f Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Sat, 20 Jul 2019 11:04:38 -0400 Subject: [PATCH] Add getting a palette value to support bitmap saving. --- shared-bindings/displayio/Palette.c | 17 ++++++++++++----- shared-bindings/displayio/Palette.h | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index 11c2f677ce3bf..7c98b37571eee 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -70,7 +70,7 @@ STATIC mp_obj_t displayio_palette_make_new(const mp_obj_type_t *type, size_t n_a //| //| Sets the pixel color at the given index. The index should be an integer in the range 0 to color_count-1. //| -//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value). +//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value). //| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), or bytearray. //| //| This allows you to:: @@ -89,13 +89,20 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) { return MP_OBJ_NULL; } - // index read is not supported - if (value == MP_OBJ_SENTINEL) { - return MP_OBJ_NULL; - } + displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); size_t index = mp_get_index(&displayio_palette_type, self->color_count, index_in, false); + + if (value == MP_OBJ_SENTINEL) { + uint16_t color; + if (displayio_palette_get_color(self, index, &color)) { + return MP_OBJ_NEW_SMALL_INT(color); + } else { + return MP_OBJ_NULL; + } + } + uint32_t color; mp_int_t int_value; mp_buffer_info_t bufinfo; diff --git a/shared-bindings/displayio/Palette.h b/shared-bindings/displayio/Palette.h index 767fc7b636911..95ab4f3bfa55c 100644 --- a/shared-bindings/displayio/Palette.h +++ b/shared-bindings/displayio/Palette.h @@ -37,4 +37,6 @@ void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t palette_index); void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index); +bool displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index, uint16_t* color); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_PALETTE_H