我试图以图形方式显示N行的图形,我正在尝试根据我有多少行来找到一种动态分配不同颜色的方法.RGB中的值范围为0到1.由于背景为白色,因此无法使用白色.我发现N <7很容易:
r=(h&0x4)/4;
g=(h&0x2)/2;
b=h&0x1;
Run Code Online (Sandbox Code Playgroud)
这给了我黑色,蓝色,绿色,青色,红色,洋红色,黄色.但之后它将使用白色然后循环.有人知道为索引分配RGB值的好方法吗?我也有一个不透明度的值.
我这样做的首选方法是n
沿着色轮找到均匀间隔的点.
我们代表色轮为0和360之间的值的范围.因此,我们将使用值360 / n * 0
,360 / n * 1
,..., 360 / n * (n - 1)
.在这一过程中,我们已经定义了色相我们每一个颜色的.我们可以通过将饱和度设置为1并将亮度设置为1来将这些颜色中的每一种描述为色调饱和度值(HSV)颜色.
(饱和度越高意味着颜色越"丰富";饱和度越低意味着颜色越接近灰色;亮度越高意味着颜色越"亮";亮度越低意味着颜色越"暗".
现在,一个简单的计算给出了每种颜色的RGB值.
http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_HSV_to_RGB
请注意,给出的方程式可以简化:
注意:这是一个非常低效的实现.在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
Run Code Online (Sandbox Code Playgroud)
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)]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2851 次 |
最近记录: |