我该如何扩展调色板?

时间:2015-08-31 22:41:37

标签: python list colors mean palette

我正在编写一个脚本,将多个直方图组合成一个堆栈图。该脚本能够处理相当数量的直方图。我希望能够根据定义的调色板为叠加图中的直方图着色,并且还能够在不足以为脚本处理的直方图数量着色时扩展该调色板。

我已经创建了处理颜色均值的函数,并考虑通过以某种自动方式混合颜色来扩展定义的调色板,然后通过添加混合颜色来扩展调色板,但我不确定如何以结构化,明智的方式做到这一点。我请求有关如何以自动方式扩展调色板的指导和建议。

style1 = [
    "#FC0000",
    "#FFAE3A",
    "#00AC00",
    "#6665EC",
    "#A9A9A9"
]

def clamp(x): 
    return(max(0, min(x, 255)))

def RGB_to_HEX(RGB_tuple):
    # This function returns a HEX string given an RGB tuple.
    r = RGB_tuple[0]
    g = RGB_tuple[1]
    b = RGB_tuple[2]
    return "#{0:02x}{1:02x}{2:02x}".format(clamp(r), clamp(g), clamp(b))

def HEX_to_RGB(HEX_string):
    # This function returns an RGB tuple given a HEX string.
    HEX = HEX_string.lstrip('#')
    HEX_length = len(HEX)
    return tuple(
        int(HEX[i:i + HEX_length // 3], 16) for i in range(
            0,
            HEX_length,
            HEX_length // 3
        )
    )

def mean_color(colorsInHEX):
    # This function returns a HEX string that represents the mean color of a
    # list of colors represented by HEX strings.
    colorsInRGB = []
    for colorInHEX in colorsInHEX:
        colorsInRGB.append(HEX_to_RGB(colorInHEX))
    sum_r = 0
    sum_g = 0
    sum_b = 0
    for colorInRGB in colorsInRGB:
        sum_r += colorInRGB[0]
        sum_g += colorInRGB[1]
        sum_b += colorInRGB[2]
    mean_r = sum_r / len(colorsInRGB)
    mean_g = sum_g / len(colorsInRGB)
    mean_b = sum_b / len(colorsInRGB)
    return RGB_to_HEX((mean_r, mean_g, mean_b))

def extend_palette(
    colors               = None, # a list of HEX string colors
    numberOfColorsNeeded = None # number of colors to which list should be extended
    )
    # magic happens here
    return colors_extended 

print(
    extend_palette(
        colors = style1,
        10
    )
)

print(
    extend_palette(
        colors = style1,
        50
    )
)

0 个答案:

没有答案