将RGB三元组列表分类到光谱中 [英] Sorting a list of RGB triplets into a spectrum

查看:326
本文介绍了将RGB三元组列表分类到光谱中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RGB三胞胎列表,我想以这样的方式绘制它们,使它们形成类似光谱的形状.

I have a list of RGB triplets, and I'd like to plot them in such a way that they form something like a spectrum.

我已经将它们转换为人们似乎推荐的HSV.

I've converted them to HSV, which people seem to recommend.

from PIL import Image, ImageDraw
import colorsys

def make_rainbow_rgb(colors, width, height):
    """colors is an array of RGB tuples, with values between 0 and 255"""

    img = Image.new("RGBA", (width, height))
    canvas = ImageDraw.Draw(img)

    def hsl(x):
        to_float = lambda x : x / 255.0
        (r, g, b) = map(to_float, x)
        h, s, l = colorsys.rgb_to_hsv(r,g,b)
        h = h if 0 < h else 1 # 0 -> 1
        return h, s, l

    rainbow = sorted(colors, key=hsl)

    dx = width / float(len(colors)) 
    x = 0
    y = height / 2.0
    for rgb in rainbow:
        canvas.line((x, y, x + dx, y), width=height, fill=rgb)
        x += dx
    img.show()

但是,结果看起来并不太像彩虹色的y光谱.我怀疑我需要转换为其他颜色空间或以不同方式处理HSL三元组.

However, the result doesn't look very much like a nice rainbow-y spectrum. I suspect I need to either convert to a different color space or handle the HSL triplet differently.

有人知道我需要做些什么来使这些数据看起来像彩虹吗?

Does anyone know what I need to do to make this data look roughly like a rainbow?

更新:

我正在研究希尔伯特曲线,并重新研究了这个问题.按照它们沿Hilbert曲线的位置对RGB值(两个图像中的相同颜色)进行排序会产生一个有趣的结果(如果仍不能完全令人满意):

I was playing around with Hilbert curves and revisited this problem. Sorting the RGB values (same colors in both images) by their position along a Hilbert curve yields an interesting (if still not entirely satisfying) result:

推荐答案

您正在尝试将三维空间转换为一维空间.正如Oli所说,无法保证您可以从中获得令人愉悦的彩虹.

You're trying to convert a three-dimensional space into a one-dimensional space. There's no guarantee that you can make a pleasing rainbow out of it, as Oli says.

您可以做的是根据饱和度和值/亮度将颜色分组"到几个不同的类别中,然后在这些类别中进行排序,以获得几个独立的渐变.例如,高饱和度颜色首先用于经典彩虹,然后是中饱和度高值颜色(粉彩),然后是低饱和度(灰色).

What you can do is "bucket" the colors into a few different categories based on saturation and value/lightness, and then sort within the categories, to get several independent gradients. For example, high-saturation colors first for the classic rainbow, then mid-saturation high-value colors (pastels), then low-saturation (grays).

或者,如果您只关心彩虹,则将其转换为hsl,然后将slam饱和度设置为1.0,将其值设置为0.5,然后转换回rgb并呈现该颜色,而不是原始颜色.

Alternately, if all you care about is the rainbow, convert to hsl, then slam saturation to 1.0 and value to 0.5, convert back to rgb and render that instead of the original color.

这篇关于将RGB三元组列表分类到光谱中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆