如何在 Python 2.7 中创建 rgb 彩虹渐变?
how to create an rgb rainbow gradient in Python 2.7?
我查看了 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()
找到答案 colorsys
http://python3.codes/fractal-tree/
货品在这里:
# https://docs.python.org/2/library/colorsys.html
(r, g, b) = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
R, G, B = int(255 * r), int(255 * g), int(255 * b)
参数 hue
必须是从 0.0 到 1.0。
我查看了 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()
找到答案 colorsys
http://python3.codes/fractal-tree/
货品在这里:
# https://docs.python.org/2/library/colorsys.html
(r, g, b) = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
R, G, B = int(255 * r), int(255 * g), int(255 * b)
参数 hue
必须是从 0.0 到 1.0。