我应该如何扩展调色板?

问题描述 投票:0回答:1

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

我创建了函数来处理获取颜色的平均值,并考虑尝试通过以某种自动方式混合颜色来扩展定义的调色板,然后通过添加这些混合颜色来扩展调色板,但我不确定如何执行此操作以结构化、合理的方式。我请求有关如何以自动化方式扩展调色板的指导和建议。

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
    )
)
python list colors mean palette
1个回答
0
投票

您可以使用 colorir 来实现。

from colorir import *
pal = Palette.load("spectral")  # Load a categorical palette, a full list can be found in the docs
grad = Grad(pal)  # Object to automatically "mix" the colors

# Now to generate a dynamic list of colors based on the number of inputs:
for i in range(15):
    # Get a bigger list by interpolating the colors if necessary
    if i > len(pal):
        colors = grad.n_colors(i) 
    else:
        colors = pal.colors
    # Use the 'colors' list for your plots
    # plt.hist(..., color=colors)

免责声明:我是开发商

© www.soinside.com 2019 - 2024. All rights reserved.