【问题标题】:how to create an rgb rainbow gradient in Python 2.7?如何在 Python 2.7 中创建 rgb 彩虹渐变?
【发布时间】: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


【解决方案1】:

找到答案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 之间。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 2016-09-14
    • 2013-11-11
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2021-12-28
    相关资源
    最近更新 更多