Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random 'greenish' colors

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.

like image 923
Echo says Reinstate Monica Avatar asked Oct 18 '09 22:10

Echo says Reinstate Monica


People also ask

How do you make RGB green?

Green is the result of mixing blue and yellow.

How do I generate a list of random colors in python?

Given colour = [ "red", "blue", "green", "yellow", "purple", "orange", "white", "black" ] generate and print a list of 50 random colours. You will need to use the random module to get random numbers. Use range and map to generated the required amount of numbers. Then use map to translate numbers to colours.

How do I make random pastel colors?

How to generate Random Pastel Colors? In order to get a random pastel color, we first generate a random color. Then we sature it a little and mix this color with white color. Combining these three steps, we get a random pastel color that you can then convert to your desired format such as HEX or RGB.


2 Answers

Simple solution: Use the HSL or HSV color space instead of rgb (convert it to RGB afterwards if you need this). The difference is the meaning of the tuple: Where RGB means values for Red, Green and Blue, in HSL the H is the color (120 degree or 0.33 meaning green for example) and the S is for saturation and the V for the brightness. So keep the H at a fixed value (or for even more random colors you could randomize it by add/sub a small random number) and randomize the S and the V. See the wikipedia article.

like image 157
theomega Avatar answered Sep 28 '22 01:09

theomega


As others have suggested, generating random colours is much easier in the HSV colour space (or HSL, the difference is pretty irrelevant for this)

So, code to generate random "green'ish" colours, and (for demonstration purposes) display them as a series of simple coloured HTML span tags:

#!/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() 

The output (when viewed in a web-browser) should look something along the lines of:

Example output, showing random green colours

Edit: I didn't know about the colorsys module. Instead of the above hsv_to_rgb function, you could use colorsys.hsv_to_rgb, which makes the code much shorter (it's not quite a drop-in replacement, as my hsv_to_rgb function expects the hue to be in degrees instead of 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) 
like image 31
dbr Avatar answered Sep 28 '22 00:09

dbr