首页 > 解决方案 > RGB循环python

问题描述

有谁知道如何在 python 中循环遍历 RGB 值?前任。R = 255,G = 255,B = 255

R -= 1 直到 0 然后回升然后让 G 下降等等?

(这是用于 pygame 中的文本颜色)

标签: pythonpygamergb

解决方案


您可以使用max()获取元组中的最大数字并减去每个数字,直到所有数字都等于或小于零,如下所示:

RGB = (255,200,201)

red = RGB[0]
green = RGB[1]
blue = RGB[2]

step = -1

for i in range( max(RGB) * 2 + 2):

    print(
        "R:",red if red > 0 else 0,
        "G:",green if green > 0 else 0,
        "B:",blue if blue > 0 else 0
        )

    if all( map( lambda x: True if x < 0 else False, [red,green,blue] ) ):
        step = 1

    red += step
    green += step
    blue += step

可以看到,在所有数字都等于或小于零之后,变量“step”被设置为1。这样我们就可以通过求和来让数字回到原来的值。


推荐阅读