我有一个Python模块,提供调色板和实用程序来处理它们。调色板对象只是继承自list
,只是HEX字符串中指定的颜色列表。调色板对象具有扩展自身的能力,以根据需要提供尽可能多的颜色。想象一下,图表中包含许多不同的数据集:可以要求调色板将其所具有的颜色数量扩展到为每个图形数据集提供唯一颜色所需的范围。它通过简单地取相邻颜色的平均值并插入这种新的平均颜色来实现这一点。
extend_palette
函数有效,但它不会均匀地扩展调色板。例如,调色板可能如下所示:
将其扩展为15种颜色仍然可用:
将其扩展为30种颜色会使扩展算法出现问题;新颜色仅添加到颜色列表的一端:
如何更改模块的函数extend_palette
以使扩展的新颜色在调色板中更均匀地分布?
代码如下(函数extend_palette
特别关注,其他代码位用于实验):
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(colors_in_HEX):
# This function returns a HEX string that represents the mean color of a
# list of colors represented by HEX strings.
colors_in_RGB = []
for color_in_HEX in colors_in_HEX:
colors_in_RGB.append(HEX_to_RGB(color_in_HEX))
sum_r = 0
sum_g = 0
sum_b = 0
for color_in_RGB in colors_in_RGB:
sum_r += color_in_RGB[0]
sum_g += color_in_RGB[1]
sum_b += color_in_RGB[2]
mean_r = sum_r / len(colors_in_RGB)
mean_g = sum_g / len(colors_in_RGB)
mean_b = sum_b / len(colors_in_RGB)
return RGB_to_HEX((mean_r, mean_g, mean_b))
class Palette(list):
def __init__(
self,
name = None, # string name
description = None, # string description
colors = None, # list of colors
*args
):
super(Palette, self).__init__(*args)
self._name = name
self._description = description
self.extend(colors)
def name(
self
):
return self._name
def set_name(
self,
name = None
):
self._name = name
def description(
self
):
return self._description
def set_description(
self,
description = None
):
self._description = description
def extend_palette(
self,
minimum_number_of_colors_needed = 15
):
colors = extend_palette(
colors = self,
minimum_number_of_colors_needed = minimum_number_of_colors_needed
)
self = colors
def save_image_of_palette(
self,
filename = "palette.png"
):
save_image_of_palette(
colors = self,
filename = filename
)
def extend_palette(
colors = None, # list of HEX string colors
minimum_number_of_colors_needed = 15
):
while len(colors) < minimum_number_of_colors_needed:
for index in range(1, len(colors), 2):
colors.insert(index, mean_color([colors[index - 1], colors[index]]))
return colors
def save_image_of_palette(
colors = None, # list of HEX string colors
filename = "palette.png"
):
import numpy
import Image
scale_x = 200
scale_y = 124
data = numpy.zeros((1, len(colors), 3), dtype = numpy.uint8)
index = -1
for color in colors:
index += 1
color_RGB = HEX_to_RGB(color)
data[0, index] = [color_RGB[0], color_RGB[1], color_RGB[2]]
data = numpy.repeat(data, scale_x, axis=0)
data = numpy.repeat(data, scale_y, axis=1)
image = Image.fromarray(data)
image.save(filename)
# Define color palettes.
palettes = []
palettes.append(Palette(
name = "palette1",
description = "primary colors for white background",
colors = [
"#fc0000",
"#ffae3a",
"#00ac00",
"#6665ec",
"#a9a9a9",
]
))
palettes.append(Palette(
name = "palette2",
description = "ATLAS clarity",
colors = [
"#FEFEFE",
"#AACCFF",
"#649800",
"#9A33CC",
"#EE2200",
]
))
def save_images_of_palettes():
for index, palette in enumerate(palettes):
save_image_of_palette(
colors = palette,
filename = "palette_{index}.png".format(index = index + 1)
)
def access_palette(
name = "palette1"
):
for palette in palettes:
if palette.name() == name:
return palette
return None
答案 0 :(得分:3)
如果您从简化的示例开始,我认为您遇到的问题更容易理解:
nums = [1, 100]
def extend_nums(nums, min_needed):
while len(nums) < min_needed:
for index in range(1, len(nums), 2):
nums.insert(index, mean(nums[index - 1], nums[index]))
return nums
def mean(x, y):
return (x + y) / 2
我已经复制了你的代码,但是使用了数字而不是颜色来简化操作。以下是我运行时会发生什么:
>>> nums = [0, 100]
>>> extend_nums(nums, 5)
[0, 12.5, 25.0, 37.5, 50.0, 100]
我们在这里有什么?
奇怪,不是吗?好吧,不,我在原地修改nums
。 index
- 循环中for
的含义随着我在nums[3]
之前和之后插入新项目而发生变化:nums.insert(1, something)
。
让我们尝试在每次迭代时创建一个新列表:
def extend_nums(nums, min_needed):
while len(nums) < min_needed:
new_nums = [] # This new list will hold the extended nums.
for index in range(1, len(nums)):
new_nums.append(nums[index - 1])
new_nums.append(mean(nums[index - 1], nums[index]))
new_nums.append(nums[-1])
nums = new_nums
return nums
试试吧:
>>> nums = [0, 100]
>>> extend_nums(nums, 5)
[0, 25.0, 50.0, 75.0, 100]
这个解决方案有效(还有改进的余地)。为什么?因为在我们新的for
- 循环中,index
具有正确的含义。以前,我们在不移动index
的情况下插入项目。
答案 1 :(得分:1)
此代码
while len(colors) < minimum_number_of_colors_needed:
for index in range(1, len(colors), 2):
colors.insert(index, mean_color([colors[index - 1], colors[index]]))
不均匀分布均值。您可以通过运行来查看效果:
colors = range(5)
while len(colors) < 15:
for index in range(1, len(colors), 2):
colors.insert(index, 99)
print(colors)
产生
[0, 99, 99, 99, 99, 99, 99, 99, 1, 99, 99, 99, 2, 3, 4]
由99&表示的太多手段放在开头附近,没有一个靠近末尾。
很高兴,因为你有numpy,你可以使用np.interp
均匀地插入颜色。
例如,如果你有一个带有数据点(0,10),(0.5,20),(1,30)的函数,那么你可以在x = [0,0.33,0.67,1]处插值来找到相应的y值:
In [80]: np.interp([0, 0.33, 0.67, 1], [0, 0.5, 1], [10, 20, 30])
Out[80]: array([ 10. , 16.6, 23.4, 30. ])
由于np.interp
仅对1D阵列进行操作,我们可以将它分别应用于每个RGB通道:
[np.interp(np.linspace(0,1,min_colors), np.linspace(0,1,ncolors), self.rgb[:,i])
for i in range(nchannels)])
例如,
import numpy as np
import Image
def RGB_to_HEX(RGB_tuple):
"""
Return a HEX string given an RGB tuple.
"""
return "#{0:02x}{1:02x}{2:02x}".format(*np.clip(RGB_tuple, 0, 255))
def HEX_to_RGB(HEX_string):
"""
Return 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 ))
class Palette(object):
def __init__(self, name=None, description=None, colors=None, *args):
super(Palette, self).__init__(*args)
self.name = name
self.description = description
self.rgb = np.array(colors)
@classmethod
def from_hex(cls, name=None, description=None, colors=None, *args):
colors = np.array([HEX_to_RGB(c) for c in colors])
return cls(name, description, colors, *args)
def to_hex(self):
return [RGB_to_HEX(color) for color in self.rgb]
def extend_palette(self, min_colors=15):
ncolors, nchannels = self.rgb.shape
if ncolors >= min_colors:
return self.rgb
return np.column_stack(
[np.interp(
np.linspace(0,1,min_colors), np.linspace(0,1,ncolors), self.rgb[:,i])
for i in range(nchannels)])
def save_image_of_palette(rgb, filename="palette.png"):
scale_x = 200
scale_y = 124
data = (np.kron(rgb[np.newaxis,...], np.ones((scale_x, scale_y, 1)))
.astype(np.uint8))
image = Image.fromarray(data)
image.save(filename)
# Define color palettes.
palettes = []
palettes.append(Palette.from_hex(
name="palette1",
description="primary colors for white background",
colors=[
"#fc0000",
"#ffae3a",
"#00ac00",
"#6665ec",
"#a9a9a9", ]))
palettes.append(Palette.from_hex(
name="palette2",
description="ATLAS clarity",
colors=[
"#FEFEFE",
"#AACCFF",
"#649800",
"#9A33CC",
"#EE2200",]))
palettes = {p.name:p for p in palettes}
p = palettes['palette1']
save_image_of_palette(p.extend_palette(), '/tmp/out.png')
请注意,您可能会发现在HSV颜色空间(而不是RGB颜色空间)中进行插值会得到better results。