-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.py
300 lines (242 loc) · 8.9 KB
/
patterns.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
""" Contains the light pattern functions. Each function
takes (phase, cache, kwargs) as arguments so that they
can be used interchangably within the Controller class.
Each function returns a numpy array with the rgb values
for each light. """
import numpy as np
from colors import wheel, shift
import random
from phase import modify_phase
import time
def pixel_train(phase, cache, kwargs, step=2.5, dim_factor=0.92):
shape = kwargs["shape"]
n_pix = shape[0]
n_cycles = kwargs["n_cycles"]
saturation = kwargs["saturation"]
warm_shift = kwargs["warm_shift"]
warm_rgb = kwargs["warm_rgb"]
alt = kwargs["alt"]
# Initialize new cache
if cache.get("mode") != "pixel_train":
cache = {"mode":"pixel_train",
"rgb_values": np.zeros(shape),
"pix_idx": 0,
"wheel_color": 0
}
rgb_values = cache["rgb_values"]
pix_idx = cache["pix_idx"]
wheel_color = cache["wheel_color"]
# Works best at slower speeds
phase, direction = modify_phase(phase, n_cycles, 2)
# Update if moved to next pix
curr_pix = int(n_pix * phase)
if alt:
curr_pix = n_pix - curr_pix - 1
if curr_pix != pix_idx:
wheel_color = int((wheel_color + step) % 255)
rgb = wheel(wheel_color, True, saturation)
rgb_values = rgb_values * dim_factor
if warm_shift:
rgb = shift(rgb, warm_rgb, 1. - saturation)
rgb_values[curr_pix] = rgb
# Save new values in cache
cache["rgb_values"] = rgb_values
cache["pix_idx"] = curr_pix
cache["wheel_color"] = wheel_color
return rgb_values, cache
def pulse(phase, cache, kwargs, floor=0.2, color_step=0.1, color_range=5):
warm_shift = kwargs["warm_shift"]
warm_rgb = kwargs["warm_rgb"]
shape = kwargs["shape"]
saturation = kwargs["saturation"]
n_pix = shape[0]
alt = kwargs["alt"]
# Initialize new cache
if cache.get("mode") != "pulse":
cache = {"mode": "pulse",
"wheel_idx": 0,
"old_alt": alt}
if alt:
cache["offset"] = np.random.randn(n_pix)
else:
cache["offset"] = np.zeros(n_pix)
cache["color_offset"] = np.random.randint(-color_range, color_range, size=(n_pix, 3))
color_offset = cache["color_offset"]
wheel_idx = (cache["wheel_idx"] + color_step) % 256
old_alt = cache["old_alt"]
if alt != old_alt:
if alt:
cache["offset"] = np.random.randn(n_pix)
else:
cache["offset"] = np.zeros(n_pix)
offset = cache["offset"]
# Phase in radians, shifted so it
# starts at the peak
phase_rad = 2 * np.pi * np.expand_dims(offset + phase, 1)
phase_rad += 0.5 * np.pi
sin_phase = np.sin(phase_rad)
sin_squashed = (sin_phase + 1) / 2
sin_floored = floor + ((1-floor) * sin_squashed)
rgb = wheel(int(wheel_idx), True, saturation)
if warm_shift:
rgb = shift(rgb, warm_rgb, 1. - saturation)
rgb_values = np.zeros(shape)
rgb_values[:] = rgb
rgb_values += color_offset
rgb_values = np.maximum(rgb_values, 0)
rgb_values = np.minimum(rgb_values, 255)
rgb_values = rgb_values * sin_floored
cache["wheel_idx"] = wheel_idx
cache["old_alt"] = alt
cache["color_offset"] = color_offset
return rgb_values, cache
def droplets(phase, cache, kwargs):
# Get var from kwargs
curr_cycle = kwargs["n_cycles"]
shape = kwargs["shape"]
n_pix = shape[0]
saturation = kwargs["saturation"]
warm_shift = kwargs["warm_shift"]
warm_rgb = kwargs["warm_rgb"]
alt = kwargs["alt"]
radius = int((n_pix / 2) - 1)
# Initialize new cache
if cache.get("mode") != "droplets":
cache = {"mode": "droplets",
"last_cycle":0,
"center_pix": random.randint(radius, n_pix - radius - 1),
"rgb": wheel(random.randint(0, 255), True, saturation),
"last_phase":0.}
center_pix = cache["center_pix"]
last_cycle = cache["last_cycle"]
last_phase = cache["last_phase"]
rgb = cache["rgb"]
rgb_values = np.zeros(shape)
# So that drops are biggest at beginning of phase
phase = (phase + 0.5) % 1
# In alt mode, color changes when droplet is at largest
if alt:
change_threshold = 0.5
if last_phase < change_threshold and phase > change_threshold:
rgb = wheel(random.randint(0, 255), True, saturation)
# In regular mode, color changes when droplet is at smallest
else:
leap = 0.9
if abs(last_phase - phase) > 0.9:
rgb = wheel(random.randint(0, 255), True, saturation)
if curr_cycle != last_cycle:
center_pix = random.randint(radius, n_pix - radius - 1)
drop_shape = calculate_drop(phase, radius)
d_start = center_pix - radius
d_end = center_pix + radius
if warm_shift:
final_rgb = shift(rgb, warm_rgb, 1. - saturation)
else:
final_rgb = rgb
rgb_values[d_start: d_end + 1] = final_rgb
rgb_values[d_start: d_end + 1] = rgb_values[d_start: d_end + 1] * drop_shape
cache["rgb"] = rgb
cache["last_cycle"] = curr_cycle
cache["last_phase"] = phase
cache["center_pix"] = center_pix
return rgb_values, cache
def calculate_drop(phase, radius, transform="linear"):
if transform == "sin":
phase_rads = phase * 2 * np.pi
phase_rads -= (np.pi / 4) # so that pixels start off
phase = (np.sin(phase_rads) + 1) / 2
elif transform == "linear":
phase = -np.abs(phase * 2 - 1) + 1
diam = 2 * radius + 1
pixels_offset = -np.abs(np.linspace(1, -1, 2 * radius + 1))
pixels_phased = np.maximum(pixels_offset + phase, 0)
return np.expand_dims(pixels_phased, 1)
def orbits(phase, cache, kwargs, dim_factor=0.8, margin=70):
shape = kwargs["shape"]
n_pix = shape[0]
n_cycles = kwargs["n_cycles"]
saturation = kwargs["saturation"]
warm_shift = kwargs["warm_shift"]
warm_rgb = kwargs["warm_rgb"]
alt = kwargs["alt"]
# Initialize new cache
if cache.get("mode") != "orbits":
cache = {"mode":"orbits",
"rgb_values": np.zeros(shape),
"pix_1": n_pix / 2
}
color_1_int = random.randint(0, 255)
color_2_int = random.randint(0, 255)
while abs(color_1_int - color_2_int) < margin:
color_2_int = random.randint(0, 255)
cache["color_1"] = wheel(color_1_int, True)
cache["color_2"] = wheel(color_2_int, True)
rgb_values = cache["rgb_values"]
pix_1 = cache["pix_1"]
color_1 = cache["color_1"]
color_2 = cache["color_2"]
desaturate = 255 - (saturation * 255.)
color_1_sat = np.maximum(color_1, desaturate)
color_2_sat = np.maximum(color_2, desaturate)
# Update if moved to next pix
curr_pix_1 = int((n_pix * phase) + (n_pix / 2))
curr_pix_1 = curr_pix_1 % n_pix
if not alt:
curr_pix_2 = n_pix - curr_pix_1 - 1
else:
curr_pix_2 = int((curr_pix_1 + (n_pix / 2))) % n_pix
if curr_pix_1 != pix_1:
# Update pix_1
rgb_1 = color_1_sat
if warm_shift:
rgb_1 = shift(rgb_1, warm_rgb, 1. - saturation)
rgb_values[curr_pix_1] = rgb_1
# Update pix_2
rgb_2 = color_2_sat
if warm_shift:
rgb_2 = shift(rgb_2, warm_rgb, 1. - saturation)
rgb_values[curr_pix_2] = rgb_2
rgb_values = rgb_values * dim_factor
# Save new values in cache
cache["rgb_values"] = rgb_values
cache["pix_1"] = curr_pix_1
cache["color_1"] = color_1
cache["color_2"] = color_2
return rgb_values, cache
def sparks(phase, cache, kwargs, active_fraction=0.5, wait_factor=0.4):
shape = kwargs["shape"]
n_pix = shape[0]
n_cycles = kwargs["n_cycles"]
saturation = kwargs["saturation"]
warm_shift = kwargs["warm_shift"]
warm_rgb = kwargs["warm_rgb"]
loop_start = kwargs["loop_start"]
# Initialize new cache
if cache.get("mode") != "sparks":
cache = {"mode":"sparks",
"wait": None,
"wait_start": None}
wait = cache["wait"]
wait_start = cache["wait_start"]
rgb_values = np.zeros(shape)
desaturate = 255 - (saturation * 255.)
# If in wait mode
if wait is not None and wait_start is not None:
elapsed = (loop_start - wait_start) * 1000
if elapsed < wait:
return rgb_values, cache
else:
wait = None
wait_start = None
n_choices = int(n_pix * active_fraction)
for i in range(n_choices):
dim = random.random()
curr_rgb = wheel(random.randint(0, 255), True, saturation)
if warm_shift:
curr_rgb = shift(curr_rgb, warm_rgb, 1. - saturation)
rgb_values[random.randint(0, n_pix-1)] = curr_rgb
wait_start = time.time()
wait = random.random() * wait_factor * 1000
cache["wait"] = wait
cache["wait_start"] = wait_start
return rgb_values, cache