Previously posted on on April 28, 2011

I’ve been looking around the internet lately for interesting ways to calculate colors. More than anything I was looking for functions that I could use to make a strip of 3-color LEDs change from one color to another. I started with a brute-force 3-loop approach where I just did and inner most look though 0-255 for Red, then a loop around that from 0-255 for Green and then the outer-most loop from 0-255 for Blue. Something like this:

for (b=0; b < 255; b++) {
  for (g=0; g < 255; g++) {
    for (r=0; r < 255; r++) {
      draw_rectangle(r,g,b) ;

I have to say, the colors produced were not pleasing to the eye at all.

The I came accross this page describing how to make a rainbow. It has a great explanation on how to use a wave function, in this case sin(), to make colors. I’m not going to get into all the theory an implementation of the algorithm because the other page does it so well. I just made a little processing sketch where I can play with some of the parameters (frequency, amplitude, and center) to see what will happen to the color sequence.

The sketch was cobbled together in a couple of minutes so I’m sorry it doesn’t look very nice. Here is the Processing sketch and applet so it can be modified to look better :). (BTW, if you don’t know about Processing, go check it out. It’s pretty amazing.)