36
36
from math import floor
37
37
38
38
39
- """
40
- FancyLED provides color- and palette-related utilities for LED projects,
41
- offering a buttery smooth look instead of the usual 8-bit-like "blip blip"
42
- effects often seen with LEDs. It's loosely inspired by, but NOT a drop-in
43
- replacement for, the FastLED library for Arduino.
44
- """
39
+ # FancyLED provides color- and palette-related utilities for LED projects,
40
+ # offering a buttery smooth look instead of the usual 8-bit-like "blip blip"
41
+ # effects often seen with LEDs. It's loosely inspired by, but NOT a drop-in
42
+ # replacement for, the FastLED library for Arduino.
45
43
46
44
47
- class CRGB :
45
+ class CRGB ( object ) :
48
46
""" RGB (red, green, blue) color class.
49
47
"""
50
48
@@ -67,9 +65,9 @@ def __init__(self, red, green=0.0, blue=0.0):
67
65
# If first/only argument is a CHSV type, perform HSV to RGB
68
66
# conversion.
69
67
hsv = red # 'red' is CHSV, this is just more readable
70
- h = hsv .hue * 6.0 # Hue circle = 0.0 to 6.0
71
- sxt = floor (h ) # Sextant index is next-lower integer of hue
72
- frac = h - sxt # Fraction-within-sextant is 0.0 to <1.0
68
+ hue = hsv .hue * 6.0 # Hue circle = 0.0 to 6.0
69
+ sxt = floor (hue ) # Sextant index is next-lower integer of hue
70
+ frac = hue - sxt # Fraction-within-sextant is 0.0 to <1.0
73
71
sxt = int (sxt ) % 6 # mod6 the sextant so it's always 0 to 5
74
72
75
73
if sxt == 0 : # Red to <yellow
@@ -112,7 +110,7 @@ def __str__(self):
112
110
return "(%s, %s, %s)" % (self .red , self .green , self .blue )
113
111
114
112
115
- class CHSV :
113
+ class CHSV ( object ) :
116
114
""" HSV (hue, saturation, value) color class.
117
115
"""
118
116
@@ -220,7 +218,7 @@ def pack(val):
220
218
221
219
# Convert CHSV input to CRGB if needed
222
220
if isinstance (val , CHSV ):
223
- val = CRGB (val )
221
+ val = CRGB (val )
224
222
225
223
return ((denormalize (val .red ) << 16 ) |
226
224
(denormalize (val .green ) << 8 ) |
@@ -323,7 +321,7 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
323
321
gamma_value = GFACTOR
324
322
return pow (val , gamma_value ) * brightness
325
323
326
- if isinstance (val , list ) or isinstance ( val , tuple ):
324
+ if isinstance (val , ( list , tuple ) ):
327
325
# List or tuple of values
328
326
if isinstance (val [0 ], float ):
329
327
# Input appears to be a list of floats
@@ -341,27 +339,17 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
341
339
# but first determine gamma-correction factors for R,G,B:
342
340
if gamma_value is None :
343
341
# No gamma specified, use default
344
- gamma_red = GFACTOR
345
- gamma_green = GFACTOR
346
- gamma_blue = GFACTOR
342
+ gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
347
343
elif isinstance (gamma_value , float ):
348
344
# Single gamma value provided, apply to R,G,B
349
- gamma_red = gamma_value
350
- gamma_green = gamma_value
351
- gamma_blue = gamma_value
345
+ gamma_red , gamma_green , gamma_blue = gamma_value , gamma_value , gamma_value
352
346
else :
353
- gamma_red = gamma_value [0 ]
354
- gamma_green = gamma_value [1 ]
355
- gamma_blue = gamma_value [2 ]
347
+ gamma_red , gamma_green , gamma_blue = gamma_value [0 ], gamma_value [1 ], gamma_value [2 ]
356
348
if isinstance (brightness , float ):
357
349
# Single brightness value provided, apply to R,G,B
358
- brightness_red = brightness
359
- brightness_green = brightness
360
- brightness_blue = brightness
350
+ brightness_red , brightness_green , brightness_blue = brightness , brightness , brightness
361
351
else :
362
- brightness_red = brightness [0 ]
363
- brightness_green = brightness [1 ]
364
- brightness_blue = brightness [2 ]
352
+ brightness_red , brightness_green , brightness_blue = brightness [0 ], brightness [1 ], brightness [2 ]
365
353
if inplace :
366
354
for i , x in enumerate (val ):
367
355
if isinstance (x , CHSV ):
@@ -382,27 +370,17 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
382
370
# Single CRGB or CHSV value
383
371
if gamma_value is None :
384
372
# No gamma specified, use default
385
- gamma_red = GFACTOR
386
- gamma_green = GFACTOR
387
- gamma_blue = GFACTOR
373
+ gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
388
374
elif isinstance (gamma_value , float ):
389
375
# Single gamma value provided, apply to R,G,B
390
- gamma_red = gamma_value
391
- gamma_green = gamma_value
392
- gamma_blue = gamma_value
376
+ gamma_red , gamma_green , gamma_blue = gamma_value , gamma_value , gamma_value
393
377
else :
394
- gamma_red = gamma_value [0 ]
395
- gamma_green = gamma_value [1 ]
396
- gamma_blue = gamma_value [2 ]
378
+ gamma_red , gamma_green , gamma_blue = gamma_value [0 ], gamma_value [1 ], gamma_value [2 ]
397
379
if isinstance (brightness , float ):
398
380
# Single brightness value provided, apply to R,G,B
399
- brightness_red = brightness
400
- brightness_green = brightness
401
- brightness_blue = brightness
381
+ brightness_red , brightness_green , brightness_blue = brightness , brightness , brightness
402
382
else :
403
- brightness_red = brightness [0 ]
404
- brightness_green = brightness [1 ]
405
- brightness_blue = brightness [2 ]
383
+ brightness_red , brightness_green , brightness_blue = brightness [0 ], brightness [1 ], brightness [2 ]
406
384
407
385
if isinstance (val , CHSV ):
408
386
val = CRGB (val )
@@ -432,7 +410,7 @@ def palette_lookup(pal, pos):
432
410
return mix (color1 , color2 , weight2 )
433
411
434
412
435
- def expand_gradient (grad , len ):
413
+ def expand_gradient (grad , length ):
436
414
""" Convert gradient palette into standard equal-interval palette.
437
415
ACCEPTS: List or tuple of of 2-element lists/tuples containing position
438
416
(0.0 to 1.0) and color (packed int, CRGB or CHSV). It's OK if
@@ -446,8 +424,8 @@ def expand_gradient(grad, len):
446
424
most = grad [- 1 ][0 ] # Highest position value (ostensibly 1.0)
447
425
newlist = []
448
426
449
- for i in range (len ):
450
- pos = i / float (len - 1 ) # 0.0 to 1.0 in 'len ' steps
427
+ for i in range (length ):
428
+ pos = i / float (length - 1 ) # 0.0 to 1.0 in 'length ' steps
451
429
# Determine indices in list of item 'below' and 'above' pos
452
430
if pos <= least :
453
431
# Off bottom of list - use lowest index
0 commit comments