-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixelset.py
384 lines (300 loc) · 10.6 KB
/
pixelset.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
from . import *
from .colorutils import *
# Represents a set of CRGB led objects. Provides the [] array operator, and works like a normal array in that case.
# This should be kept in sync with the set of functions provided by CRGB as well as functions in colorutils. Note
# that a pixel set is a window into another set of led data, it is not its own set of led data.
class CPixelView(object):
def __init__(self, leds=None, len_=None, _start=None, _end=None, other=None):
if isinstance(other, CPixelView):
self.dir = other.dir
self.len = other.len
self.leds = other.leds
self.end_pos = other.end_pos
elif isinstance(leds, CPixelView):
self.dir = leds.dir
self.len = leds.len
self.leds = leds.leds
self.end_pos = leds.end_pos
elif len_ is not None and _start is not None:
_end = _start
_start = len_
len_ = None
if _start is not None and _end is not None:
if _end - _start < 0:
self.dir = -1
else:
self.dir = 1
self.len = (_end - _start) + self.dir
self.leds = leds[_start:]
self.end_pos = _start + self.len
else:
self.dir = -1 if len_ < 0 else 1
self.len = len_
self.leds = leds
self.end_pos = len_
# Get the size of this set
# @return the size of the set
def size(self):
return abs(self.len)
# Whether or not this set goes backwards
# @return whether or not the set is backwards
def reversed(self):
return self.len < 0
# do these sets point to the same thing (note, this is different from the contents of the set being the same)
def __eq__(self, rhs):
return (
self.leds == rhs.leds and
self.len == rhs.len and
self.dir == rhs.dir
)
# do these sets point to the different things (note, this is different from the contents of the set being the same)
def __ne__(self, rhs):
return not self.__eq__(rhs)
# access a single element in this set, just like an array operator
def __getitem__(self, x):
if isinstance(x, slice):
start = x.start
stop = x.stop
if start is None:
start = 0
if stop is None:
stop = len(self.leds)
return CPixelView(self.leds, start, stop)
if self.dir & 0x80:
return self.leds[-x]
return self.leds[x]
def __neg__(self):
return CPixelView(self.leds, self.len - self.dir, 0)
def dump(self):
pass
# Add the passed in value to r,g, b for all the pixels in this set
def addToRGB(self, inc):
for pixel in self.leds:
pixel += inc
return self
# Add every pixel in the other set to this set
def __iadd__(self, rhs):
if isinstance(rhs, CPixelView):
if rhs.len > self.len:
for i in range(rhs.len):
try:
pixel = self[i]
except IndexError:
break
pixel += rhs[i]
else:
for i in range(self.len):
try:
pixel = rhs[i]
except IndexError:
break
self[i] += pixel
else:
for pixel in self.leds:
pixel += rhs
return self
# Subtract the passed in value from r,g,b for all pixels in this set
def subFromRGB(self, inc):
for pixel in self.leds:
pixel -= inc
return self
# Subtract every pixel in the other set from this set
def __isub__(self, rhs):
if isinstance(rhs, CPixelView):
if rhs.len > self.len:
for i in range(rhs.len):
try:
pixel = self[i]
except IndexError:
break
pixel -= rhs[i]
else:
for i in range(self.len):
try:
pixel = rhs[i]
except IndexError:
break
self[i] -= pixel
else:
for pixel in self.leds:
pixel -= rhs
return self
# Divide every led by the given value
def __idiv__(self, d):
for pixel in self.leds:
pixel //= d
return self
def __imul__(self, d):
for pixel in self.leds:
pixel *= d
return self
def __irshift__(self, d):
for pixel in self.leds:
pixel >>= d
return self
# Scale every led by the given scale
def nscale8_video(self, scaledown):
for pixel in self.leds:
pixel.nscale8_video(scaledown)
return self
# Scale down every led by the given scale
def __imod__(self, scaledown):
for pixel in self.leds:
pixel.nscale8_video(scaledown)
return self
# Fade every led down by the given scale
def fadeLightBy(self, fadefactor):
return self.nscale8_video(255 - fadefactor)
# Scale every led by the given scale
def nscale8(self, scaledown):
if isinstance(scaledown, CPixelView):
if scaledown.len() > self.len():
for i in range(scaledown.len()):
try:
pixel = self[i]
except IndexError:
break
pixel.nscale8(scaledown[i])
else:
for i in range(self.len()):
try:
pixel = scaledown[i]
except IndexError:
break
self[i].nscale8(pixel)
else:
for pixel in self.leds:
pixel.nscale8(scaledown)
return self
# Fade every led down by the given scale
def fadeToBlackBy(self, fade):
return self.nscale8(255 - fade)
def __ior__(self, rhs):
if isinstance(rhs, CPixelView):
if rhs.len > self.len:
for i in range(rhs.len):
try:
pixel = self[i]
except IndexError:
break
pixel |= rhs[i]
else:
for i in range(self.len):
try:
pixel = rhs[i]
except IndexError:
break
self[i] |= pixel
else:
for pixel in self.leds:
pixel |= rhs
return self
def __iand__(self, rhs):
if isinstance(rhs, CPixelView):
if rhs.len > self.len:
for i in range(rhs.len):
try:
pixel = self[i]
except IndexError:
break
pixel &= rhs[i]
else:
for i in range(self.len):
try:
pixel = rhs[i]
except IndexError:
break
self[i] &= pixel
else:
for pixel in self.leds:
pixel &= rhs
return self
# Returns whether or not any leds in this set are non-zero
def __bool__(self):
for pixel in self.leds:
if pixel:
return True
return False
# Color util functions
def fill_solid(self, color):
for pixel in self.leds:
if isinstance(color, CHSV):
pixel.setHSV(color)
else:
pixel.setRGB(color)
return self
def fill_rainbow(self, initialhue, deltahue=5):
if self.dir >= 0:
fill_rainbow(self.leds, self.len, initialhue, deltahue)
else:
fill_rainbow(self.leds[:self.len + 1], -self.len, initialhue, deltahue)
return self
def fill_gradient(
self,
c1,
c2,
c3=None,
c4=None,
directionCode=SHORTEST_HUES
):
if self.dir >= 0:
fill_gradient(self.leds, self.len, c1, c2, c3, c4, directionCode)
else:
fill_gradient(self.leds[:self.len + 1], -self.len, c1, c2, c3, c4, directionCode)
return self
def fill_gradient_RGB(
self,
c1,
c2,
c3=None,
c4=None,
directionCode=SHORTEST_HUES
):
if self.dir >= 0:
fill_gradient_RGB(self.leds, self.len, c1, c2, c3, c4, directionCode)
else:
fill_gradient_RGB(self.leds[:self.len + 1], -self.len, c1, c2, c3, c4, directionCode)
return self
def nblend(self, overlay, amountOfOverlay):
if isinstance(overlay, CPixelView):
if overlay.len() > self.len():
for i in range(overlay.len()):
try:
pixel = self[i]
except IndexError:
break
nblend(pixel, overlay[i], amountOfOverlay)
else:
for i in range(self.len()):
try:
pixel = overlay[i]
except IndexError:
break
nblend(self[i], pixel, amountOfOverlay)
else:
for pixel in self.leds:
nblend(pixel, overlay, amountOfOverlay)
# Note: only bringing in a 1d blur, not sure 2d blur makes sense when looking at sub arrays
def blur1d(self, blur_amount):
if self.dir >= 0:
blur1d(self.leds, self.len, blur_amount)
else:
blur1d(self.leds[self.len + 1], -self.len, blur_amount)
return self
def napplyGamma_video(self, gammaR, gammaG=None, gammaB=None):
if gammaG is None and gammaB is None:
if self.dir >= 0:
napplyGamma_video(self.leds, self.len, gammaR)
else:
napplyGamma_video(self.leds[self.len + 1], -self.len, gammaR)
else:
if self.dir >= 0:
napplyGamma_video(self.leds, self.len, gammaR, gammaG, gammaB)
else:
napplyGamma_video(self.leds[self.len + 1], -self.len, gammaR, gammaG, gammaB)
return self
CRGBSet = CPixelView
class CRGBArray(CRGBSet):
def __init__(self, *args, **kwargs):
super(CRGBArray, self).__init__(*args, **kwargs)
self.rawleds = [pixel for pixel in self.leds]