def _knight_rider_animation(self): n = len(self._lights) while True: t = time.time() f = self.happiness_freq x0 = math.sin(2.0*math.pi*f*t) x1 = math.sin(2.0*math.pi*f*t - math.pi*(1/n)) i0 = int(utils.lerp(x0, -1.0, 1.0, 0, n)) i1 = int(utils.lerp(x1, -1.0, 1.0, 0, n)) for i in range(n): if i == i0: yield color.hsv_to_rgb(self.hue, 1.0, self.brightness_hsv) elif i == i1: yield color.hsv_to_rgb(self.hue, 1.0, self.brightness_hsv/2) else: yield 0
def rgb(self, value): if is_rgb_tuple(value): self._rgb = value elif is_hsv_tuple(value): self._rgb = hsv_to_rgb(value) elif isinstance(value, Color): self._rgb = value.rgb else: self._rgb = self._rgb
def _pulse_animation(): while True: n = len(self._lights) t = time.time() x = math.sin(2.0*math.pi*freq_hz*t) max_val = self.brightness_hsv min_val = 0.75 * max_val value = utils.lerp(x, -1.0, 1.0, max_val, min_val) for i in range(n): yield color.hsv_to_rgb(hue, 1.0, value)
def _idle_animation(self): n = len(self._lights) while True: t = time.time() max_val = self.brightness_hsv min_val = 0.5 * max_val for i in range(n): phase = i/(n-1)*2.0*math.pi x = math.sin(2.0*math.pi*self.happiness_freq*t + phase) value = utils.lerp(x, -1.0, 1.0, max_val, min_val) yield color.hsv_to_rgb(self.hue, 1.0, value)
def _sparkle_animation(self): n = len(self._lights) phases = [random.uniform(0, 2.0*math.pi) for _ in range(n)] f = self.happiness_freq frequencies = [random.uniform(f/2.0, 2.0*f) for _ in range(n)] while True: t = time.time() for i in range(n): x = math.sin(2.0*math.pi*frequencies[i]*t + phases[i]) hue = utils.lerp(x, -1.0, 1.0, 0.0, 360.0) x = math.sin(2.0*math.pi*frequencies[-(i+1)]*t + phases[-(i+1)]) value = utils.lerp(x, -1.0, 1.0, 0, self.brightness_hsv) yield color.hsv_to_rgb(hue, 1.0, value)
def set(self, pixel, color): universe, index = pixel.get_universe_index() try: _ = self.leds[universe] except KeyError: raise IndexError("Channel {} does not exists".format(universe)) # Dim hsv and convert hsv to rgb if self.brightness < 1: color = color_func.dim_color(color, amount=self.brightness) for i, c in enumerate(color_func.hsv_to_rgb(color)): try: self.leds[universe][index + i] = c except IndexError: raise IndexError( "Universe: {}, Index: {} does not exist".format( universe, index))
def aco_decode(code, w, x, y, z): row = {} if code == 0: #RGB r, g, b = int(w // 256), int(x // 256), int(y // 256) elif code == 1: #HSV r, g, b = hsv_to_rgb(w / 65535, x / 65535, y / 65535) elif code == 2: #CMYK r, g, b = cmyk_to_rgb(w / 65535, x / 65535, y / 65535, z / 65535) elif code == 7: #Lab r, g, b = lab_to_rgb(w / 100, x / 100, y / 100) elif code == 8: #Grayscale r, g, b = int(w / 39.0625), int(w / 39.0625), int(w / 39.0625) elif code == 9: #Wide CMYK r, g, b = cmyk_to_rgb(w / 10000, x / 10000, y / 10000, z / 10000) else: r, g, b = 0, 0, 0 row['hex'] = rgb_to_hex(r, g, b) return row
def _spectrum_animation(self): n = len(self._lights) while True: # Run a FFT on the incoming audio to break it into frequency # buckets. Interpolate those as the intensity of hues across the # pixels. audio = self._microphone.last_read if audio is None or len(audio) < 4*n: continue audio = np.frombuffer(audio[:4*n], dtype='int16') freqs = 10*np.log10(np.abs(np.fft.rfft(audio))) if len(freqs) < (n+1): continue max_power = 30.0 #TODO: Auto tune this value? max_value = self.brightness_hsv for i in range(n): hue = utils.lerp(i, 0, n, 0.0, 360.0) value = utils.lerp(freqs[i+1], 0, max_power, 0, max_value) value = utils.clamp(value, 0, max_value) yield color.hsv_to_rgb(hue, 1.0, value)
def plot_grid(xs, ys, vals, title, xlabel="", ylabel=""): fig, ax = plt.subplots() d = 5 xmin = min(0, min(xs)) - d xmax = max(100, max(xs)) + d ymin = min(0, min(ys)) - d ymax = max(100, max(ys)) + d print(int(xmin), int(xmax), int(ymin), int(ymax)) ax.set_xlim(xmin=xmin, xmax=xmax) ax.set_ylim(ymin=ymin, ymax=ymax) smallest_val = min(vals) biggest_val = max(vals) value_range = biggest_val - smallest_val smallest_circle = 0.1 biggest_circle = 2 circle_range = value_range color_range = value_range start_color = color.rgb_to_hsv(0, 1, 0) end_color = color.rgb_to_hsv(1, 0, 0) for (x, y, val) in zip(xs, ys, vals): current_color = color.hsv_to_rgb( *color.lerp3(start_color, end_color, val - smallest_val, color_range)) current_radius = smallest_circle + ( val - smallest_val) * biggest_circle / value_range ax.add_artist(plt.Circle((x, y), current_radius, color=current_color)) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title)
def rainbow_test(fps=60, duration=180): print("rainbow_test") _c = count(duration * fps) rainbow = [(hsv_to_rgb(((i / 1000), 1, 10 / 10.0))) for i in range(1000)] pixels = rainbow[0:numLEDs]
def test_hsv_to_rgb_zero_value(self): val = color.hsv_to_rgb(0.0, 1.0, 0.0) self.assertEqual(val, 0)
def test_hsv_to_rgb(self): val = color.hsv_to_rgb(0.0, 1.0, 1.0) self.assertEqual(val, 0xFF0000)
green=4 / 12.0, cyan=6 / 12.0, blue=8 / 12.0, magenta=10 / 12.0) pixels = [] c = count() fps = 30 beat = next(c) bps = 129 / 120.0 bong = 0.5 while True: beat = next(c) print if beat > (fps / bps): bong = 1.0 pixels = [ hsv_to_rgb(((beat % 90) / 100.0, 1.0, bong)) for i in range(numLEDs) ] client.put_pixels(pixels) bong = 0.5 c = count() beat = next(c) else: pixels = [ hsv_to_rgb(((beat % 900) / 1000.0, 1.0, bong)) for i in range(0, 900, 10) ] client.put_pixels(pixels) sleep(fps / 6000.0)