views:

398

answers:

4
+8  Q: 

Paint me a Rainbow

How would you go about making a range of RGB colours evenly spaced over the spectral colour range? So as to look like a real rainbow.

+8  A: 

Use HSL instead: fix the brightness and saturation and vary the hue from 0 to 360, then convert to RGB.

HSL describes colors as they are perceived by people. RGB describes them as they are used by machines. So you can't really do anything visually pleasing directly using RGB.

Laurent
+11  A: 
John Kugelman
+1  A: 
  • Red (web colour) (Hex: #FF0000) (RGB: 255, 0, 0)
  • Orange (colour wheel Orange) (Hex: #FF7F00) (RGB: 255, 127, 0)
  • Yellow (web colour) (Hex: #FFFF00) (RGB: 255, 255, 0)
  • Green (X11) (Electric Green) (HTML/CSS “Lime”) (Colour wheel green) (Hex: #00FF00) (RGB: 0, 255, 0)
  • Blue (web colour) (Hex: #0000FF) (RGB: 0, 0, 255)
  • Indigo (Electric Indigo) (Hex: #6600FF) (RGB: 102, 0, 255)
  • Violet (Electric Violet) (Hex: #8B00FF) (RGB: 139, 0, 255)

Between each colour make linear interpolation.

ralu
linear interpolation in which color space? ;)
Stefano Borini
In RGB of course. You might take this points for linear interpolations #FF0000 #FFFF00 #00FF00 #00FFFF #0000FF #FF00FF #FF0000 in that case it would look like less natural. I took "rainbow" colors intepolations when making fancy RGB lightings of glass
ralu
Isn't it better to end on Vivid violet instead of Electric violet?
Annan
I got this this values from http://en.wikipedia.org/wiki/Rainbow
ralu
I was looking at http://en.wikipedia.org/wiki/Violet_(color) which says Electric violet is in the middle of the violet spectrum while vivid violet is at the very edge.
Annan
+2  A: 

The simplest approach is to do a linear interpolation (in RGB) between each consecutive pair in this sequence:

  • #ff0000 red
  • #ffff00 yellow
  • #00ff00 green
  • #00ffff cyan
  • #0000ff blue
  • #ff00ff magenta
  • #ff0000 back to red

This should get you pretty much the same result as sweeping through the hue values in HSV or HSL, but lets you work directly in RGB. Note that only one component changes for each interpolation, which simplifies things. Here's a Python implementation:

def rainbow():
  r, g, b = 255, 0, 0
  for g in range(256):
    yield r, g, b
  for r in range(255, -1, -1):
    yield r, g, b
  for b in range(256):
    yield r, g, b
  for g in range(255, -1, -1):
    yield r, g, b
  for r in range(256):
    yield r, g, b
  for b in range(255, -1, -1):
    yield r, g, b
Laurence Gonsalves
One minor problem is that rainbows end in violet, not red.
Annan
True. This problem exists with the HSL and HSV solutions as well.
Laurence Gonsalves