在Python中生成颜色范围 [英] Generating color ranges in Python
本文介绍了在Python中生成颜色范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以(r,g,b)元组的形式生成颜色规范的列表,该列表跨越整个色谱,包含尽可能多的条目。所以对于5个条目,我想要的是:
I want to generate a list of color specifications in the form of (r, g, b) tuples, that span the entire color spectrum with as many entries as I want. So for 5 entries I would want something like:
- (0,0,1)
- (0,1,0)
- (1,0,0)
- (1,0.5,1)
- (0,0,0.5)
- (0, 0, 1)
- (0, 1, 0)
- (1, 0, 0)
- (1, 0.5, 1)
- (0, 0, 0.5)
当然,如果有比0和1的组合更多的条目,
Of course, if there are more entries than combination of 0 and 1 it should turn to use fractions, etc. What would be the best way to do this?
推荐答案
使用HSV / HSB / HSL颜色空间(三个名字或多或少相同的东西)。生成N个元组在色调空间中均匀分布,然后将它们转换为RGB。
Use the HSV/HSB/HSL color space (three names for more or less the same thing). Generate N tuples equally spread in hue space, then just convert them to RGB.
示例代码:
import colorsys
N = 5
HSV_tuples = [(x*1.0/N, 0.5, 0.5) for x in range(N)]
RGB_tuples = map(lambda x: colorsys.hsv_to_rgb(*x), HSV_tuples)
这篇关于在Python中生成颜色范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文