Color Wheel Generation
Today I was working with a set of 32 items that each needed to be displayed with a unique color. The actual color assigned to any given item did not matter, but I wanted to make sure there was enough variation to avoid confusion among them. I also wanted to avoid anyone saying this project was from my blue period or green period.
I did a bit of web searching for color wheels and palettes containing around that many colors, but did not find anything that was immediately useful. I found articles on the theory of color wheels, JavaScript website color scheme generators, and palettes for various desktop environments. I just wanted a simple list of colors that were well distributed across the color space.
I ended up hacking together a quick Ruby script to generate a color wheel with 33 steps moving from red to green to blue and back to red. After I was finished with the project I decided to clean it up a bit, generalize it to n steps, and post it, figuring that it may be of use to someone else.
I am not a color scientist and I have no idea whether this is the “proper” way to generate a color wheel, but it worked for my purposes and I was happy with the results. The code is below:
def ramp(step, total_steps)
(255 * Math::sin(Math::PI/2.0/total_steps * step)).round
end
# Generates the color sequence for a color wheel containting the specified
# number of positions. If positions is not a multiple of 3, it will be
# rounded up to the next multiple.
def color_wheel(positions)
sps = (positions/3.0).ceil # steps per segment
colors = []
(0...sps).each do |step|
colors << "#%02x%02x00" % [ramp(sps - step, sps), ramp(step, sps)]
end
(0...sps).each do |step|
colors << "#00%02x%02x" % [ramp(sps - step, sps), ramp(step, sps)]
end
(0...sps).each do |step|
colors << "#%02x00%02x" % [ramp(step, sps), ramp(sps - step, sps)]
end
colors
end
And, here are the results when called with color_wheel(12):
Happy coloring!

Comments