Skip to content

Commit 9bb2d70

Browse files
committed
displayio: Pass transparent_color to ColorConverter
Signed-off-by: Jensen <[email protected]>
1 parent 66c2566 commit 9bb2d70

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

shared-bindings/displayio/ColorConverter.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,27 @@
4343
//| """Create a ColorConverter object to convert color formats. Only supports RGB888 to RGB565
4444
//| currently.
4545
//| :param bool dither: Adds random noise to dither the output image"""
46+
//| :param bool transparent_color: Sets one color in the colorset to transparent"""
4647
//| ...
4748
//|
4849

4950
// TODO(tannewt): Add support for other color formats.
5051
//|
5152
STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
5253
enum { ARG_dither};
54+
enum { ARG_transparent_color };
5355

5456
static const mp_arg_t allowed_args[] = {
5557
{ MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
58+
{ MP_QSTR_transparent_color, MP_ARG_KW_ONLY | MP_ARG_INT, {.uint32_t = 0} },
5659
};
5760
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
5861
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
5962

6063
displayio_colorconverter_t *self = m_new_obj(displayio_colorconverter_t);
6164
self->base.type = &displayio_colorconverter_type;
6265

63-
common_hal_displayio_colorconverter_construct(self, args[ARG_dither].u_bool);
66+
common_hal_displayio_colorconverter_construct(self, args[ARG_dither].u_bool, args[ARG_transparent_color].uint32_t);
6467

6568
return MP_OBJ_FROM_PTR(self);
6669
}

shared-bindings/displayio/ColorConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
extern const mp_obj_type_t displayio_colorconverter_type;
3535

36-
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither);
36+
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither, uint32_t transparent_color);
3737
void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color);
3838

3939
void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t* self, bool dither);

shared-module/displayio/ColorConverter.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) {
3939
return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF);
4040
}
4141

42-
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither) {
42+
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither, uint32_t transparent_color) {
4343
self->dither = dither;
44+
self->transparent_color = transparent_color;
4445
}
4546

4647
uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) {
@@ -161,6 +162,9 @@ void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d
161162
}
162163
output_color->pixel = packed;
163164
output_color->opaque = true;
165+
if (self->transparent_color == pixel) {
166+
output_color->opaque = false;
167+
}
164168
return;
165169
} else if (colorspace->tricolor) {
166170
uint8_t luma = displayio_colorconverter_compute_luma(pixel);
@@ -170,6 +174,9 @@ void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d
170174
output_color->pixel = 0;
171175
}
172176
output_color->opaque = true;
177+
if (self->transparent_color == pixel) {
178+
output_color->opaque = false;
179+
}
173180
return;
174181
}
175182
uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel);
@@ -179,6 +186,9 @@ void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d
179186
uint8_t luma = displayio_colorconverter_compute_luma(pixel);
180187
output_color->pixel = luma >> (8 - colorspace->depth);
181188
output_color->opaque = true;
189+
if (self->transparent_color == pixel) {
190+
output_color->opaque = false;
191+
}
182192
return;
183193
}
184194
output_color->opaque = false;

shared-module/displayio/ColorConverter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
typedef struct {
3737
mp_obj_base_t base;
3838
bool dither;
39+
uint32_t transparent_color;
3940
} displayio_colorconverter_t;
4041

4142
bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self);

0 commit comments

Comments
 (0)