Reverse engineering matlab's jet function into a function of x

Alec Jacobson

January 13, 2013

weblog/

Here are some anonymous functions to act like matlab's jet function for building colormaps. Here I return the R,G,B values for a given parameter value between [0,1].
R = @(x) (x<3/8).*0 + (x>=3/8 & x<5/8).*(x-3/8)./(5/8-3/8) + (x>=5/8 & x<7/8).*1 + (x>=7/8).*(1-(x-7/8)./(1-7/8).*0.5);
G = @(x) (x<1/8).*0 + (x>=1/8 & x<3/8).*(x-1/8)./(3/8-1/8) + (x>=3/8 & x<5/8).*1 + (x>=5/8 & x<7/8).*(1-(x-5/8)./(7/8-5/8)) + (x>7/8).*0;
B = @(x) (x<1/8).*(0.5+(x)./(1/8).*0.5) + (x>=1/8 & x<3/8).*1 + (x>=3/8 & x<5/8).*(1-(x-3/8)./(5/8-3/8)) + (x>5/8).*0;
Test it with:
x = linspace(0,1,1024);
plot(x,jet(numel(x)),x,[R(x);G(x);B(x)],'--','LineWidth',3);
Note: This will only match jet's output for large m: jet(m).