代码之家  ›  专栏  ›  技术社区  ›  Sverre Rabbelier

在Python中生成颜色范围

  •  17
  • Sverre Rabbelier  · 技术社区  · 17 年前

    • (0, 0, 1)
    • (1, 0, 0)

    当然,如果条目多于0和1的组合,则应使用分数等。这样做的最佳方式是什么?

    4 回复  |  直到 12 年前
        1
  •  57
  •   kquinn    17 年前

    使用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)
    
        2
  •  25
  •   serv-inc    8 年前

    调色板很有趣。你知道吗,同样的亮度,比如说绿色,比红色更强烈?看看 http://poynton.ca/PDFs/ColorFAQ.pdf . 如果要使用预配置的选项板,请查看 seaborn's palettes :

    import seaborn as sns
    palette = sns.color_palette(None, 3)
    

    从当前调色板生成3种颜色。

        3
  •  10
  •   ceprio    8 年前

    遵循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
    
        4
  •  9
  •   Community Mohan Dere    9 年前

    kquinn's 答复

    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