Python RGB LED褪色

2024-09-29 23:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处找遍了,试图找到一种在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}$

Tags: 方法代码infor颜色steprangesteps
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:55

听起来你想从colorFrom开始,然后逐步地 沿着一条直线直到你到达colorTo。在

这段代码的作用是从colorFrom开始,然后增加当前值 将其涂上颜色,就好像你是从黑色colorTo。在

没有完整的代码很难确定,但看起来你应该这样做 替换此项:

step_R = int(colorTo[0]) / steps
step_G = int(colorTo[1]) / steps
step_B = int(colorTo[2]) / steps

有了这个:

^{pr2}$

编辑:而且,正如jasonharper指出的,您可能正在进行整数除法。不清楚你的类型。如果您使用的是python2,/是整数除法。在python3中,它是浮点。在

相关问题 更多 >

    热门问题