4545# replacement for, the FastLED library for Arduino.
4646
4747
48- class CRGB ( object ) :
48+ class CRGB :
4949 """Color stored in Red, Green, Blue color space.
5050
5151 One of two ways: separate red, gren, blue values (either as integers
@@ -68,26 +68,26 @@ def __init__(self, red, green=0.0, blue=0.0):
6868 if isinstance (red , CHSV ):
6969 # If first/only argument is a CHSV type, perform HSV to RGB
7070 # conversion.
71- hsv = red # 'red' is CHSV, this is just more readable
72- hue = hsv .hue * 6.0 # Hue circle = 0.0 to 6.0
73- sxt = floor (hue ) # Sextant index is next-lower integer of hue
74- frac = hue - sxt # Fraction-within-sextant is 0.0 to <1.0
71+ hsv = red # 'red' is CHSV, this is just more readable
72+ hue = hsv .hue * 6.0 # Hue circle = 0.0 to 6.0
73+ sxt = floor (hue ) # Sextant index is next-lower integer of hue
74+ frac = hue - sxt # Fraction-within-sextant is 0.0 to <1.0
7575 sxt = int (sxt ) % 6 # mod6 the sextant so it's always 0 to 5
7676
77- if sxt == 0 : # Red to <yellow
77+ if sxt == 0 : # Red to <yellow
7878 r , g , b = 1.0 , frac , 0.0
79- elif sxt == 1 : # Yellow to <green
79+ elif sxt == 1 : # Yellow to <green
8080 r , g , b = 1.0 - frac , 1.0 , 0.0
81- elif sxt == 2 : # Green to <cyan
81+ elif sxt == 2 : # Green to <cyan
8282 r , g , b = 0.0 , 1.0 , frac
83- elif sxt == 3 : # Cyan to <blue
83+ elif sxt == 3 : # Cyan to <blue
8484 r , g , b = 0.0 , 1.0 - frac , 1.0
85- elif sxt == 4 : # Blue to <magenta
85+ elif sxt == 4 : # Blue to <magenta
8686 r , g , b = frac , 0.0 , 1.0
87- else : # Magenta to <red
87+ else : # Magenta to <red
8888 r , g , b = 1.0 , 0.0 , 1.0 - frac
8989
90- invsat = 1.0 - hsv .saturation # Inverse-of-saturation
90+ invsat = 1.0 - hsv .saturation # Inverse-of-saturation
9191
9292 self .red = ((r * hsv .saturation ) + invsat ) * hsv .value
9393 self .green = ((g * hsv .saturation ) + invsat ) * hsv .value
@@ -122,25 +122,26 @@ def __getitem__(self, key):
122122 """Retrieve red, green or blue value as iterable."""
123123 if key == 0 :
124124 return self .red
125- elif key == 1 :
125+ if key == 1 :
126126 return self .green
127- elif key == 2 :
127+ if key == 2 :
128128 return self .blue
129- else :
130- raise IndexError
129+ raise IndexError
131130
132131 def pack (self ):
133132 """'Pack' a `CRGB` color into a 24-bit RGB integer.
134133
135134 :returns: 24-bit integer a la ``0x00RRGGBB``.
136135 """
137136
138- return ((denormalize (self .red ) << 16 ) |
139- (denormalize (self .green ) << 8 ) |
140- (denormalize (self .blue )))
137+ return (
138+ (denormalize (self .red ) << 16 )
139+ | (denormalize (self .green ) << 8 )
140+ | (denormalize (self .blue ))
141+ )
141142
142143
143- class CHSV ( object ) :
144+ class CHSV :
144145 """Color stored in Hue, Saturation, Value color space.
145146
146147 Accepts hue as float (any range) or integer (0-256 -> 0.0-1.0) with
@@ -187,12 +188,11 @@ def __getitem__(self, key):
187188 """Retrieve hue, saturation or value as iterable."""
188189 if key == 0 :
189190 return self .hue
190- elif key == 1 :
191+ if key == 1 :
191192 return self .saturation
192- elif key == 2 :
193+ if key == 2 :
193194 return self .value
194- else :
195- raise IndexError
195+ raise IndexError
196196
197197 def pack (self ):
198198 """'Pack' a `CHSV` color into a 24-bit RGB integer.
@@ -277,9 +277,11 @@ def unpack(val):
277277 # See notes in normalize() for math explanation. Large constants here
278278 # avoid the usual shift-right step, e.g. 16711680.0 is 255 * 256 * 256,
279279 # so we can just mask out the red and divide by this for 0.0 to 1.0.
280- return CRGB ((val & 0xFF0000 ) / 16711680.0 , # Red
281- (val & 0x00FF00 ) / 65280.0 , # Green
282- (val & 0x0000FF ) / 255.0 ) # Blue
280+ return CRGB (
281+ (val & 0xFF0000 ) / 16711680.0 , # Red
282+ (val & 0x00FF00 ) / 65280.0 , # Green
283+ (val & 0x0000FF ) / 255.0 ,
284+ ) # Blue
283285
284286
285287def mix (color1 , color2 , weight2 = 0.5 ):
@@ -323,13 +325,16 @@ def mix(color1, color2, weight2=0.5):
323325 color1 = unpack (color1 )
324326
325327 # Interpolate and return as CRGB type
326- return CRGB ((color1 .red * weight1 + color2 .red * weight2 ),
327- (color1 .green * weight1 + color2 .green * weight2 ),
328- (color1 .blue * weight1 + color2 .blue * weight2 ))
328+ return CRGB (
329+ (color1 .red * weight1 + color2 .red * weight2 ),
330+ (color1 .green * weight1 + color2 .green * weight2 ),
331+ (color1 .blue * weight1 + color2 .blue * weight2 ),
332+ )
329333
330334
331335GFACTOR = 2.7 # Default gamma-correction factor for function below
332336
337+
333338def gamma_adjust (val , gamma_value = None , brightness = 1.0 , inplace = False ):
334339 """Provides gamma adjustment for single values, `CRGB` and `CHSV` types
335340 and lists of any of these.
@@ -387,33 +392,47 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
387392 gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
388393 elif isinstance (gamma_value , float ):
389394 # Single gamma value provided, apply to R,G,B
390- gamma_red , gamma_green , gamma_blue = (
391- gamma_value , gamma_value , gamma_value )
395+ gamma_red , gamma_green , gamma_blue = (gamma_value , gamma_value , gamma_value )
392396 else :
393397 gamma_red , gamma_green , gamma_blue = (
394- gamma_value [0 ], gamma_value [1 ], gamma_value [2 ])
398+ gamma_value [0 ],
399+ gamma_value [1 ],
400+ gamma_value [2 ],
401+ )
395402 if isinstance (brightness , float ):
396403 # Single brightness value provided, apply to R,G,B
397404 brightness_red , brightness_green , brightness_blue = (
398- brightness , brightness , brightness )
405+ brightness ,
406+ brightness ,
407+ brightness ,
408+ )
399409 else :
400410 brightness_red , brightness_green , brightness_blue = (
401- brightness [0 ], brightness [1 ], brightness [2 ])
411+ brightness [0 ],
412+ brightness [1 ],
413+ brightness [2 ],
414+ )
402415 if inplace :
403416 for i , x in enumerate (val ):
404417 if isinstance (x , CHSV ):
405418 x = CRGB (x )
406- val [i ] = CRGB (pow (x .red , gamma_red ) * brightness_red ,
407- pow (x .green , gamma_green ) * brightness_green ,
408- pow (x .blue , gamma_blue ) * brightness_blue )
419+ val [i ] = CRGB (
420+ pow (x .red , gamma_red ) * brightness_red ,
421+ pow (x .green , gamma_green ) * brightness_green ,
422+ pow (x .blue , gamma_blue ) * brightness_blue ,
423+ )
409424 return None
410425 newlist = []
411426 for x in val :
412427 if isinstance (x , CHSV ):
413428 x = CRGB (x )
414- newlist .append (CRGB (pow (x .red , gamma_red ) * brightness_red ,
415- pow (x .green , gamma_green ) * brightness_green ,
416- pow (x .blue , gamma_blue ) * brightness_blue ))
429+ newlist .append (
430+ CRGB (
431+ pow (x .red , gamma_red ) * brightness_red ,
432+ pow (x .green , gamma_green ) * brightness_green ,
433+ pow (x .blue , gamma_blue ) * brightness_blue ,
434+ )
435+ )
417436 return newlist
418437
419438 # Single CRGB or CHSV value
@@ -422,25 +441,35 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
422441 gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
423442 elif isinstance (gamma_value , float ):
424443 # Single gamma value provided, apply to R,G,B
425- gamma_red , gamma_green , gamma_blue = (
426- gamma_value , gamma_value , gamma_value )
444+ gamma_red , gamma_green , gamma_blue = (gamma_value , gamma_value , gamma_value )
427445 else :
428446 gamma_red , gamma_green , gamma_blue = (
429- gamma_value [0 ], gamma_value [1 ], gamma_value [2 ])
447+ gamma_value [0 ],
448+ gamma_value [1 ],
449+ gamma_value [2 ],
450+ )
430451 if isinstance (brightness , float ):
431452 # Single brightness value provided, apply to R,G,B
432453 brightness_red , brightness_green , brightness_blue = (
433- brightness , brightness , brightness )
454+ brightness ,
455+ brightness ,
456+ brightness ,
457+ )
434458 else :
435459 brightness_red , brightness_green , brightness_blue = (
436- brightness [0 ], brightness [1 ], brightness [2 ])
460+ brightness [0 ],
461+ brightness [1 ],
462+ brightness [2 ],
463+ )
437464
438465 if isinstance (val , CHSV ):
439466 val = CRGB (val )
440467
441- return CRGB (pow (val .red , gamma_red ) * brightness_red ,
442- pow (val .green , gamma_green ) * brightness_green ,
443- pow (val .blue , gamma_blue ) * brightness_blue )
468+ return CRGB (
469+ pow (val .red , gamma_red ) * brightness_red ,
470+ pow (val .green , gamma_green ) * brightness_green ,
471+ pow (val .blue , gamma_blue ) * brightness_blue ,
472+ )
444473
445474
446475def palette_lookup (palette , position ):
@@ -454,13 +483,13 @@ def palette_lookup(palette, position):
454483
455484 position %= 1.0 # Wrap palette position in 0.0 to <1.0 range
456485
457- weight2 = position * len (palette ) # Scale position to palette length
458- idx = int (floor (weight2 )) # Index of 'lower' color (0 to len-1)
459- weight2 -= idx # Weighting of 'upper' color
486+ weight2 = position * len (palette ) # Scale position to palette length
487+ idx = int (floor (weight2 )) # Index of 'lower' color (0 to len-1)
488+ weight2 -= idx # Weighting of 'upper' color
460489
461- color1 = palette [idx ] # Fetch 'lower' color
462- idx = (idx + 1 ) % len (palette ) # Get index of 'upper' color
463- color2 = palette [idx ] # Fetch 'upper' color
490+ color1 = palette [idx ] # Fetch 'lower' color
491+ idx = (idx + 1 ) % len (palette ) # Get index of 'upper' color
492+ color2 = palette [idx ] # Fetch 'upper' color
464493
465494 return mix (color1 , color2 , weight2 )
466495
@@ -476,7 +505,7 @@ def expand_gradient(gradient, length):
476505 :returns: CRGB list, can be used with palette_lookup() function.
477506 """
478507
479- gradient = sorted (gradient ) # Sort list by position values
508+ gradient = sorted (gradient ) # Sort list by position values
480509 least = gradient [0 ][0 ] # Lowest position value (ostensibly 0.0)
481510 most = gradient [- 1 ][0 ] # Highest position value (ostensibly 1.0)
482511 newlist = []
@@ -503,7 +532,7 @@ def expand_gradient(gradient, length):
503532 # Range between below, above
504533 r = gradient [above ][0 ] - gradient [below ][0 ]
505534 if r <= 0 :
506- newlist .append (gradient [below ][1 ]) # Use 'below' color only
535+ newlist .append (gradient [below ][1 ]) # Use 'below' color only
507536 else :
508537 weight2 = (pos - gradient [below ][0 ]) / r # Weight of 'above' color
509538 color1 = gradient [below ][1 ]
0 commit comments