2016-11-13 13 views
0

我写的neopixel库中的呼吸效果,需要基本上增加三个数字来创造一组期间ColorPython的 - 循环从黑色到一组彩色

我可以做到这一点很容易地与黑到白:

def colorBreathing(strip, color, wait_ms = 5): 
# Breathing lights 
for b in range(200): 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, Color(b, b, b)) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 
for b in reversed(range(200)): 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, Color(b, b, b)) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 

的问题是,我会怎么做这与另一种颜色,用户组?因此,一组颜色可以是这样的Color(200, 180, 230)例如。

+0

如果有'步骤= 200'然后用'step_R = 200/steps','step_G = 180/steps','step_B = 230/steps'然后'颜色(INT(step_R * b),INT(step_G * b),INT(step_B * b))' – furas

回答

1

如果你有steps = 200(200, 180, 230)然后用

step_R = 200/steps 
step_G = 180/steps 
step_B = 230/steps 

然后Color(int(step_R*b), int(step_G*b), int(step_B*b))

未测试:

destination = (200, 180, 230) 

steps = 200 

step_R = destination[0]/steps 
step_G = destination[1]/steps 
step_B = destination[2]/steps 

for x in range(steps): 
    r = int(step_R*x) 
    g = int(step_G*x) 
    b = int(step_B*x) 
    c = Color(r, g, b) 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, c) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 

for x in range(steps, -1, -1): 
    r = int(step_R*x) 
    g = int(step_G*x) 
    b = int(step_B*x) 
    c = Color(r, g, b) 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, c) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 

可以使用r += step_R,而不是r = int(step_R * x)

未测试:

destination = (200, 180, 230) 

steps = 200 

step_R = destination[0]/steps 
step_G = destination[1]/steps 
step_B = destination[2]/steps 

r = 0 
g = 0 
b = 0 

for x in range(steps): 
    c = Color(int(r), int(g), int(b)) 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, c) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 
    r += step_R 
    g += step_G 
    b += step_B 

for x in range(steps): 
    r -= step_R 
    g -= step_G 
    b -= step_B 
    c = Color(int(r), int(g), int(b)) 
    for i in range(strip.numPixels()): 
     strip.setPixelColor(i, c) 
    strip.show() 
    time.sleep(wait_ms/1000.0) 
+0

此作品谢谢!我使用的是第二种方法。我不得不降低步骤100这似乎与每一种颜色我用它来工作。 – Timmo