45
45
# replacement for, the FastLED library for Arduino.
46
46
47
47
48
- class CRGB ( object ) :
48
+ class CRGB :
49
49
"""Color stored in Red, Green, Blue color space.
50
50
51
51
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):
68
68
if isinstance (red , CHSV ):
69
69
# If first/only argument is a CHSV type, perform HSV to RGB
70
70
# 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
75
75
sxt = int (sxt ) % 6 # mod6 the sextant so it's always 0 to 5
76
76
77
- if sxt == 0 : # Red to <yellow
77
+ if sxt == 0 : # Red to <yellow
78
78
r , g , b = 1.0 , frac , 0.0
79
- elif sxt == 1 : # Yellow to <green
79
+ elif sxt == 1 : # Yellow to <green
80
80
r , g , b = 1.0 - frac , 1.0 , 0.0
81
- elif sxt == 2 : # Green to <cyan
81
+ elif sxt == 2 : # Green to <cyan
82
82
r , g , b = 0.0 , 1.0 , frac
83
- elif sxt == 3 : # Cyan to <blue
83
+ elif sxt == 3 : # Cyan to <blue
84
84
r , g , b = 0.0 , 1.0 - frac , 1.0
85
- elif sxt == 4 : # Blue to <magenta
85
+ elif sxt == 4 : # Blue to <magenta
86
86
r , g , b = frac , 0.0 , 1.0
87
- else : # Magenta to <red
87
+ else : # Magenta to <red
88
88
r , g , b = 1.0 , 0.0 , 1.0 - frac
89
89
90
- invsat = 1.0 - hsv .saturation # Inverse-of-saturation
90
+ invsat = 1.0 - hsv .saturation # Inverse-of-saturation
91
91
92
92
self .red = ((r * hsv .saturation ) + invsat ) * hsv .value
93
93
self .green = ((g * hsv .saturation ) + invsat ) * hsv .value
@@ -122,25 +122,26 @@ def __getitem__(self, key):
122
122
"""Retrieve red, green or blue value as iterable."""
123
123
if key == 0 :
124
124
return self .red
125
- elif key == 1 :
125
+ if key == 1 :
126
126
return self .green
127
- elif key == 2 :
127
+ if key == 2 :
128
128
return self .blue
129
- else :
130
- raise IndexError
129
+ raise IndexError
131
130
132
131
def pack (self ):
133
132
"""'Pack' a `CRGB` color into a 24-bit RGB integer.
134
133
135
134
:returns: 24-bit integer a la ``0x00RRGGBB``.
136
135
"""
137
136
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
+ )
141
142
142
143
143
- class CHSV ( object ) :
144
+ class CHSV :
144
145
"""Color stored in Hue, Saturation, Value color space.
145
146
146
147
Accepts hue as float (any range) or integer (0-256 -> 0.0-1.0) with
@@ -187,12 +188,11 @@ def __getitem__(self, key):
187
188
"""Retrieve hue, saturation or value as iterable."""
188
189
if key == 0 :
189
190
return self .hue
190
- elif key == 1 :
191
+ if key == 1 :
191
192
return self .saturation
192
- elif key == 2 :
193
+ if key == 2 :
193
194
return self .value
194
- else :
195
- raise IndexError
195
+ raise IndexError
196
196
197
197
def pack (self ):
198
198
"""'Pack' a `CHSV` color into a 24-bit RGB integer.
@@ -277,9 +277,11 @@ def unpack(val):
277
277
# See notes in normalize() for math explanation. Large constants here
278
278
# avoid the usual shift-right step, e.g. 16711680.0 is 255 * 256 * 256,
279
279
# 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
283
285
284
286
285
287
def mix (color1 , color2 , weight2 = 0.5 ):
@@ -323,13 +325,16 @@ def mix(color1, color2, weight2=0.5):
323
325
color1 = unpack (color1 )
324
326
325
327
# 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
+ )
329
333
330
334
331
335
GFACTOR = 2.7 # Default gamma-correction factor for function below
332
336
337
+
333
338
def gamma_adjust (val , gamma_value = None , brightness = 1.0 , inplace = False ):
334
339
"""Provides gamma adjustment for single values, `CRGB` and `CHSV` types
335
340
and lists of any of these.
@@ -387,33 +392,47 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
387
392
gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
388
393
elif isinstance (gamma_value , float ):
389
394
# 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 )
392
396
else :
393
397
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
+ )
395
402
if isinstance (brightness , float ):
396
403
# Single brightness value provided, apply to R,G,B
397
404
brightness_red , brightness_green , brightness_blue = (
398
- brightness , brightness , brightness )
405
+ brightness ,
406
+ brightness ,
407
+ brightness ,
408
+ )
399
409
else :
400
410
brightness_red , brightness_green , brightness_blue = (
401
- brightness [0 ], brightness [1 ], brightness [2 ])
411
+ brightness [0 ],
412
+ brightness [1 ],
413
+ brightness [2 ],
414
+ )
402
415
if inplace :
403
416
for i , x in enumerate (val ):
404
417
if isinstance (x , CHSV ):
405
418
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
+ )
409
424
return None
410
425
newlist = []
411
426
for x in val :
412
427
if isinstance (x , CHSV ):
413
428
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
+ )
417
436
return newlist
418
437
419
438
# Single CRGB or CHSV value
@@ -422,25 +441,35 @@ def gamma_adjust(val, gamma_value=None, brightness=1.0, inplace=False):
422
441
gamma_red , gamma_green , gamma_blue = GFACTOR , GFACTOR , GFACTOR
423
442
elif isinstance (gamma_value , float ):
424
443
# 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 )
427
445
else :
428
446
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
+ )
430
451
if isinstance (brightness , float ):
431
452
# Single brightness value provided, apply to R,G,B
432
453
brightness_red , brightness_green , brightness_blue = (
433
- brightness , brightness , brightness )
454
+ brightness ,
455
+ brightness ,
456
+ brightness ,
457
+ )
434
458
else :
435
459
brightness_red , brightness_green , brightness_blue = (
436
- brightness [0 ], brightness [1 ], brightness [2 ])
460
+ brightness [0 ],
461
+ brightness [1 ],
462
+ brightness [2 ],
463
+ )
437
464
438
465
if isinstance (val , CHSV ):
439
466
val = CRGB (val )
440
467
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
+ )
444
473
445
474
446
475
def palette_lookup (palette , position ):
@@ -454,13 +483,13 @@ def palette_lookup(palette, position):
454
483
455
484
position %= 1.0 # Wrap palette position in 0.0 to <1.0 range
456
485
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
460
489
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
464
493
465
494
return mix (color1 , color2 , weight2 )
466
495
@@ -476,7 +505,7 @@ def expand_gradient(gradient, length):
476
505
:returns: CRGB list, can be used with palette_lookup() function.
477
506
"""
478
507
479
- gradient = sorted (gradient ) # Sort list by position values
508
+ gradient = sorted (gradient ) # Sort list by position values
480
509
least = gradient [0 ][0 ] # Lowest position value (ostensibly 0.0)
481
510
most = gradient [- 1 ][0 ] # Highest position value (ostensibly 1.0)
482
511
newlist = []
@@ -503,7 +532,7 @@ def expand_gradient(gradient, length):
503
532
# Range between below, above
504
533
r = gradient [above ][0 ] - gradient [below ][0 ]
505
534
if r <= 0 :
506
- newlist .append (gradient [below ][1 ]) # Use 'below' color only
535
+ newlist .append (gradient [below ][1 ]) # Use 'below' color only
507
536
else :
508
537
weight2 = (pos - gradient [below ][0 ]) / r # Weight of 'above' color
509
538
color1 = gradient [below ][1 ]
0 commit comments