Colors 查找N个不同的RGB颜色
我试图以图形方式显示一个由N条线组成的图形,并试图找到一种方法,根据我有多少条线来动态分配不同的颜色。RGB中的值范围为0到1。我不能用白色,因为背景是白色的。我发现对于N<7的人来说很容易:Colors 查找N个不同的RGB颜色,colors,rgb,Colors,Rgb,我试图以图形方式显示一个由N条线组成的图形,并试图找到一种方法,根据我有多少条线来动态分配不同的颜色。RGB中的值范围为0到1。我不能用白色,因为背景是白色的。我发现对于N
r=(h&0x4)/4;
g=(h&0x2)/2;
b=h&0x1;
这给我黑色,蓝色,绿色,青色,红色,洋红,黄色。但在那之后,它将使用白色,然后循环。有人知道为索引分配RGB值的好方法吗?我还有一个不透明度值可以使用。我写了一次代码,完全解决了您描述的问题(背景也是白色的)。这和你的想法一样,只是泛化了。从OCaml到您的语言应该很容易适应 用法 你不需要告诉函数你需要多少颜色。用1,2,…,调用函数。。。要获得颜色编号1,编号2。。。小数字的颜色相距甚远,后者的颜色当然会越来越近 代码
lsl
是“逻辑左移”;lsr
是“逻辑右移”;代码>仅表示“访问引用”。在OCaml中,RGB颜色以单个整数表示,每种颜色一个字节
let number_to_color n =
let color = ref 0 in
let number = ref n in
for i = 0 to 7 do
color := (!color lsl 1) +
(if !number land 1 <> 0 then 1 else 0) +
(if !number land 2 <> 0 then 256 else 0) +
(if !number land 4 <> 0 then 65536 else 0);
number := !number lsr 3
done;
!color
让数字到颜色=
让颜色=参考0英寸
设编号=参考号n in
对于i=0到7 do
颜色:=(!颜色lsl 1)+
(如果!编号为land 1 0,则为1,否则为0)+
(如果!编号为2 0,则为256,否则为0)+
(如果!编号为4 0,则为65536,否则为0);
号码:=!号码LSR3
完成;
!颜色
我的首选方法是沿色轮找到n
等间距点
我们将色轮表示为介于0和360之间的一系列值。因此,我们将使用的值是360/n*0
,360/n*1
,…,360/n*(n-1)
。在此过程中,我们定义了每种颜色的色调。通过将饱和度设置为1,亮度设置为1,我们可以将每种颜色描述为色调饱和度值(HSV)颜色
(饱和度越高,颜色越“丰富”;饱和度越低,颜色越接近灰色。亮度越高,颜色越“明亮”;亮度越低,颜色越“暗”。)
现在,一个简单的计算给出了每种颜色的RGB值
请注意,可以简化给出的方程式:
- 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中的伪代码实现
注意:这是一个非常低效的实现。在Python中给出这个示例的意义在于,我可以给出可执行的伪代码
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中的简明实现
也许答案的一个很好的要求是颜色尽可能地彼此远离。(如果可以执行类似于0xF00、0x0F0和0x00F的操作,您不希望只选择0x001、0x002和0x003)您实际上没有可以使用的不透明度值:)在白色背景上,不透明度为50%的0,0,0与不透明度为100%的127127相同。换句话说,不透明度不是独立于其他参数的。在前20种颜色之后,如果不利用饱和度和/或亮度不同于1的颜色,就有点浪费了。这是绝对正确的。最好为s=1、v=1生成一组颜色,然后可能是s=0.5、v=1,依此类推。大概有一种算法可以做到这一点,但必须进行检查,以避免产生大量难以区分的近灰色或近黑色。如果f=(h/60-math.floor(h/60))floor(h/60)不总是0,因为h在0和5之间吗?如果这是真的,f在0和5/60之间。是这样吗?太棒了,苏亚特-我想在那里写“色调”。谢谢在uniquecolors的简明实现中,色调对象不应该是生成器,因为其他生成器将使用相同的迭代器,只产生预期颜色的一半。将线条更改为色调=[…]可以解决此问题。
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
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)]