【发布时间】:2015-11-10 14:52:19
【问题描述】:
我查看了Generating color gradient in Python,但它没有起到作用
我通常为图形着色创建随机 rgb 值,但我想在特定范围之间创建渐变 rgb。在这种情况下,范围为 [0-307],其中 0 是 RGB 中最深的红色,307 是 RGB 中频率最高的紫色
在下面亚瑟王的帮助下,我使用此代码将其从最深的红色变为紫外线,但两者之间没有彩虹色。我正在寻找跨越彩虹颜色的 rgb 十六进制颜色的 n 个值(在本例中为 307)的列表,以便我可以在 matplotlib 中绘制它们
def hex_to_RGB(hex):
''' "#FFFFFF" -> [255,255,255] '''
# Pass 16 to the integer function for change of base
return [int(hex[i:i+2], 16) for i in range(1,6,2)]
def linear_gradient(start_hex, finish_hex="#FFFFFF", n=307):
''' returns a gradient list of (n) colors between
two hex colors. start_hex and finish_hex
should be the full six-digit color string,
inlcuding the number sign ("#FFFFFF") '''
# Starting and ending colors in RGB form
s = hex_to_RGB(start_hex)
f = hex_to_RGB(finish_hex)
# Initilize a list of the output colors with the starting color
RGB_list = [s]
# Calcuate a color at each evenly spaced value of t from 1 to n
for t in range(1, n):
# Interpolate RGB vector for color at the current value of t
curr_vector = [int(s[j] + (float(t)/(n-1))*(f[j]-s[j])) for j in range(3)]
# Add it to our list of output colors
RGB_list.append(curr_vector)
return RGB_list
start = '#660000'
end = '#7f1ae5'.upper()
【问题讨论】:
-
您为什么希望
randint()生成渐变?你知道什么是渐变吗? -
?我是说我不知道如何制作渐变,这就是我生成随机 RGB 值的方式。你只看标题和代码吗?
-
请包含显示尝试创建渐变的代码,指定您期望发生的情况以及实际发生的情况。
标签: python matplotlib graph colors rgb