我到处找遍了,试图找到一种在Python中从一种颜色淡入另一种颜色的方法。我发现的大多数例子通常都是特定于某个设备或另一种无法翻译的语言。在
我现在有一段修改过的代码,用来创建一个呼吸效果,例如从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
主叫代码:
^{pr2}$
听起来你想从
colorFrom
开始,然后逐步地 沿着一条直线直到你到达colorTo
。在这段代码的作用是从
colorFrom
开始,然后增加当前值 将其涂上颜色,就好像你是从黑色到colorTo
。在没有完整的代码很难确定,但看起来你应该这样做 替换此项:
有了这个:
^{pr2}$编辑:而且,正如jasonharper指出的,您可能正在进行整数除法。不清楚你的类型。如果您使用的是python2,
/
是整数除法。在python3中,它是浮点。在相关问题 更多 >
编程相关推荐