查找N个不同的RGB颜色 [英] Finding N Distinct RGB Colors

查看:152
本文介绍了查找N个不同的RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以图形方式显示N行的图形,我试图找到一种方法来动态分配不同的颜色,基于我有多少行。 RGB中的值范围从0到1.我不能使用白色,因为背景是白色的。我发现N < 7:

I'm trying to graphically display a graph of N lines and I'm trying to find a way to dynamically assign distinct colors based on how many lines I have. The values in RGB range from 0 to 1. I can't use white because the background is white. I found it easy for N < 7:

r=(h&0x4)/4;
g=(h&0x2)/2;
b=h&0x1;

这给我黑色,蓝色,绿色,青色,红色,洋红色,黄色。但之后,它将使用白色,然后循环。有人知道一个好的方法来为索引分配RGB值吗?我还有一个不透明度值来玩。

This gives me black, blue, green, cyan, red, magenta, yellow. But after that it will use white and then loop. Does anybody know a good way to assign RGB values for an index? I also have an opacity value to play with.

推荐答案

我首选的方法是找到

My preferred method for doing this is to find n evenly-spaced points along the colour wheel.

我们将色轮表示为介于0和360之间的值范围。因此,我们将使用的值是 360 / n * 0 360 / n * 1 ,..., 360 / n *(n-1)。为此,我们定义了每种颜色的 hue 。我们可以通过将饱和度设置为1和亮度设置为1来将这些颜色描述为色相 - 饱和度值(HSV)颜色。

We represent the colour wheel as a range of values between 0 and 360. Thus, the values we will use are 360 / n * 0, 360 / n * 1, ..., 360 / n * (n - 1). In doing this, we've defined the hue of each of our colours. We can describe each of these colours as Hue-Saturation-Value (HSV) colours by setting saturation to 1 and lightness to 1.

更丰富;较低的饱和度意味着颜色更接近灰色;较高的亮度意味着颜色是较亮的;较低的亮度意味着颜色是较暗的。)

(A higher saturation means the colour is more "rich"; a lower saturation means the colour is closer to gray. A higher lightness means the colour is "brighter"; a lower lightness means the colour is "darker".)

现在,一个简单的计算给出了每种颜色的RGB值。

Now, a simple calculation gives us the RGB values of each of these colours.

http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_HSV_to_RGB

请注意,可以简化给出的方程:

Note that the equations given can be simplified:


  • p = v *(1 - s)= 1 * 1)= 1 * 0 = 0

  • q = v *(1 - f * s) f * 1)= 1 - f

  • t = v *(1 - (1 - f)* s)= 1 *(1-(1-f)* 1)= 1-(1-f)= 1-1 + f = f

  • p = v * (1 - s) = 1 * (1 - 1) = 1 * 0 = 0
  • q = v * (1 - f * s) = 1 * (1 - f * 1) = 1 - f
  • t = v * (1 - (1 - f) * s) = 1 * (1 - (1 - f) * 1) = 1 - (1 - f) = 1 - 1 + f = f

注意:这是一个非常低效的实现。在Python中给出这个例子的本质是我可以给出可执行的伪代码。

Note: This is intentionally a horribly inefficient implementation. The point of giving this example in Python is essentially so I can give executable pseudocode.

import math

def uniquecolors(n):
    """Compute a list of distinct colors, each of which is represented as an RGB 3-tuple."""
    hues = []
    # i is in the range 0, 1, ..., n - 1
    for i in range(n):
        hues.append(360.0 / i)

    hs = []
    for hue in hues:
        h = math.floor(hue / 60) % 6
        hs.append(h)

    fs = []
    for hue in hues:
        f = hue / 60 - math.floor(hue / 60)
        fs.append(f)

    rgbcolors = []
    for h, f in zip(hs, fs):
        v = 1
        p = 0
        q = 1 - f
        t = f
        if h == 0:
            color = v, t, p
        elif h == 1:
            color = q, v, p
        elif h == 2:
            color = p, v, t
        elif h == 3:
            color = p, q, v
        elif h == 4:
            color = t, p, v
        elif h == 5:
            color = v, p, q
        rgbcolors.append(color)

    return rgbcolors



在Python中简明实现



Concise Implementation in Python

import math

v = 1.0
s = 1.0
p = 0.0
def rgbcolor(h, f):
    """Convert a color specified by h-value and f-value to an RGB
    three-tuple."""
    # q = 1 - f
    # t = f
    if h == 0:
        return v, f, p
    elif h == 1:
        return 1 - f, v, p
    elif h == 2:
        return p, v, f
    elif h == 3:
        return p, 1 - f, v
    elif h == 4:
        return f, p, v
    elif h == 5:
        return v, p, 1 - f

def uniquecolors(n):
    """Compute a list of distinct colors, ecah of which is
    represented as an RGB three-tuple"""
    hues = (360.0 / n * i for i in range(n))
    hs = (math.floor(hue / 60) % 6 for hue in hues)
    fs = (hue / 60 - math.floor(hue / 60) for hue in hues)
    return [rgbcolor(h, f) for h, f in zip(hs, fs)]

这篇关于查找N个不同的RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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