我周围环顾四周,试图找到一种在Python中将颜色淡入一种颜色的方法。我发现的大多数示例通常都是特定于某个设备或其他语言的翻译不好的。Python - RGB LED颜色褪色
我目前有一段修改过的代码,用于创建呼吸效果,例如从0到255的效果非常好。我现在所设置的颜色是绿色,如from
。闪烁;然后设置to
颜色:
def colorFade(strip, colorFrom, colorTo, wait_ms=20, steps=10):
steps = 200
step_R = int(colorTo[0])/steps
step_G = int(colorTo[1])/steps
step_B = int(colorTo[2])/steps
r = int(colorFrom[0])
g = int(colorFrom[1])
b = int(colorFrom[2])
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
调用代码:
colorFade(strip, [200, 0, 0], [0, 200, 0])
用''steps = 200.0''试一下 - 你可能是由于整数除法错误地计算了step_R/G/B变量。 – jasonharper
很难处理一个不断变化的问题。也许需要一些时间,并做* *编辑? –
当前写入的方式是在函数的第一行分配'steps' arg参数。 –