Skip to content

Commit b68c704

Browse files
committed
Allow setting RGBW pixels with RGB tuples
1 parent db4dbe0 commit b68c704

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t
5858
//| When brightness is less than 1.0, a second buffer will be used to store the color values
5959
//| before they are adjusted for brightness.
6060
//|
61-
//| When ``P`` (pwm duration) is present as the first character of the byteorder
61+
//| When ``P`` (PWM duration) is present as the first character of the byteorder
6262
//| string, the 4th value in the tuple/list for a pixel is the individual pixel
63-
//| brightness (0.0-1.0) and will enable a Dotstar compatible 1st byte in the
63+
//| brightness (0.0-1.0) and will enable a DotStar compatible 1st byte in the
6464
//| output buffer (``buf``).
6565
//|
6666
//| :param ~int size: Number of pixels
67-
//| :param ~str byteorder: Byte order string (such as "BGR" or "BGRP")
67+
//| :param ~str byteorder: Byte order string (such as "RGB", "RGBW" or "PBGR")
6868
//| :param ~float brightness: Brightness (0 to 1.0, default 1.0)
6969
//| :param ~bool auto_write: Whether to automatically write pixels (Default False)
7070
//| :param bytes header: Sequence of bytes to always send before pixel values.
@@ -281,13 +281,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_fill_obj, pixelbuf_pixelbuf_f
281281
//| .. method:: __getitem__(index)
282282
//|
283283
//| Returns the pixel value at the given index as a tuple of (Red, Green, Blue[, White]) values
284-
//| between 0 and 255.
284+
//| between 0 and 255. When in PWM (DotStar) mode, the 4th tuple value is a float of the pixel
285+
//| intensity from 0-1.0.
285286
//|
286287
//| .. method:: __setitem__(index, value)
287288
//|
288-
//| Sets the pixel value at the given index. Value can either be a tuple of (Red, Green, Blue
289-
//| [, White]) values between 0 and 255 or an integer where the red, green and blue values are
290-
//| packed into the lower three bytes (0xRRGGBB).
289+
//| Sets the pixel value at the given index. Value can either be a tuple or integer. Tuples are
290+
//| The individual (Red, Green, Blue[, White]) values between 0 and 255. If given an integer, the
291+
//| red, green and blue values are packed into the lower three bytes (0xRRGGBB).
292+
//| For RGBW byteorders, if given only RGB values either as an int or as a tuple, the white value
293+
//| is used instead when red, green, and blue are the same value.
291294
//|
292295
STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
293296
if (value == MP_OBJ_NULL) {

shared-module/_pixelbuf/PixelBuf.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,11 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
147147
*r = value >> 16 & 0xff;
148148
*g = (value >> 8) & 0xff;
149149
*b = value & 0xff;
150-
// Int colors can't set white directly so convert to white when all components are equal.
151-
if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
152-
*w = *r;
153-
*r = 0;
154-
*g = 0;
155-
*b = 0;
156-
}
157150
} else {
158151
mp_obj_t *items;
159152
size_t len;
160153
mp_obj_get_array(color, &len, &items);
161-
if (len != byteorder->bpp && !byteorder->is_dotstar) {
154+
if (len < 3 || len > 4) {
162155
mp_raise_ValueError_varg(translate("Expected tuple of length %d, got %d"), byteorder->bpp, len);
163156
}
164157

@@ -171,8 +164,17 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
171164
} else {
172165
*w = mp_obj_get_int_truncated(items[PIXEL_W]);
173166
}
167+
return;
174168
}
175169
}
170+
// Int colors can't set white directly so convert to white when all components are equal.
171+
// Also handles RGBW values assigned an RGB tuple.
172+
if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
173+
*w = *r;
174+
*r = 0;
175+
*g = 0;
176+
*b = 0;
177+
}
176178
}
177179

178180
void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t* self, size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {

0 commit comments

Comments
 (0)