@@ -418,6 +418,46 @@ static mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
418418}
419419MP_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 ) },
0 commit comments