Skip to content

Instantly share code, notes, and snippets.

@neptunius
Last active October 9, 2019 03:14
Show Gist options
  • Select an option

  • Save neptunius/f9c7347cc81320a68e62989fda763c8a to your computer and use it in GitHub Desktop.

Select an option

Save neptunius/f9c7347cc81320a68e62989fda763c8a to your computer and use it in GitHub Desktop.
Rainbow code for Dani's Lite Brite activity – client: make.sc/litebrite-client and server: make.sc/litebrite-repo
from math import sin, cos, radians
import os
import pusher
# Step 1: Change YOUR_NAME to your own name!
YOUR_NAME = 'Rainbow'
# Step 2: Choose a color and assign it to YOUR_COLOR.
# HINT 1: Value should always be a six character hex code with a # in front.
# HINT 2: Find colors that will look good in the demo here: https://github.com/yeun/open-color#available-colors
YOUR_COLOR = 'blue'
MAX_ROW = 33
MAX_COL = 33
# Initialize the Pusher API.
p = pusher.Pusher(
app_id=os.getenv('PUSHER_APP_ID'),
key=os.getenv('PUSHER_APP_KEY'),
secret=os.getenv('PUSHER_APP_SECRET'),
cluster=os.getenv('PUSHER_APP_CLUSTER'))
def light_on(row, column, color=YOUR_COLOR):
"""
Changes a single LED on the game board to the color specified in YOUR_COLOR.
Don't change this function! Do all your work in __main__.
HINT 1: Row and column indexes start at 1 and end at 33.
HINT 2: DO NOT change the names of the keys in the event_data dictionary.
"""
# Data that we'll send to the game board.
event_data = {
'name': YOUR_NAME,
'color': color,
'event': 'led-on',
'row': row + 1,
'column': column + 1
}
# Sending the data through pusher to turn on the LED.
p.trigger(os.getenv('CHANNEL_NAME'), 'led-on', event_data)
# Print a success message on the terminal.
# You should now see your color on the game board!
print(f'LED at ({row}, {column}) is on! Color: {color}')
def hexify(scale):
value = int(scale * 255)
hex_digits = hex(value).split('x')[-1]
# Add leading zero if needed
if len(hex_digits) == 1:
hex_digits = '0' + hex_digits
return hex_digits
def blue_green_cyan_rgb():
for row in range(MAX_ROW):
for col in range(MAX_COL):
green = hexify(row / 32)
blue = hexify(col / 32)
rgb = f'#00{green}{blue}'
light_on(row, col, rgb)
def rainbow_background_hsl():
for row in range(MAX_ROW):
for col in range(MAX_COL):
hue = int(row / 32 * 255)
lightness = int(col / 32 * 50) + 25
hsl = f'HSL({hue}, 100%, {lightness}%)'
light_on(row, col, hsl)
def sky():
for row in range(MAX_ROW):
for col in range(MAX_COL):
hue = 190 + int(40 * (1 - (row / 32)))
row_norm = row / 32
col_offset = abs(col - MAX_COL / 2) / 16
light_norm = (row_norm + (1 - col_offset)) / 2
lightness = 40 + int(40 * light_norm)
hsl = f'HSL({hue}, 100%, {lightness}%)'
light_on(row, col, hsl)
def sun():
hue = 55
lightness = 50
color = f'HSL({hue}, 100%, {lightness}%)'
center_row = 16 # 29
center_col = 16
ray_len = 4
for row in range(center_row - ray_len, center_row + ray_len + 1):
light_on(row, center_col, color)
for col in range(center_col - ray_len, center_col + ray_len + 1):
light_on(center_row, col, color)
for offset in range(-ray_len // 2, ray_len // 2 + 1):
light_on(center_row + offset, center_col + offset, color)
light_on(center_row + offset, center_col - offset, color)
def rainbow():
num_colors = 10
center_row = 16
center_col = 16
max_radius = 16
epsilon = 0.01 # Hack for derpiness at 90 degrees
for color in range(0, num_colors):
hue = 30 * color
hsl = f'HSL({hue}, 100%, 50%)'
if color == 3: continue # Skip yellow-green
if color == 5: continue # Skip cyan-green
if color > 5: color -= 2 # Account for 2 skips
elif color > 3: color -= 1 # Account for skip
radius = max_radius - color
angle_skip = 3 if color < 5 else 6
for degrees in range(180, -1, -angle_skip):
angle = radians(degrees)
left_right = (degrees - 90) // 90
row = center_row - int(radius * sin(angle) - epsilon)
col = center_col + int(radius * cos(angle) + epsilon * left_right)
light_on(row, col, hsl)
if __name__ == '__main__':
sky()
sun()
rainbow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment