Ech*_*ica 33 python language-agnostic random colors
任何人都有任何关于如何制作绿色随机颜色的建议吗?现在我正在生成颜色:
color = (randint(100, 200), randint(120, 255), randint(100, 200))
Run Code Online (Sandbox Code Playgroud)
这大部分都有效,但我的褐色很多.
dbr*_*dbr 21
正如其他人所建议的那样,在HSV颜色空间中生成随机颜色要容易得多(或者HSL,差异与此无关)
因此,生成随机"绿色"颜色的代码和(出于演示目的)将它们显示为一系列简单的彩色HTML span标记:
#!/usr/bin/env python2.5
"""Random green colour generator, written by dbr, for
http://stackoverflow.com/questions/1586147/how-to-generate-random-greenish-colors
"""
def hsv_to_rgb(h, s, v):
"""Converts HSV value to RGB values
Hue is in range 0-359 (degrees), value/saturation are in range 0-1 (float)
Direct implementation of:
http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_HSV_to_RGB
"""
h, s, v = [float(x) for x in (h, s, v)]
hi = (h / 60) % 6
hi = int(round(hi))
f = (h / 60) - (h / 60)
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
if hi == 0:
return v, t, p
elif hi == 1:
return q, v, p
elif hi == 2:
return p, v, t
elif hi == 3:
return p, q, v
elif hi == 4:
return t, p, v
elif hi == 5:
return v, p, q
def test():
"""Check examples on..
http://en.wikipedia.org/wiki/HSL_and_HSV#Examples
..work correctly
"""
def verify(got, expected):
if got != expected:
raise AssertionError("Got %s, expected %s" % (got, expected))
verify(hsv_to_rgb(0, 1, 1), (1, 0, 0))
verify(hsv_to_rgb(120, 0.5, 1.0), (0.5, 1, 0.5))
verify(hsv_to_rgb(240, 1, 0.5), (0, 0, 0.5))
def main():
"""Generate 50 random RGB colours, and create some simple coloured HTML
span tags to verify them.
"""
test() # Run simple test suite
from random import randint, uniform
for i in range(50):
# Tweak these values to change colours/variance
h = randint(90, 140) # Select random green'ish hue from hue wheel
s = uniform(0.2, 1)
v = uniform(0.3, 1)
r, g, b = hsv_to_rgb(h, s, v)
# Convert to 0-1 range for HTML output
r, g, b = [x*255 for x in (r, g, b)]
print "<span style='background:rgb(%i, %i, %i)'> </span>" % (r, g, b)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
输出(在Web浏览器中查看时)应该看起来像是:
编辑:我不知道colorsys模块.hsv_to_rgb
您可以使用colorsys.hsv_to_rgb而不是上述函数,这使得代码更短(它不是一个简单的替代品,因为我的hsv_to_rgb
函数期望色调是以度为单位而不是0-1):
#!/usr/bin/env python2.5
from colorsys import hsv_to_rgb
from random import randint, uniform
for x in range(50):
h = uniform(0.25, 0.38) # Select random green'ish hue from hue wheel
s = uniform(0.2, 1)
v = uniform(0.3, 1)
r, g, b = hsv_to_rgb(h, s, v)
# Convert to 0-1 range for HTML output
r, g, b = [x*255 for x in (r, g, b)]
print "<span style='background:rgb(%i, %i, %i)'> </span>" % (r, g, b)
Run Code Online (Sandbox Code Playgroud)
Ant*_*lls 16
看看colorsys
模块:
http://docs.python.org/library/colorsys.html
使用HSL或HSV颜色空间.将色调随机化为接近绿色,然后选择完全随机的饱和度和V(亮度).
小智 9
如果你坚持使用RGB,你基本上只需要确保G值大于R和B,并尝试保持蓝色和红色值相似,这样色调就不会太疯狂.从Slaks扩展,也许就像(我几乎不知道Python):
greenval = randint(100, 255)
redval = randint(20,(greenval - 60))
blueval = randint((redval - 20), (redval + 20))
color = (redval, greenval, blueval)
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下,您很幸运能够想要原色的变化,但对于这样的艺术用途,最好指定色轮坐标而不是原色.
您可能需要colorsys
模块中的某些内容,例如:
colorsys.hsv_to_rgb(h, s, v)
Convert the color from HSV coordinates to RGB coordinates.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7188 次 |
最近记录: |