【发布时间】:2017-01-23 00:57:24
【问题描述】:
我环顾四周,试图找到一种在 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
调用代码:
colorFade(strip, [200, 0, 0], [0, 200, 0])
【问题讨论】:
-
尝试
steps = 200.0- 由于整数除法,您可能错误地计算了 step_R/G/B 变量。 -
很难处理一个不断变化的问题。也许需要一些时间并进行一个编辑?
-
标签: python interpolation rgb lighting