-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorutils.py
2390 lines (1942 loc) · 74.5 KB
/
colorutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# @file colorutils.h
# functions for color fill, paletters, blending, and more
from .FatsLED import *
from .pixeltypes import *
from fastled_progmem import *
from math import *
# @defgroup Colorutils Color utility functions
# A variety of functions for working with color, palletes, and leds
# @{
def fill_solid(leds, numToFill, color):
for i in range(numToFill):
if isinstance(color, CHSV):
leds[i].setHSV(*color.raw)
else:
leds[i].setRGB(*color.raw)
# fill_rainbow - fill a range of LEDs with a rainbow of colors, at
# full saturation and full value (brightness)
def fill_rainbow(targetArray, numToFill, initialhue, deltahue):
hsv = CHSV(initialhue, 240, 255)
for i in range(numToFill):
targetArray[i].setHSV(*hsv.raw)
hsv.hue += deltahue
# fill_gradient - fill an array of colors with a smooth HSV gradient
# between two specified HSV colors.
# Since 'hue' is a value around a color wheel,
# there are always two ways to sweep from one hue
# to another.
# This function lets you specify which way you want
# the hue gradient to sweep around the color wheel:
# FORWARD_HUES: hue always goes clockwise
# BACKWARD_HUES: hue always goes counter-clockwise
# SHORTEST_HUES: hue goes whichever way is shortest
# LONGEST_HUES: hue goes whichever way is longest
# The default is SHORTEST_HUES, as this is nearly
# always what is wanted.
#
# fill_gradient can write the gradient colors EITHER
# (1) into an array of CRGBs (e.g., into leds[] array, or an RGB Palette)
# OR
# (2) into an array of CHSVs (e.g. an HSV Palette).
#
# In the case of writing into a CRGB array, the gradient is
# computed in HSV space, and then HSV values are converted to RGB
# as they're written into the RGB array.
FORWARD_HUES = 0
BACKWARD_HUES = 1
SHORTEST_HUES = 2
LONGEST_HUES = 3
saccum87 = int
# fill_gradient - fill an array of colors with a smooth HSV gradient
# between two specified HSV colors.
# Since 'hue' is a value around a color wheel,
# there are always two ways to sweep from one hue
# to another.
# This function lets you specify which way you want
# the hue gradient to sweep around the color wheel:
#
# FORWARD_HUES: hue always goes clockwise
# BACKWARD_HUES: hue always goes counter-clockwise
# SHORTEST_HUES: hue goes whichever way is shortest
# LONGEST_HUES: hue goes whichever way is longest
#
# The default is SHORTEST_HUES, as this is nearly
# always what is wanted.
#
# fill_gradient can write the gradient colors EITHER
# (1) into an array of CRGBs (e.g., into leds[] array, or an RGB Palette)
# OR
# (2) into an array of CHSVs (e.g. an HSV Palette).
#
# In the case of writing into a CRGB array, the gradient is
# computed in HSV space, and then HSV values are converted to RGB
# as they're written into the RGB array.
# Convenience functions to fill an array of colors with a
# two-color, three-color, or four-color gradient
def fill_gradient(targetArray, startpos, startcolor, endpos, endcolor=SHORTEST_HUES, directionCode=SHORTEST_HUES, c1=SHORTEST_HUES, c2=None, c3=None, c4=None, numLeds=None):
if isinstance(directionCode, CHSV):
directionCode, c4 = c4, directionCode
c1 = startcolor
c2 = endpos
c3 = endcolor
elif isinstance(endcolor, CHSV):
c3 = endcolor
c2 = endpos
c1 = startcolor
elif isinstance(endpos, CHSV):
c2 = endpos
c1 = startcolor
directionCode = endcolor
if c4 is not None:
onethird = numLeds / 3
twothirds = (numLeds * 2) / 3
last = numLeds - 1
fill_gradient(targetArray, 0, c1, onethird, c2, directionCode)
fill_gradient(targetArray, onethird, c2, twothirds, c3, directionCode)
fill_gradient(targetArray, twothirds, c3, last, c4, directionCode)
elif c3 is not None:
half = (numLeds / 2)
last = numLeds - 1
fill_gradient(targetArray, 0, c1, half, c2, directionCode)
fill_gradient(targetArray, half, c2, last, c3, directionCode)
elif c2 is not None:
last = numLeds - 1
fill_gradient(targetArray, 0, c1, last, c2, directionCode)
else:
# if the points are in the wrong order, straighten them
if endpos < startpos:
t = endpos
tc = CHSV(endcolor)
endcolor = startcolor
endpos = startpos
startpos = t
startcolor = tc
# If we're fading toward black (val=0) or white (sat=0),
# then set the endhue to the starthue.
# This lets us ramp smoothly to black or white, regardless
# of what 'hue' was set in the endcolor (since it doesn't matter)
if endcolor.value == 0 == endcolor.saturation:
endcolor.hue = startcolor.hue
# Similarly, if we're fading in from black (val=0) or white (sat=0)
# then set the starthue to the endhue.
# This lets us ramp smoothly up from black or white, regardless
# of what 'hue' was set in the startcolor (since it doesn't matter)
if startcolor.value == 0 == startcolor.saturation:
startcolor.hue = endcolor.hue
satdistance87 = saccum87((endcolor.sat - startcolor.sat) << 7)
valdistance87 = saccum87((endcolor.val - startcolor.val) << 7)
huedelta8 = endcolor.hue - startcolor.hue
if directionCode == SHORTEST_HUES:
directionCode = FORWARD_HUES
if huedelta8 > 127:
directionCode = BACKWARD_HUES
if directionCode == LONGEST_HUES:
directionCode = FORWARD_HUES
if huedelta8 < 128:
directionCode = BACKWARD_HUES
if directionCode == FORWARD_HUES:
huedistance87 = saccum87(huedelta8 << 7)
else: # directionCode == BACKWARD_HUES
huedistance87 = saccum87((256 - huedelta8) << 7)
huedistance87 = -huedistance87
pixeldistance = endpos - startpos
divisor = pixeldistance if pixeldistance else 1
huedelta87 = saccum87(huedistance87 / divisor)
satdelta87 = saccum87(satdistance87 / divisor)
valdelta87 = saccum87(valdistance87 / divisor)
huedelta87 *= 2
satdelta87 *= 2
valdelta87 *= 2
hue88 = accum88(startcolor.hue << 8)
sat88 = accum88(startcolor.sat << 8)
val88 = accum88(startcolor.val << 8)
for i in range(startpos + 1):
targetArray[i] = CHSV(hue88 >> 8, sat88 >> 8, val88 >> 8)
hue88 += huedelta87
sat88 += satdelta87
val88 += valdelta87
# convenience synonym
fill_gradient_HSV = fill_gradient
# fill_gradient_RGB - fill a range of LEDs with a smooth RGB gradient
# between two specified RGB colors.
# Unlike HSV, there is no 'color wheel' in RGB space,
# and therefore there's only one 'direction' for the
# gradient to go, and no 'direction code' is needed.
def fill_gradient_RGB(leds, startpos, startcolor, endpos, endcolor, c1=None, c2=None, c3=None, c4=None):
if isinstance(endpos, CRGB):
c4 = endpos
c3 = startcolor
c2 = startpos
c1 = leds
leds = FastLED[0].leds()
numLeds = FastLED[0].size()
onethird = (numLeds / 3)
twothirds = ((numLeds * 2) / 3)
last = numLeds - 1
fill_gradient_RGB(leds, 0, c1, onethird, c2)
fill_gradient_RGB(leds, onethird, c2, twothirds, c3)
fill_gradient_RGB(leds, twothirds, c3, last, c4)
elif isinstance(startcolor, CRGB) and isinstance(startpos, CRGB):
c3 = startcolor
c2 = startpos
c1 = leds
leds = FastLED[0].leds()
numLeds = FastLED[0].size()
half = (numLeds / 2)
last = numLeds - 1
fill_gradient_RGB(leds, 0, c1, half, c2)
fill_gradient_RGB(leds, half, c2, last, c3)
elif isinstance(startpos, CRGB):
c2 = startpos
c1 = leds
leds = FastLED[0].leds()
numLeds = FastLED[0].size()
last = numLeds - 1
fill_gradient_RGB(leds, 0, c1, last, c2)
else:
# if the points are in the wrong order, straighten them
if endpos < startpos:
t = endpos
tc = endcolor
endcolor = startcolor
endpos = startpos
startpos = t
startcolor = tc
rdistance87 = saccum87((endcolor.r - startcolor.r) << 7)
gdistance87 = saccum87((endcolor.g - startcolor.g) << 7)
bdistance87 = saccum87((endcolor.b - startcolor.b) << 7)
pixeldistance = endpos - startpos
divisor = pixeldistance if pixeldistance else 1
rdelta87 = saccum87(rdistance87 / divisor)
gdelta87 = saccum87(gdistance87 / divisor)
bdelta87 = saccum87(bdistance87 / divisor)
rdelta87 *= 2
gdelta87 *= 2
bdelta87 *= 2
r88 = accum88(startcolor.r << 8)
g88 = accum88(startcolor.g << 8)
b88 = accum88(startcolor.b << 8)
for i in range(startpos, endpos):
leds[i] = CRGB(r88 >> 8, g88 >> 8, b88 >> 8)
r88 += rdelta87
g88 += gdelta87
b88 += bdelta87
# fadeLightBy and fade_video - reduce the brightness of an array
# of pixels all at once. Guaranteed
# to never fade all the way to black.
# (The two names are synonyms.)
def fadeLightBy(leds, num_leds, fadeBy):
nscale8_video(leds, num_leds, 255 - fadeBy)
def fade_video(leds, num_leds, fadeBy):
nscale8_video(leds, num_leds, 255 - fadeBy)
# nscale8_video - scale down the brightness of an array of pixels
# all at once. Guaranteed to never scale a pixel
# all the way down to black, unless 'scale' is zero.
def nscale8_video(leds, num_leds, scale):
for i in range(num_leds):
leds[i].nscale8_video(scale)
# fadeToBlackBy and fade_raw - reduce the brightness of an array
# of pixels all at once. These
# functions will eventually fade all
# the way to black.
# (The two names are synonyms.)
def fadeToBlackBy(leds, num_leds, fadeBy):
nscale8(leds, num_leds, 255 - fadeBy)
def fade_raw(leds, num_leds, fadeBy):
nscale8( leds, num_leds, 255 - fadeBy)
# nscale8 - scale down the brightness of an array of pixels
# all at once. This function can scale pixels all the
# way down to black even if 'scale' is not zero.
def nscale8(leds, num_leds, scale):
for i in range(num_leds):
leds[i].nscale8(scale)
def nscale8_raw(leds, num_leds, scale):
nscale8(leds, num_leds, scale)
# fadeUsingColor - scale down the brightness of an array of pixels,
# as though it were seen through a transparent
# filter with the specified color.
# For example, if the colormask is
# CRGB( 200, 100, 50)
# then the pixels' red will be faded to 200/256ths,
# their green to 100/256ths, and their blue to 50/256ths.
# This particular example give a 'hot fade' look,
# with white fading to yellow, then red, then black.
# You can also use colormasks like CRGB::Blue to
# zero out the red and green elements, leaving blue
# (largely) the same.
def fadeUsingColor(leds, numLeds, colormask):
fr = colormask.r
fg = colormask.g
fb = colormask.b
for i in range(numLeds):
leds[i].r = scale8_LEAVING_R1_DIRTY( leds[i].r, fr)
leds[i].g = scale8_LEAVING_R1_DIRTY( leds[i].g, fg)
leds[i].b = scale8(leds[i].b, fb)
# Pixel blending
#
# blend - computes a new color blended some fraction of the way
# between two other colors.
def blend(src1, src2, dest, count=None, amountOfsrc2=None, directionCode=SHORTEST_HUES):
if isinstance(dest, CRGB):
for i in range(count):
dest[i] = blend(src1[i], src2[i], amountOfsrc2)
return dest
elif isinstance(dest, CHSV):
for i in range(count):
dest[i] = blend(src1[i], src2[i], amountOfsrc2, directionCode)
return dest
elif count is None:
amountOfsrc2 = dest
nu = CRGB(src1)
nblend(nu, src2, amountOfsrc2)
return nu
else:
amountOfsrc2 = dest
directionCode = count if count is not None else directionCode
nu = CHSV(src1)
nblend(nu, src2, amountOfsrc2, directionCode)
return nu
# nblend - destructively modifies one color, blending
# in a given fraction of an overlay color
def nblend(existing, overlay, count, amountOfOverlay=None, directionCode=SHORTEST_HUES):
if isinstance(existing, CRGB):
if amountOfOverlay is None:
amountOfOverlay = count
if amountOfOverlay == 0:
return existing
if amountOfOverlay == 255:
existing = overlay
return existing
# Corrected blend method, with no loss-of-precision rounding errors
existing.red = blend8(existing.red, overlay.red, amountOfOverlay)
existing.green = blend8(existing.green, overlay.green, amountOfOverlay)
existing.blue = blend8(existing.blue, overlay.blue, amountOfOverlay)
return existing
else:
pos = 0
for i in range(count, -1, -1):
nblend(existing[pos:], overlay[pos:], amountOfOverlay)
pos += 1
else:
if isinstance(count, fract8):
directionCode = amountOfOverlay if amountOfOverlay is not None else directionCode
amountOfOverlay = count
if amountOfOverlay == 0:
return existing
if amountOfOverlay == 255:
existing = overlay
return existing
amountOfKeep = fract8(255 - amountOfOverlay)
huedelta8 = overlay.hue - existing.hue
if directionCode == SHORTEST_HUES:
directionCode = FORWARD_HUES
if huedelta8 > 127:
directionCode = BACKWARD_HUES
if directionCode == LONGEST_HUES:
directionCode = FORWARD_HUES
if huedelta8 < 128:
directionCode = BACKWARD_HUES
if directionCode == FORWARD_HUES:
existing.hue = existing.hue + scale8(huedelta8, amountOfOverlay)
else: # directionCode == BACKWARD_HUES
huedelta8 = -huedelta8
existing.hue = existing.hue - scale8(huedelta8, amountOfOverlay)
existing.sat = (
scale8_LEAVING_R1_DIRTY(existing.sat, amountOfKeep) +
scale8_LEAVING_R1_DIRTY(overlay.sat, amountOfOverlay)
)
existing.val = (
scale8_LEAVING_R1_DIRTY(existing.val, amountOfKeep) +
scale8_LEAVING_R1_DIRTY(overlay.val, amountOfOverlay)
)
cleanup_R1()
return existing
else:
if existing == overlay:
return
pos = 0
for i in range(count, 0, -1):
nblend(existing[pos:], overlay[pos:], amountOfOverlay, directionCode)
pos += 1
# blur1d: one-dimensional blur filter. Spreads light to 2 line neighbors.
# blur2d: two-dimensional blur filter. Spreads light to 8 XY neighbors.
#
# 0 = no spread at all
# 64 = moderate spreading
# 172 = maximum smooth, even spreading
#
# 173..255 = wider spreading, but increasing flicker
#
# Total light is NOT entirely conserved, so many repeated
# calls to 'blur' will also result in the light fading,
# eventually all the way to black this is by design so that
# it can be used to (slowly) clear the LEDs to black.
def blur1d(leds, numLeds, blur_amount):
keep = 255 - blur_amount
seep = blur_amount >> 1
carryover = CRGB(CRGB.Black)
for i in range(numLeds):
cur = CRGB(leds[i])
part = CRGB(cur)
part.nscale8(seep)
cur.nscale8(keep)
cur += carryover
if i:
leds[i - 1] += part
leds[i] = cur
carryover = part
def blur2d(leds, width, height, blur_amount):
blurRows(leds, width, height, blur_amount)
blurColumns(leds, width, height, blur_amount)
# blurRows: perform a blur1d on every row of a rectangular matrix
def blurRows(leds, width, height, blur_amount):
for row in range(height):
rowbase = leds[row * width:]
blur1d(rowbase, width, blur_amount)
# blurColumns: perform a blur1d on each column of a rectangular matrix
def blurColumns(leds, width, height, blur_amount):
keep = 255 - blur_amount
seep = blur_amount >> 1
for col in range(width):
carryover = CRGB(CRGB.Black)
for i in range(height):
cur = CRGB(leds[XY(col, i)])
part = CRGB(cur)
part.nscale8(seep)
cur.nscale8(keep)
cur += carryover
if i:
leds[XY(col, i - 1)] += part
leds[XY(col, i)] = cur
carryover = part
# CRGB HeatColor( uint8_t temperature)
#
# Approximates a 'black body radiation' spectrum for
# a given 'heat' level. This is useful for animations of 'fire'.
# Heat is specified as an arbitrary scale from 0 (cool) to 255 (hot).
# This is NOT a chromatically correct 'black body radiation'
# spectrum, but it's surprisingly close, and it's fast and small.
def HeatColor(temperature):
heatcolor = CRGB()
# Scale 'heat' down from 0-255 to 0-191,
# which can then be easily divided into three
# equal 'thirds' of 64 units each.
t192 = scale8_video(temperature, 191)
# calculate a value that ramps up from
# zero to 255 in each 'third' of the scale.
heatramp = t192 & 0x3F # 0..63
heatramp <<= 2 # scale up to 0..252
# now figure out which third of the spectrum we're in:
if t192 & 0x80:
# we're in the hottest third
heatcolor.r = 255 # full red
heatcolor.g = 255 # full green
heatcolor.b = heatramp # ramp up blue
elif t192 & 0x40:
# we're in the middle third
heatcolor.r = 255 # full red
heatcolor.g = heatramp # ramp up green
heatcolor.b = 0 # no blue
else:
# we're in the coolest third
heatcolor.r = heatramp # ramp up red
heatcolor.g = 0 # no green
heatcolor.b = 0 # no blue
return heatcolor
# Palettes
#
# RGB Palettes map an 8-bit value (0..255) to an RGB color.
#
# You can create any color palette you wish a couple of starters
# are provided: Forest, Clouds, Lava, Ocean, Rainbow, and Rainbow Stripes.
#
# Palettes come in the traditional 256-entry variety, which take
# up 768 bytes of RAM, and lightweight 16-entry varieties. The 16-entry
# variety automatically interpolates between its entries to produce
# a full 256-element color map, but at a cost of only 48 bytes or RAM.
#
# Basic operation is like this: (example shows the 16-entry variety)
# 1. Declare your palette storage:
# CRGBPalette16 myPalette
#
# 2. Fill myPalette with your own 16 colors, or with a preset color scheme.
# You can specify your 16 colors a variety of ways:
# CRGBPalette16 myPalette(
# CRGB::Black,
# CRGB::Black,
# CRGB::Red,
# CRGB::Yellow,
# CRGB::Green,
# CRGB::Blue,
# CRGB::Purple,
# CRGB::Black,
#
# 0x100000,
# 0x200000,
# 0x400000,
# 0x800000,
#
# CHSV( 30,255,255),
# CHSV( 50,255,255),
# CHSV( 70,255,255),
# CHSV( 90,255,255)
# )
#
# Or you can initiaize your palette with a preset color scheme:
# myPalette = RainbowStripesColors_p
#
# 3. Any time you want to set a pixel to a color from your palette, use
# "ColorFromPalette(...)" as shown:
#
# uint8_t index = /* any value 0..255 */
# leds[i] = ColorFromPalette( myPalette, index)
#
# Even though your palette has only 16 explicily defined entries, you
# can use an 'index' from 0..255. The 16 explicit palette entries will
# be spread evenly across the 0..255 range, and the intermedate values
# will be RGB-interpolated between adjacent explicit entries.
#
# It's easier to use than it sounds.
#
class TProgmemRGBPalette16(list):
def __new__(cls, value=None):
if value is None:
value = [0] * 16
elif len(value) < 16:
value += [0] * (16 - len(value))
elif len(value) > 16:
raise ValueError('16 entries are allowed in this array')
return super(TProgmemRGBPalette16, cls).__new__(value)
class TProgmemHSVPalette16(list):
def __new__(cls, value=None):
if value is None:
value = [0] * 16
elif len(value) < 16:
value += [0] * (16 - len(value))
elif len(value) > 16:
raise ValueError('16 entries are allowed in this array')
return super(TProgmemHSVPalette16, cls).__new__(value)
TProgmemPalette16 = TProgmemRGBPalette16
class TProgmemRGBPalette32(list):
def __new__(cls, value=None):
if value is None:
value = [0] * 32
elif len(value) < 32:
value += [0] * (32 - len(value))
elif len(value) > 32:
raise ValueError('32 entries are allowed in this array')
return super(TProgmemRGBPalette32, cls).__new__(value)
class TProgmemHSVPalette32(list):
def __new__(cls, value=None):
if value is None:
value = [0] * 32
elif len(value) < 32:
value += [0] * (32 - len(value))
elif len(value) > 32:
raise ValueError('32 entries are allowed in this array')
return super(TProgmemHSVPalette32, cls).__new__(value)
TProgmemPalette32 = TProgmemRGBPalette32
TProgmemRGBGradientPalette_byte = int
TProgmemRGBGradientPalette_bytes = list
TProgmemRGBGradientPalettePtr = TProgmemRGBGradientPalette_bytes
class TRGBGradientPaletteEntryUnion(object):
def __init__(self, index=0, r=0, g=0, b=0, dword=0, bytes_=None):
if bytes_ is None:
bytes_ = [0] * 4
self.index = index
self.r = r
self.g = g
self.b = b
self.dword = dword
self.bytes = bytes_
TDynamicRGBGradientPalette_byte = int
TDynamicRGBGradientPalette_bytes = list
TDynamicRGBGradientPalettePtr = TDynamicRGBGradientPalette_bytes
'''
'''
# Convert a 16-entry palette to a 256-entry palette
def UpscalePalette(srcpal, destpal):
if isinstance(srcpal, CRGBPalette16) and isinstance(destpal, CRGBPalette256):
for i in range(256):
try:
destpal[i] = ColorFromPalette(srcpal, i)
except IndexError:
break
elif isinstance(srcpal, CHSVPalette16) and isinstance(destpal, CHSVPalette256):
for i in range(256):
try:
destpal[i] = ColorFromPalette(srcpal, i)
except IndexError:
break
elif isinstance(srcpal, CRGBPalette16) and isinstance(destpal, CRGBPalette32):
for i in range(16):
j = i * 2
destpal[j + 0] = srcpal[i]
destpal[j + 1] = srcpal[i]
elif isinstance(srcpal, CRGBPalette32) and isinstance(destpal, CRGBPalette256):
for i in range(256):
try:
destpal[i] = ColorFromPalette(srcpal, i)
except IndexError:
break
elif isinstance(srcpal, CHSVPalette32) and isinstance(destpal, CHSVPalette256):
for i in range(256):
try:
destpal[i] = ColorFromPalette(srcpal, i)
except IndexError:
break
class CHSVPalette16(object):
def __init__(
self,
c00=None,
c01=None,
c02=None,
c03=None,
c04=None,
c05=None,
c06=None,
c07=None,
c08=None,
c09=None,
c10=None,
c11=None,
c12=None,
c13=None,
c14=None,
c15=None,
rhs=None
):
if isinstance(c00, CHSVPalette16):
self.entries = c00.entries[:]
elif isinstance(c00, TProgmemHSVPalette16):
self.entries = []
for i in range(16):
self.entries.append(CHSV(*FL_PGM_READ_DWORD_NEAR(c00[i])))
elif isinstance(rhs, CHSVPalette16):
self.entries = rhs.entries[:]
elif isinstance(rhs, TProgmemHSVPalette16):
self.entries = []
for i in range(16):
self.entries.append(CHSV(*FL_PGM_READ_DWORD_NEAR(rhs[i])))
else:
if c01 is not None and c02 is None:
self.entries = []
for i in range(16):
self.entries += [CHSV()]
fill_solid(self.entries, 16, c01)
elif None not in (c01, c02) and c03 is None:
self.entries = []
for i in range(16):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 16, c01, c02)
elif None not in (c01, c02, c03) and c04 is None:
self.entries = []
for i in range(16):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 16, c01, c02, c03)
elif None not in (c01, c02, c03, c04) and c05 is None:
self.entries = []
for i in range(16):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 16, c01, c02, c03, c04)
else:
if c00 is None:
c00 = CHSV()
if c01 is None:
c01 = CHSV()
if c02 is None:
c02 = CHSV()
if c03 is None:
c03 = CHSV()
if c04 is None:
c04 = CHSV()
if c05 is None:
c05 = CHSV()
if c06 is None:
c06 = CHSV()
if c07 is None:
c07 = CHSV()
if c08 is None:
c08 = CHSV()
if c09 is None:
c09 = CHSV()
if c10 is None:
c10 = CHSV()
if c11 is None:
c11 = CHSV()
if c12 is None:
c12 = CHSV()
if c13 is None:
c13 = CHSV()
if c14 is None:
c14 = CHSV()
if c15 is None:
c15 = CHSV()
self.entries = [
c00,
c01,
c02,
c03,
c04,
c05,
c06,
c07,
c08,
c09,
c10,
c11,
c12,
c13,
c14,
c15
]
def __getitem__(self, x):
return self.entries[x]
def __eq__(self, rhs):
if isinstance(rhs, CHSVPalette16):
for i in range(len(self.entries)):
if self.entries[i] == rhs.entries[i]:
continue
break
else:
return True
return False
def __ne__(self, rhs):
return not self.__eq__(rhs)
class CHSVPalette256(object):
def __init__(
self,
c00=None,
c01=None,
c02=None,
c03=None,
c04=None,
c05=None,
c06=None,
c07=None,
c08=None,
c09=None,
c10=None,
c11=None,
c12=None,
c13=None,
c14=None,
c15=None,
rhs=None,
rhs16=None
):
if isinstance(c00, CHSVPalette256):
self.entries = c00.entries[:]
elif isinstance(c00, CHSVPalette16):
self.entries = []
for i in range(256):
self.entries += [CHSV()]
UpscalePalette(c00, self)
elif isinstance(c00, TProgmemHSVPalette16):
self.entries = []
for i in range(256):
self.entries += [CHSV()]
c00 = CHSVPalette16(c00)
UpscalePalette(c00, self)
elif isinstance(rhs, CHSVPalette256):
self.entries = rhs.entries[:]
elif isinstance(rhs, TProgmemHSVPalette16):
self.entries = []
for i in range(256):
self.entries += [CHSV()]
rhs = CHSVPalette16(rhs)
UpscalePalette(rhs, self)
elif isinstance(rhs16, CHSVPalette16):
self.entries = []
for i in range(256):
self.entries += [CHSV()]
UpscalePalette(rhs16, self)
else:
if c01 is not None and c02 is None:
self.entries = []
for i in range(256):
self.entries += [CHSV()]
fill_solid(self.entries, 16, c01)
elif None not in (c01, c02) and c03 is None:
self.entries = []
for i in range(256):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 256, c01, c02)
elif None not in (c01, c02, c03) and c04 is None:
for i in range(256):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 256, c01, c02, c03)
elif None not in (c01, c02, c03, c04) and c05 is None:
self.entries = []
for i in range(256):
self.entries += [CHSV()]
fill_gradient_HSV(self.entries, 256, c01, c02, c03, c04)
else:
if c00 is None:
c00 = CHSV()
if c01 is None:
c01 = CHSV()
if c02 is None:
c02 = CHSV()
if c03 is None:
c03 = CHSV()
if c04 is None:
c04 = CHSV()
if c05 is None:
c05 = CHSV()
if c06 is None:
c06 = CHSV()
if c07 is None:
c07 = CHSV()
if c08 is None:
c08 = CHSV()
if c09 is None:
c09 = CHSV()
if c10 is None:
c10 = CHSV()
if c11 is None:
c11 = CHSV()
if c12 is None:
c12 = CHSV()
if c13 is None:
c13 = CHSV()
if c14 is None:
c14 = CHSV()
if c15 is None:
c15 = CHSV()
self.entries = [CHSV()] * 256