Skip to content

Add getter for displayio.Palette item transparency/opacity. #4426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions shared-bindings/displayio/Palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,25 @@ STATIC mp_obj_t displayio_palette_obj_make_opaque(mp_obj_t self_in, mp_obj_t pal
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_make_opaque_obj, displayio_palette_obj_make_opaque);

//| def is_transparent(self, palette_index: int) -> bool:
//| """Returns `True` if the palette index is transparent. Returns `False` if opaque."""
//| ...
//|
STATIC mp_obj_t displayio_palette_obj_is_transparent(mp_obj_t self_in, mp_obj_t palette_index_obj) {
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);

mp_int_t palette_index;
if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) {
mp_raise_ValueError(translate("palette_index should be an int"));
}
return mp_obj_new_bool(common_hal_displayio_palette_is_transparent(self, palette_index));
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_is_transparent_obj, displayio_palette_obj_is_transparent);

STATIC const mp_rom_map_elem_t displayio_palette_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_make_transparent), MP_ROM_PTR(&displayio_palette_make_transparent_obj) },
{ MP_ROM_QSTR(MP_QSTR_make_opaque), MP_ROM_PTR(&displayio_palette_make_opaque_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_transparent), MP_ROM_PTR(&displayio_palette_is_transparent_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_palette_locals_dict, displayio_palette_locals_dict_table);

Expand Down
1 change: 1 addition & 0 deletions shared-bindings/displayio/Palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ uint32_t common_hal_displayio_palette_get_len(displayio_palette_t *self);

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 common_hal_displayio_palette_is_transparent(displayio_palette_t *self, uint32_t palette_index);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_PALETTE_H
4 changes: 4 additions & 0 deletions shared-module/displayio/Palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ void common_hal_displayio_palette_make_transparent(displayio_palette_t *self, ui
self->needs_refresh = true;
}

bool common_hal_displayio_palette_is_transparent(displayio_palette_t *self, uint32_t palette_index) {
return self->colors[palette_index].transparent;
}

uint32_t common_hal_displayio_palette_get_len(displayio_palette_t *self) {
return self->color_count;
}
Expand Down