python - RGB循环python
问题描述
有谁知道如何在 python 中循环遍历 RGB 值?前任。R = 255,G = 255,B = 255
R -= 1 直到 0 然后回升然后让 G 下降等等?
(这是用于 pygame 中的文本颜色)
解决方案
您可以使用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。这样我们就可以通过求和来让数字回到原来的值。
推荐阅读
- java - 使用 Apache Batik 对 SVG 文件进行正确缩进(漂亮的打印)
- c# - 未找到“EntityQueryable < ReportCompetency >”。确保实体类型已添加到模型中
- vba - 表格的vba选择范围
- sql - 重构 Oracle SQL 查询
- html - 内部 A Href 链接在网站上不起作用,而使用同一类的外部链接可以正常工作
- node.js - node V12 的 node-sspi 安装错误
- node.js - Mongoose 获取当前数据,即使它对模型无效
- java - 如何在android中添加带边界的彩色折线
- ios - 任何类似于 Libgdx 的 Swift 2D 框架/引擎?
- r - 仪表板中的串扰过滤器可能性