I’m trying to generate a rainbow with 15 different colors with (runnable code here):
size(360,100);
colorMode(HSB, 360, 100, 100); // Hue in degrees in [0, 360],
// saturation/brightness in [0, 100]
// like in Photoshop
noStroke();
for (int i = 0; i < 15; i++)
{
fill(i*24, 100, 100); // 24*15 = 360
rect(i*24, 0, 25, 100);
}
but it doesn’t produce a rich 15 rainbow-color palette, instead some colors are missing (vivid yellow for example).
Is there a well known algorithm to produce a vivid rainbow color palette?
2
Answers
To understand what’s going on, try creating a program that shows a line for each value 0-360:
You’ll see this:
Notice that the “vivid yellow” band is much more narrow than, for example, the green or blue bands. That’s why simply sampling every X values doesn’t generate a yellow color.
The yellow color is around value
60
, so you could modify your increment so it lands on 60. Drawing 12 rectangles with a width of 30 lets you land on the yellow:Or you could come up with the values you want ahead of time and put them in an array instead of using an even distribution:
I created a function that generates N colors (rainbow) and outputs a list of strings (Hex values). This is in C# but logic can be converted. In order to understand what’s going on I graphed the red, blue, and green values vs n. Doing that you’ll see the three graphs each are piecewise functions with points of interest at n=0, n=1/4, n=1/2 and n=3/4.