Skip to content

Commit 26bd5af

Browse files
committed
bitmaptools.replace_color() function
1 parent 11727bd commit 26bd5af

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

shared-bindings/bitmaptools/__init__.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,46 @@ static mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
418418
}
419419
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_alphablend_obj, 0, bitmaptools_alphablend);
420420

421+
//| def replace_color(
422+
//| dest_bitmap: displayio.Bitmap, old_color: int, new_color: int
423+
//| ) -> None:
424+
//| """Replace any pixels of old_color with new_color in the dest_bitmap
425+
//|
426+
//| :param bitmap dest_bitmap: Destination bitmap that will be written into
427+
//| :param int old_color: Bitmap palette index that will overwritten
428+
//| :param int new_color: Bitmap palette index that will get put in the bitmap"""
429+
//| ...
430+
//|
431+
//|
432+
static mp_obj_t bitmaptools_obj_replace_color(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
433+
enum {ARG_dest_bitmap, ARG_old_color, ARG_new_color};
434+
435+
static const mp_arg_t allowed_args[] = {
436+
{MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
437+
{MP_QSTR_old_color, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
438+
{MP_QSTR_new_color, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}},
439+
};
440+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
441+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
442+
443+
displayio_bitmap_t *destination = MP_OBJ_TO_PTR(args[ARG_dest_bitmap].u_obj); // the destination bitmap
444+
445+
uint32_t old_color, new_color, color_depth;
446+
old_color = args[ARG_old_color].u_int;
447+
new_color = args[ARG_new_color].u_int;
448+
449+
color_depth = (1 << destination->bits_per_value);
450+
if (color_depth <= old_color || color_depth <= new_color) {
451+
mp_raise_ValueError(MP_ERROR_TEXT("out of range of target"));
452+
}
453+
454+
common_hal_bitmaptools_replace_color(destination, old_color, new_color);
455+
456+
return mp_const_none;
457+
}
458+
459+
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_replace_color_obj, 0, bitmaptools_obj_replace_color);
460+
421461
//| def fill_region(
422462
//| dest_bitmap: displayio.Bitmap, x1: int, y1: int, x2: int, y2: int, value: int
423463
//| ) -> None:
@@ -1103,6 +1143,7 @@ static const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
11031143
{ MP_ROM_QSTR(MP_QSTR_rotozoom), MP_ROM_PTR(&bitmaptools_rotozoom_obj) },
11041144
{ MP_ROM_QSTR(MP_QSTR_arrayblit), MP_ROM_PTR(&bitmaptools_arrayblit_obj) },
11051145
{ MP_ROM_QSTR(MP_QSTR_alphablend), MP_ROM_PTR(&bitmaptools_alphablend_obj) },
1146+
{ MP_ROM_QSTR(MP_QSTR_replace_color), MP_ROM_PTR(&bitmaptools_replace_color_obj) },
11061147
{ MP_ROM_QSTR(MP_QSTR_fill_region), MP_ROM_PTR(&bitmaptools_fill_region_obj) },
11071148
{ MP_ROM_QSTR(MP_QSTR_boundary_fill), MP_ROM_PTR(&bitmaptools_boundary_fill_obj) },
11081149
{ MP_ROM_QSTR(MP_QSTR_draw_line), MP_ROM_PTR(&bitmaptools_draw_line_obj) },

shared-bindings/bitmaptools/__init__.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
4242
int16_t x2, int16_t y2,
4343
uint32_t value);
4444

45+
void common_hal_bitmaptools_replace_color(displayio_bitmap_t *destination,
46+
uint32_t old_color, uint32_t new_color);
47+
4548
void common_hal_bitmaptools_boundary_fill(displayio_bitmap_t *destination,
4649
int16_t x, int16_t y,
4750
uint32_t fill_color_value, uint32_t replaced_color_value);

shared-module/bitmaptools/__init__.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16
204204
}
205205
}
206206

207+
void common_hal_bitmaptools_replace_color(displayio_bitmap_t *destination,
208+
uint32_t old_color,
209+
uint32_t new_color){
210+
211+
int16_t x, y;
212+
for (x = 0; x < destination->width; x++) {
213+
for (y = 0; y < destination->height; y++) {
214+
uint32_t pixel_val = common_hal_displayio_bitmap_get_pixel(destination, x, y);
215+
if (pixel_val == old_color) {
216+
displayio_bitmap_write_pixel(destination, x, y, new_color);
217+
}
218+
}
219+
}
220+
}
221+
207222
void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
208223
int16_t x1, int16_t y1,
209224
int16_t x2, int16_t y2,

0 commit comments

Comments
 (0)