@@ -227,31 +227,39 @@ def _parse_color(self, value):
227
227
g = (value >> 8 ) & 0xFF
228
228
b = value & 0xFF
229
229
w = 0
230
- # If all components are the same and we have a white pixel then use it
231
- # instead of the individual components.
232
- if self ._bpp == 4 and self ._has_white and r == g and g == b :
230
+
231
+ if self ._dotstar_mode :
232
+ w = 1.0
233
+ else :
234
+ if len (value ) < 3 or len (value ) > 4 :
235
+ raise ValueError (
236
+ "Expected tuple of length {}, got {}" .format (self ._bpp , len (value ))
237
+ )
238
+ if len (value ) == self ._bpp :
239
+ if self ._bpp == 3 :
240
+ r , g , b = value
241
+ else :
242
+ r , g , b , w = value
243
+ elif len (value ) == 3 :
244
+ r , g , b = value
245
+ if self ._dotstar_mode :
246
+ w = 1.0
247
+
248
+ if self ._bpp == 4 :
249
+ if self ._dotstar_mode :
250
+ # LED startframe is three "1" bits, followed by 5 brightness bits
251
+ # then 8 bits for each of R, G, and B. The order of those 3 are configurable and
252
+ # vary based on hardware
253
+ # same as math.ceil(brightness * 31) & 0b00011111
254
+ # Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions
255
+ w = (32 - int (32 - w * 31 ) & 0b00011111 ) | DOTSTAR_LED_START
256
+ elif self ._has_white and r == g and g == b :
257
+ # If all components are the same and we have a white pixel then use it
258
+ # instead of the individual components.
233
259
w = r
234
260
r = 0
235
261
g = 0
236
262
b = 0
237
- elif self ._dotstar_mode :
238
- w = 1.0
239
- elif len (value ) == self ._bpp :
240
- if self ._bpp == 3 :
241
- r , g , b = value
242
- else :
243
- r , g , b , w = value
244
- elif len (value ) == 3 and self ._dotstar_mode :
245
- r , g , b = value
246
- w = 1.0
247
-
248
- if self ._bpp == 4 and self ._dotstar_mode :
249
- # LED startframe is three "1" bits, followed by 5 brightness bits
250
- # then 8 bits for each of R, G, and B. The order of those 3 are configurable and
251
- # vary based on hardware
252
- # same as math.ceil(brightness * 31) & 0b00011111
253
- # Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions
254
- w = (32 - int (32 - w * 31 ) & 0b00011111 ) | DOTSTAR_LED_START
255
263
256
264
return (r , g , b , w )
257
265
0 commit comments