Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Generating a rainbow?

Reply
Thread Tools

Generating a rainbow?

 
 
Tobiah
Guest
Posts: n/a
 
      04-08-2010
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
 
Reply With Quote
 
 
 
 
Chris Colbert
Guest
Posts: n/a
 
      04-08-2010
On Thu, Apr 8, 2010 at 12:46 PM, Tobiah <(E-Mail Removed)> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>


In [43]: possible = []

In [44]: for i in range(2**:
....: h = hex(i).lstrip('0x')
....: while len(h) < 2:
....: h = '0' + h
....: possible.append(h)
....:
....:

In [45]: full = [r + g + b for r in possible for g in possible for b
in possible]

In [46]: len(full)
Out[46]: 16777216

In [47]: 2**24
Out[47]: 16777216
 
Reply With Quote
 
 
 
 
Richard Thomas
Guest
Posts: n/a
 
      04-08-2010
On Apr 8, 5:46*pm, Tobiah <(E-Mail Removed)> 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


Look at the colorsys module.

http://docs.python.org/library/color...odule-colorsys
 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      04-08-2010
> Look at the colorsys module.
>
> http://docs.python.org/library/color...lorsys#module-

colorsys

That so rocks. Thanks!
 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      04-08-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.
 
Reply With Quote
 
Chris Colbert
Guest
Posts: n/a
 
      04-08-2010
On Thu, Apr 8, 2010 at 1:14 PM, Tobiah <(E-Mail Removed)> wrote:
>> Look at the colorsys module.
>>
>> http://docs.python.org/library/color...lorsys#module-

> colorsys
>
> That so rocks. *Thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list
>


How does that answer your original question?
 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      04-08-2010
> How does that answer your original question?

I was able to do this:

import colorsys

sat = 1
value = 1
length = 1000
for x in range(0, length + 1):
hue = x / float(length)
color = list(colorsys.hsv_to_rgb(hue, sat, value))
for x in range(3):
color[x] = int(color[x] * 255)
hexval = ("#%02x%02x%02x" % tuple(color)).upper()
print "<div style='height: 1; width: 500; background-color: %s'>"
% hexval


http://tobiah.org/rainbow.html
 
Reply With Quote
 
Peter Parker
Guest
Posts: n/a
 
      04-08-2010
Tobiah wrote:
>> How does that answer your original question?

>
> I was able to do this:
>
> import colorsys
>
> sat = 1
> value = 1
> length = 1000
> for x in range(0, length + 1):
> hue = x / float(length)
> color = list(colorsys.hsv_to_rgb(hue, sat, value))
> for x in range(3):
> color[x] = int(color[x] * 255)
> hexval = ("#%02x%02x%02x" % tuple(color)).upper()
> print "<div style='height: 1; width: 500; background-color: %s'>"
> % hexval
>
>
> http://tobiah.org/rainbow.html


Roy G. Biv would like you to order the colors according to their
wavelength. He would also like to see Orange and Yellow appear in
your rainbow.
 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      04-08-2010
Tobiah:

> for x in range(0, length + 1):
> ...
> for x in range(3):


You should use different variables for the two loops.

Neil

 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      04-08-2010
Me:
> You should use different variables for the two loops.


Actually it is closing the divs that makes it work in FireFox:

import colorsys

sat = 1
value = 1
length = 1000
for h in range(0, length + 1):
hue = h / float(length)
color = list(colorsys.hsv_to_rgb(hue, sat, value))
for x in range(3):
color[x] = int(color[x] * 255)
hexval = ("#%02x%02x%02x" % tuple(color)).upper()
print(
"<div style='height: 1; width: 500; background-color: %s'>"
"</div>" % hexval)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generating within a case statement colin VHDL 3 03-07-2005 02:51 PM
Generating a trigger signal to align two processes running on different clocks Divyang M VHDL 4 02-17-2005 09:17 PM
Generating a output signal with a specific pulse width arturomeza@gmail.com VHDL 3 12-21-2004 07:33 PM
Any idea about generating SAIF files ? New to Power Analysis VHDL 3 10-22-2004 09:24 AM
Generating combination signal from within clocked clocked block Valentin Tihomirov VHDL 3 10-28-2003 08:56 AM



Advertisments