简体   繁体   English

在HSV中生成两种颜色之间的颜色列表

[英]Generate color list between two colors in HSV

Lets say i have two RGB colors (255,0,0) and (0,0,255), I also require 100 colors that are between them. 可以说我有两种RGB颜色(255,0,0)和(0,0,255),我还需要介于它们之间的100种颜色。 How would i generate a list of HSV color values based on a number of required steps? 如何根据一些所需步骤生成HSV颜色值列表?

Currently using python 3 i created this code: 当前使用python 3我创建了以下代码:

def lerpcolourhsv(rgb1, rgb2, steps):
#convert rgb to hsv
hsv1 = colorsys.rgb_to_hsv(rgb1[0], rgb1[1], rgb1[2])
hsv2 = colorsys.rgb_to_hsv(rgb2[0], rgb2[1], rgb2[2])

# calculate distance between two hue numbers, divide by number of steps. then loop to generate
rgb_colour_list = []
dist = cachefunc.numdistance(hsv1[0], hsv2[0])
step_value = dist / steps
hue = hsv1[0]

for x in range(steps):
    hsv_tupel = (hue, 1, 1)
    hue += step_value
    rgb = colorsys.hsv_to_rgb(hsv_tupel[0], hsv_tupel[1], hsv_tupel[2])
    rgb = (round(rgb[0] * 255), round(rgb[1] * 255), round(rgb[2] * 255), round(255)) #0-1 to 0-255
    rgb_colour_list.append(rgb)

return rgb_colour_list

The only problem with the code above is that it loops through multiple different colors for example: red -> orange -> yellow -> green -> cyan -> blue 上面代码的唯一问题是,它会循环显示多种不同的颜色,例如:红色->橙色->黄色->绿色->青色->蓝色

I would like a function that instead produces results more similar to this: 我想要一个函数,它产生的结果与此类似: 红色到蓝色渐变

As can be seen, this gradient produces a more expected result of red -> purple -> blue 可以看出,此渐变产生了红色->紫色->蓝色的更期望的结果

I tried making a function that interpolates using individual rgb channel values, this gave a more similar result however the perceived brightness and saturation of the colors were off so i am trying to do this in hsv color space. 我尝试制作一个使用单个rgb通道值进行插值的函数,这给出了更相似的结果,但是感觉到的颜色的亮度和饱和度已关闭,因此我尝试在hsv颜色空间中进行此操作。

Can anyone create a function or explain how to achieve the gradient above using the hsv color space? 谁能创建一个函数或解释如何使用hsv颜色空间实现上述渐变?

It sounds like you want to create a gradient across the RGB colorspace rather than across the hue. 听起来您想跨RGB颜色空间而不是色调创建渐变。 And your problem is that you wrote a bunch of code to convert it to HSV so you can gradient across the hue and convert it back? 而您的问题是,您编写了一堆代码将其转换为HSV,以便可以在色调上渐变并将其转换回? If so, the answer is simple: don't write that code; 如果是这样,答案很简单:不要编写该代码; just stick with RGB. 坚持使用RGB。

Since you haven't given us a complete example, I can't show you exactly how to change your code, but here's a simple version: 由于您尚未提供完整的示例,因此我无法向您确切演示如何更改代码,但是这里有一个简单的版本:

r1, g1, b1 = rgb1
r2, g2, b2 = rgb2
rdelta, gdelta, bdelta = (r2-r1)/steps, (g2-g1)/steps, (b2-b1)/steps
for step in range(steps):
    r1 += rdelta
    g1 += gdelta
    b1 += bdelta
    output.append((r1, g1, b1))

So, for example, because g1 and g2 are both 0, none of your output colors will include any green, which I think is what you're after. 因此,例如,由于g1g2均为0,因此您的输出颜色都不会包含任何绿色,我想这就是您要追求的。

(Of course if you're using numpy arrays, the whole thing is even simpler—you can just operate on rows as 2D arrays of RGB columns, and replace the whole loop with a single call to linspace .) (当然,如果您使用的是numpy数组,则整个过程甚至更简单-您可以将行作为RGB列的2D数组进行操作,并用一次对linspace调用来替换整个循环。)

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

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