# 如何产生随机的“绿色”的颜色

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

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

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

` `greenval = randint(100, 255) redval = randint(20,(greenval - 60)) blueval = randint((redval - 20), (redval + 20)) color = (redval, greenval, blueval)` `

` `colorsys.hsv_to_rgb(h, s, v) Convert the color from HSV coordinates to RGB coordinates.` `

HSx色彩空间的解决scheme是一个非常好的解决scheme。 但是，如果您需要的东西非常简单，对色彩的分布（如均匀性）没有特别的要求，那么简单的基于RGB的解决scheme就是确保G值大于R和B

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

` ` /** @param h hue [0, 2PI] @param s saturation [0,1] @param l lightness [0,1] @return object {r,g,b} {[0,1],[0,1][0,1]} */ public function hslToRGB(h:Number, s:Number, l:Number):Color { var q:Number = (l<0.5)?l*(1+s):l+sl*s; var p:Number = 2*lq; var h_k:Number = h/(Math.PI*2); var t_r:Number = h_k+1/3; var t_g:Number = h_k; var t_b:Number = h_k-1/3; if (t_r < 0) ++t_r; else if (t_r > 1) --t_r; if (t_g < 0) ++t_g; else if (t_g > 1) --t_g; if (t_b < 0) ++t_b; else if (t_b > 1) --t_b; var c:Color = new Color(); if (t_r < 1/6) cr = p+((qp)*6*t_r); else if (t_r < 1/2) cr = q; else if (t_r < 2/3) cr = p+((qp)*6*(2/3-t_r)); else cr = p; if (t_g < 1/6) cg = p+((qp)*6*t_g); else if (t_g < 1/2) cg = q; else if (t_g < 2/3) cg = p+((qp)*6*(2/3-t_g)); else cg = p; if (t_b < 1/6) cb = p+((qp)*6*t_b); else if (t_b < 1/2) cb = q; else if (t_b < 2/3) cb = p+((qp)*6*(2/3-t_b)); else cb = p; return c; }` `

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