How to generate random 'greenish' colors

2019-02-02 03:07发布

Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:

color = (randint(100, 200), randint(120, 255), randint(100, 200))

That mostly works, but I get brownish colors a lot.

9条回答
做个烂人
2楼-- · 2019-02-02 03:26

So in this case you are lucky enough to want variations on a primary color, but for artistic uses like this it is better to specify color wheel coordinates rather than primary color magnitudes.

You probably want something from the colorsys module like:

colorsys.hsv_to_rgb(h, s, v)
    Convert the color from HSV coordinates to RGB coordinates.
查看更多
乱世女痞
3楼-- · 2019-02-02 03:28

The solution with HSx color space is a very good one. However, if you need something extremely simplistic and have no specific requirements about the distribution of the colors (like uniformity), a simplistic RGB-based solution would be just to make sure that G value is greater than both R and B

rr = randint(100, 200)
rb = randint(100, 200)
rg = randint(max(rr, rb) + 1, 255)

This will give you "greenish" colors. Some of them will be ever so slightly greenish. You can increase the guaranteed degree of greenishness by increasing (absolutely or relatively) the lower bound in the last randint call.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-02 03:29

The simplest way to do this is to make sure that the red and blue components are the same, like this: (Forgive my Python)

rb = randint(100, 200)
color = (rb, randint(120, 255), rb)
查看更多
登录 后发表回答