Don’t know if you guys have run across any of the various “Night Light” “Red Shift” “Flux”-type color temperature adjusting settings and apps that are getting added to everything, but I decided it’d be nice to give that ability to the Python scripts I have driving the BlinkStick.
If you’ve never heard of it before, the idea is that blue light interferes with melatonin production making you feel less sleepy and making it harder to get a decent night’s sleep. So you adjust the color temperature to shift things over to the red end of the spectrum, washing the blue out of things.
Had my LEDs going last night and thought “Well what’s the point of having it on my monitors if I’m working on my LEDs that are blasting blue light across the whole room” so I did spent some time with DuckDuckGo and found an algorithm that I translated to Python:
def color_temp(temp):
# Algorithm for color temp taken from http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
temp = temp / 100
if temp <= 66:
r = 255
else:
r = temp - 60
r = 329.698727446 * (r ** -0.1332047592)
r = min(255, max(0, r))
if temp < 66:
g = temp
g = 99.4708025861 * log(g) - 161.1195681661
g = min(255, max(0, g))
else:
g = temp - 60
g = 288.1221695283 * (g ** -0.0755148492)
g = min(255, max(0, g))
if temp >= 65:
b = 255
elif temp < 20:
b = 0
else:
b = temp - 10
b = 138.5177312231 * log(b) - 305.0447927307
b = min(255, max(0, b))
return (r/255, g/255, b/255)
And the way I’m using it is a bit like this:
from blinkstick import blinkstick
from math import sin, pi, cos, log
import time
pulse_speed = 1.0
brightness = 1.0
temperature = 3200
fps = 50
stick = blinkstick.find_first()
count = stick.get_led_count()
tr, tg, tb = color_temp(temperature)
while True:
x = time.time() * pulse_speed
fade = sin( x - sin(x) / 5 ) / 2 + .4
fade = max(0, fade)
data = []
for i in range(count):
falloff = cos( i/(count-1) * 2 * pi - pi) / 2 + .5
value = fade * falloff
r = int(value*tr*brightness*255)
g = int(value*tg*brightness*255)
b = int(value*tb*brightness*255)
data += [g, r, b]
stick.set_led_data(0, data)
time.sleep(1/fps)
And that’ll just create a little white “breathing” effect with whatever color temperature you want. And with this particular algorithm, anywhere from 6559 to 6599 comes out as (1.0, 1.0, 1.0)
so you can use that as a baseline/unchanged white.