Generating a rainbow?

Discussion in 'Python' started by Tobiah, Apr 8, 2010.

  1. Tobiah

    Tobiah Guest

    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
    Tobiah, Apr 8, 2010
    #1
    1. Advertising

  2. On Thu, Apr 8, 2010 at 12:46 PM, 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    In [43]: possible = []

    In [44]: for i in range(2**8):
    ....: 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
    Chris Colbert, Apr 8, 2010
    #2
    1. Advertising

  3. Richard Thomas, Apr 8, 2010
    #3
  4. Tobiah

    Tobiah Guest

    Tobiah, Apr 8, 2010
    #4
  5. Tobiah

    Gary Herron Guest

    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.
    Gary Herron, Apr 8, 2010
    #5
  6. Chris Colbert, Apr 8, 2010
    #6
  7. Tobiah

    Tobiah Guest

    > 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
    Tobiah, Apr 8, 2010
    #7
  8. Tobiah

    Peter Parker Guest

    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.
    Peter Parker, Apr 8, 2010
    #8
  9. Tobiah

    Neil Hodgson Guest

    Tobiah:

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


    You should use different variables for the two loops.

    Neil
    Neil Hodgson, Apr 8, 2010
    #9
  10. Tobiah

    Neil Hodgson Guest

    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)
    Neil Hodgson, Apr 8, 2010
    #10
  11. On Thu, Apr 8, 2010 at 2:34 PM, 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    I see. It appears I misunderstood the question.

    The link doesn't work in Chrome btw.
    Chris Colbert, Apr 8, 2010
    #11
  12. Tobiah

    Tobiah Guest


    >> You should use different variables for the two loops.

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


    Hah. I new that the rainbow wasn't complete and that it
    didn't work in Opera. I just fizzled on the closing of the
    divs.

    I also don't get why it worked at all when I was stomping
    on my x variable in the inside loop.

    It's better now.

    http://tobiah.org/rainbow.html
    Tobiah, Apr 9, 2010
    #12
  13. Tobiah

    Peter Parker Guest

    Tobiah wrote:
    >>> You should use different variables for the two loops.

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

    >
    > Hah. I new that the rainbow wasn't complete and that it
    > didn't work in Opera. I just fizzled on the closing of the
    > divs.
    >
    > I also don't get why it worked at all when I was stomping
    > on my x variable in the inside loop.
    >
    > It's better now.
    >
    > http://tobiah.org/rainbow.html


    Better than before ... Oy G. Bivr
    Peter Parker, Apr 11, 2010
    #13
  14. Tobiah

    Robert Kern Guest

    On 2010-04-11 12:54 , Peter Parker wrote:
    > Tobiah wrote:
    >>>> You should use different variables for the two loops.
    >>> Actually it is closing the divs that makes it work in FireFox:
    >>>

    >>
    >> Hah. I new that the rainbow wasn't complete and that it
    >> didn't work in Opera. I just fizzled on the closing of the
    >> divs.
    >> I also don't get why it worked at all when I was stomping
    >> on my x variable in the inside loop.
    >>
    >> It's better now.
    >>
    >> http://tobiah.org/rainbow.html

    >
    > Better than before ... Oy G. Bivr


    Uh, the first color is #FF0000, pure red, not orange.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Apr 11, 2010
    #14
  15. Tobiah

    Peter Parker Guest

    Robert Kern wrote:
    > On 2010-04-11 12:54 , Peter Parker wrote:
    >> Tobiah wrote:
    >>>>> You should use different variables for the two loops.
    >>>> Actually it is closing the divs that makes it work in FireFox:
    >>>>
    >>>
    >>> Hah. I new that the rainbow wasn't complete and that it
    >>> didn't work in Opera. I just fizzled on the closing of the
    >>> divs.
    >>> I also don't get why it worked at all when I was stomping
    >>> on my x variable in the inside loop.
    >>>
    >>> It's better now.
    >>>
    >>> http://tobiah.org/rainbow.html

    >>
    >> Better than before ... Oy G. Bivr

    >
    > Uh, the first color is #FF0000, pure red, not orange.
    >


    Ah, you're right, but it's only 1 pixel high so it's hard to
    see. The last one is also #FF0000, pure red ... Roy G. Bivr
    Peter Parker, Apr 12, 2010
    #15
  16. Tobiah <> writes:

    > 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?


    if you mean real rainbows when you say "rainbow", as rainbows go from
    low to high wavelengths and you have to specify colors to your display
    in RGB, i think that you have to read, e.g.,

    http://www.physics.sfasu.edu/astro/color.html

    otoh if you mean simply a continuos palette with varying hues,
    excellent answers were already posted

    hth
    g
    --
    compro mobili vecchi - vendo mobili antichi
    Giacomo Boffi, Apr 13, 2010
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Valentin Tihomirov
    Replies:
    3
    Views:
    519
    Jonathan Bromley
    Oct 28, 2003
  2. Sasha
    Replies:
    1
    Views:
    468
    Karl Seguin
    Feb 2, 2005
  3. Simon Forman

    How to make "rainbow" RGB values?

    Simon Forman, Feb 25, 2008, in forum: Python
    Replies:
    2
    Views:
    1,084
    Simon Forman
    Feb 25, 2008
  4. Andrew McNamara

    Re: How to make "rainbow" RGB values?

    Andrew McNamara, Feb 26, 2008, in forum: Python
    Replies:
    0
    Views:
    948
    Andrew McNamara
    Feb 26, 2008
  5. Thomas Roberts

    rainbow tables

    Thomas Roberts, Jul 5, 2008, in forum: Ruby
    Replies:
    1
    Views:
    75
    fedzor
    Jul 6, 2008
Loading...

Share This Page