Generating a rainbow?

Gary Herron gherron at
Thu Apr 8 13:14:43 EDT 2010

Tobiah wrote:
> I'm having a difficult time with this.  I want
> to display a continuous range of hues using HTML
> hex representation (#RRGGBB).  How would I go 
> about scanning through the hues in order to
> make a rainbow?
> Thanks,
> Toby

Use the hue-saturation-value color space, and call hsv_to_rgb from the 
standard Python library to convert to RGB.  Enjoy!

Gary Herron

from colorsys import hsv_to_rgb

for hue ....:
  rgb = hsv_to_rgb(hue, saturation, value)

Let 'hue' run from 0 (red) through 2/3 (blue)  Hues from 2/3 to 1 get 
into purples and magentas, which are not spectral (i.e., rainbow) colors.

Set 'saturation' to perhaps 0.5 (for a washed out effect) through 1.0 
(for pure color).  Even an intensely colorful rainbow has lots of white 
light mixed in with it;  a saturation of 0.5 is probably good.

Set 'value' to something in the range of 0 to 1 to control brightness.

More information about the Python-list mailing list