Skip to content

Instantly share code, notes, and snippets.

@Pyredrid
Last active August 28, 2016 20:37
Show Gist options
  • Save Pyredrid/39128df29f9078850068029ab6bb8777 to your computer and use it in GitHub Desktop.
Save Pyredrid/39128df29f9078850068029ab6bb8777 to your computer and use it in GitHub Desktop.
A python function to make rainbows with included example using BearLibTerminal
__author__='@pyredrid'
#Takes an angle from 0.0 to 1.0 (exclusive) and returns the
#corresponding color in ARGB from a color wheel with full alpha
def colorFromWheel(angle):
#Ensure that angle is positive
angle = abs(angle)
#Ensure that angle is between 0.0 and 1.0(exclusive)
#Uses repeating instead of clamping to
#allow time.time() rainbow tomfoolery
angle = angle%1.0
if angle >= 0 and angle < 1/6:
#Linear interpolation for ramping color
t = (angle * 6.0) * 255.0
#Ramp up green, maintain red
r = 255
g = t
b = 0
elif angle >= 1/6 and angle < 2/6:
#Backwards linear interpolation for ramping color
t = 255.0 + (((angle - 1/6) * 6.0) * -255.0)
#Ramp down red, maintain green
r = t
g = 255
b = 0
elif angle >= 2/6 and angle < 3/6:
#Linear interpolation for ramping color
t = ((angle - 2/6) * 6.0) * 255.0
#Ramp up blue, maintain green
r = 0
g = 255
b = t
elif angle >= 3/6 and angle < 4/6:
#Backwards linear interpolation for ramping color
t = 255.0 + (((angle - 3/6) * 6.0) * -255.0)
#Ramp down green, maintain blue
r = 0
g = t
b = 255
elif angle >= 4/6 and angle < 5/6:
#Linear interpolation for ramping color
t = ((angle - 4/6) * 6.0) * 255.0
#Ramp up red, maintain blue
r = t
g = 0
b = 255
elif angle >= 5/6 and angle < 1:
#Backwards linear interpolation for ramping color
t = 255.0 + (((angle - 5/6) * 6.0) * -255.0)
#Ramp down blue, maintain red
r = 255
g = 0
b = t
#Bitwise operations to create the ARGB representation
result = (255 << 24) | (int(r) << 16) | (int(g) << 8) | int(b)
return result
__author__='@pyredrid'
import lib.PyBearLibTerminal as term
global keysPressed
global keysDown
global keysReleased
keysPressed = []
keysDown = []
keysReleased = []
#Poll all key events, should be called every frame
def poll():
global keysPressed
global keysDown
global keysReleased
keysDown = keysPressed
keysPressed = []
keysReleased = []
while term.has_input():
key = term.read()
if key & term.TK_KEY_RELEASED == term.TK_KEY_RELEASED:
keysReleased.append(key)
else:
keysPressed.append(key)
#Returns True iff the key is pressed down
def isKeyDown(key):
global keysPressed
global keysDown
if key in keysDown:
return True
if key in keysPressed:
return True
return False
#Returns True iff the key is not pressed down
def isKeyUp(key):
global keysPressed
global keysDown
if key in keysDown:
return False
if key in keysPressed:
return False
return True
#Returns True iff the key has just been pressed in the last poll
def isKeyPressed(key):
global keysPressed
if key in keysPressed:
return True
return False
#Returns True iff the key has just been released in the last poll
def isKeyReleased(key):
global keysReleased
if key in keysReleased:
return True
return False
__author__='@pyredrid'
#Should appear similar to this: http://i.imgur.com/rCM7pFL.gif
import input
import random
import time
import utils.color as colorUtil
import lib.PyBearLibTerminal as term
print('starting...')
term.open()
term.set('window: title="Rainbow Tests", size=64x21')
isRunning = True
while isRunning:
input.poll()
if input.isKeyDown(term.TK_CLOSE) == True:
isRunning = False
t = time.time()
for x in range(0, 64):
for y in range(0, 21):
bg = colorUtil.colorFromWheel((x/64)+(y/21)+(t/2))
fg = colorUtil.colorFromWheel((t/2)+0.5)
term.bkcolor(bg)
term.color(fg)
term.put(x, y, random.choice('0123456789ABCDEF'))
term.refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment