如何生成随机的"绿色"颜色

Ech*_*ica 33 python language-agnostic random colors

任何人都有任何关于如何制作绿色随机颜色的建议吗?现在我正在生成颜色:

color = (randint(100, 200), randint(120, 255), randint(100, 200))
Run Code Online (Sandbox Code Playgroud)

这大部分都有效,但我的褐色很多.

the*_*ega 54

简单的解决方案:使用HSL或HSV颜色空间而不是rgb(如果需要,请将其转换为RGB).不同之处在于元组的含义:其中RGB表示红色,绿色和蓝色的值,在HSL中,H是颜色(例如120度或0.33表示绿色),S表示饱和度,V表示亮度.因此,将H保持在固定值(或者对于更随机的颜色,您可以通过添加/子化一个小的随机数随机化它)并随机化S和V.请参阅维基百科文章.

  • 我唯一要补充的是,你甚至可以将H从0.31变为0.35甚至更多,以获得一些稍微不同的色调. (4认同)

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)'>&nbsp;&nbsp;</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)'>&nbsp;&nbsp;</span>" % (r, g, b)
Run Code Online (Sandbox Code Playgroud)


Ant*_*lls 16

看看colorsys模块:

http://docs.python.org/library/colorsys.html

使用HSL或HSV颜色空间.将色调随机化为接近绿色,然后选择完全随机的饱和度和V(亮度).

  • 随机选择饱和度和亮度将包括纯黑色和白色作为可能的结果. (5认同)

小智 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)


Dig*_*oss 5

因此,在这种情况下,您很幸运能够想要原色的变化,但对于这样的艺术用途,最好指定色轮坐标而不是原色.

您可能需要colorsys模块中的某些内容,例如:

colorsys.hsv_to_rgb(h, s, v)
    Convert the color from HSV coordinates to RGB coordinates.
Run Code Online (Sandbox Code Playgroud)