简体   繁体   English

在 python 中生成梯度 colors

[英]Generating gradient colors in python

Given N number of buckets, I would like to generate colors continuously on the rainbow (where n>=2):给定 N 个桶,我想在彩虹上连续生成 colors(其中 n>=2):

在此处输入图像描述

For example, if n=2, the colors would be:例如,如果 n=2,则 colors 将是:

rgb(255,0,0) -->rgb(0,0,255)

If n=3, the colors would be:如果 n=3,则 colors 将是:

rgb(255,0,0) --> rgb(0,255,0) --> rgb(0,0,255)

What would be a good way to generate these color codes?生成这些颜色代码的好方法是什么?

Your rainbow gradient smoothly varies the hue from 0 degrees to 240 degrees (the hue of the pure blue colour).您的彩虹渐变平滑地将色调从 0 度变为 240 度(纯蓝色的色调)。 Therefore it will be easiest to work with eg HSL values rather than RGB values, so you can vary the hue while leaving the saturation and lightness constant.因此,使用例如 HSL 值而不是 RGB 值将是最容易的,因此您可以在保持饱和度和亮度不变的情况下改变色调。

The colorsys library allows conversion between different colour spaces, so you can use this to generate the RGB values along the rainbow gradient as you need. colorsys库允许在不同颜色空间之间进行转换,因此您可以根据需要使用它沿彩虹渐变生成 RGB 值。 The hls_to_rgb function uses float values between 0 and 1, so 2/3 is the hue for blue, and the bright colours should have a lightness of 0.5 and a saturation of 1 . hls_to_rgb function 使用介于 0 和 1 之间的浮点值,因此2/3是蓝色的色调,明亮的颜色应该具有0.5的亮度和1的饱和度。

from colorsys import hls_to_rgb

def rainbow_color_stops(n=10, end=2/3):
    return [ hls_to_rgb(end * i/(n-1), 0.5, 1) for i in range(n) ]

The result is a list of (r,g,b) tuples.结果是(r,g,b)元组的列表。

Here's a little script to do so, using kaya3's suggestion about HSV.这是一个小脚本,使用 kaya3 关于 HSV 的建议。 Was a fun little exercise.是一个有趣的小练习。

import colorsys
import numpy as np
from matplotlib import pyplot as plt


def spectrum(n : int):
    hsv = [(h, 1, 1) for h in np.linspace(0, 240/360, n)]
    rgb = [colorsys.hsv_to_rgb(*tup) for tup in hsv]
    defloat = lambda x: tuple((int(255 * i) for i in x))
    return [defloat(x) for x in rgb]

if __name__ == '__main__':
    n = 100
    rgb = np.array(spectrum(n))
    rgb = rgb.reshape((1, n, 3))
    rgb = np.tile(rgb, (n, 1, 1))
    plt.imshow(rgb)
    plt.show()

Which generates the following figure生成下图在此处输入图像描述

Using the RGB scale, Red starts at (255,0,0) and the middle, Green, is at (0,255,0) and the end, Blue, is at (0,0,255) .使用 RGB 比例,红色从(255,0,0)开始,中间绿色在(0,255,0)处,蓝色结束在(0,0,255)处。 We can move through the scale by starting at (255,0,0) and 'stepping' until we get to the end.我们可以从(255,0,0)开始并“步进”直到结束。

Let's say there are 256*2 steps to get from red to green (red goes from 256->0 and green goes from 0->256) and 256*2 to get from green to blue.假设从红色到绿色有 256*2 步(红色从 256->0 和绿色从 0->256)和 256*2 从绿色到蓝色。 This comes up to 1024 steps.这达到 1024 步。 Now we can divide 1024 by the number of buckets we want, n .现在我们可以将 1024 除以我们想要的桶数n Here's an example:这是一个例子:

def generate_gradient_rgbs(num_buckets):
    rgb_codes = []
    step_size = 1024 / num_buckets
    for step in range(0,num_buckets):
        red = int(max(0, 255 - (step_size*step*0.5))) # step size is half of the step size since both this item goes down and the next one goes up
        blue = int(max(0, 255 - (step_size*0.5*(num_buckets-step-1))))
        green = (255 - red) if red else (255 - blue)
        rgb_codes.append((red, green, blue))
    return rgb_codes
 >>> generate_gradient_rgbs(4) [(255, 0, 0), (127, 128, 0), (0, 128, 127), (0, 0, 255)]

Of course this is a starting point but for a quick-and-dirty way to get rgb's, this is a possible approach.当然,这是一个起点,但对于获得 rgb 的快速而简单的方法,这是一种可能的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM