我想以(r,g,b)元组的形式生成一个颜色规范列表,它跨越整个色谱,包含我想要的多个条目.所以对于5个条目我想要的东西像:
当然,如果有比0和1的组合更多的条目,它应该转向使用分数等.最好的方法是什么?
kqu*_*inn 44
使用HSV/HSB/HSL颜色空间(三个名称或多或少相同).生成在色调空间中均匀分布的N个元组,然后将它们转换为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)
Run Code Online (Sandbox Code Playgroud)
我根据kquinn的答案创建了以下函数.
import colorsys
def get_N_HexCol(N=5):
HSV_tuples = [(x*1.0/N, 0.5, 0.5) for x in xrange(N)]
hex_out = []
for rgb in HSV_tuples:
rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
hex_out.append("".join(map(lambda x: chr(x).encode('hex'),rgb)))
return hex_out
Run Code Online (Sandbox Code Playgroud)
遵循kquinn和jhrf的步骤:)
对于Python 3,可以通过以下方式完成:
def get_N_HexCol(N=5):
HSV_tuples = [(x * 1.0 / N, 0.5, 0.5) for x in range(N)]
hex_out = []
for rgb in HSV_tuples:
rgb = map(lambda x: int(x * 255), colorsys.hsv_to_rgb(*rgb))
hex_out.append('#%02x%02x%02x' % tuple(rgb))
return hex_out
Run Code Online (Sandbox Code Playgroud)
调色板很有趣。您是否知道,与绿色相比,与绿色相同的亮度要强于红色?看看http://poynton.ca/PDFs/ColorFAQ.pdf。如果您想使用预配置的调色板,请查看seaborn的调色板:
import seaborn as sns
palette = sns.color_palette(None, 3)
Run Code Online (Sandbox Code Playgroud)
从当前调色板生成3种颜色。