Mapping [0,1] into the color space

In many situations, I find that I want to translate a parameter t in the range 0 ≤ t ≤ 1 into a color. There are, naturally uncountably (if we pretend things are continuous) many ways to do this.

The general idea that I usually use is to assign the red, green, and blue components of the color using three functions of t; that is, red=f(t), green=g(t), and blue=h(t), say, with red, green, and blue in the range 0 ≤ t ≤ 1.

What I usually want is f,g, and h to be functions with range [0,1] to get as wide a variety of colors as possible. They should also be different functions to increase the variety, and make the colors "less grey".

So what simple functions do we have to choose from that map [0,1] onto [0,1]? Well, there's the identity function f(t)=t, and its friendly cousin f(t)=1-t. That's two functions that do the job; what's a third? One is f(t) = 4t(1-t). This function is zero at 0 and 1, and 1 at t=0.5. Works nicely (see below). Another choice would be this last function composed with itself: f(t) = -64t4 + 128t3 - 80t2 + 16t.

A class of second-degree polynomials that works is
f(t)=A(x-h)2 and f(t) = 1-A(x-h)2
with 0 ≤ h ≤ 1, and A = min{1/h2,1/(1-h)2}.

Of course, there is always the classic f(t)=6(t2/2+t3/3) = t2(3-2t) which is useful for lots of things since f'(0)=f'(1)=1, in addition to mapping [0,1] to [0,1].

If you insist on using circular functions, may I suggest f(t) = 1/2 + 1/2 sin(2 π n (x- 1/(4n))) where n is any positive integer? Other horizontal shifts of these functions work great, too.

Below are a some examples of functions f(t),g(t), and h(t) and the resulting color spectrum, discretized for easy viewing. I may be adding to these in the future...

t,t,t - black to white (all grey)


1-t,t,t - red to aqua through grey


t,1-t,t - green to magenta through grey


t,t,1-t - blue to yellow through grey


4(1-t)t,t,1-t


t,4(1-t)t,1-t


t,1-t,4(1-t)t


0.5+0.5sin(4 π (t-1/8)),t,1-t




back to miscellany