Colormaps

Michaël Baudin

The goal of this page is to see the available color maps in Scilab.

The scripts

This page is inspired by Reynald Passerin's Scilab tools. But the code below is original, provided under the CeCILL.

Scilab provides several colormaps:

These colormaps can be used to color a plot, such as a 2D plot, 3D plot or a contour plot, for example.

The following script applies the autumn colormap to a 3D plot:

f = scf();
plot3d1();
f.color_map = autumncolormap(32);

This produces the following plot.

attachment:3Dplot_autumn.png

The colormap function are difficult to automate. This is because there are so many function. Instead, we suggest to create one single function, which would take the name of the color as an input argument. This is why it is convenient to create some helper functions.

The following function returns the matrix of all available colormaps.

function cmapmatrix = colormap_all()
    // Returns all the available colormaps.
    // Copyright (C) 2011 - Michael Baudin
    cmapmatrix = [
    "autumn"
    "bone"
    "cool"
    "copper"
    "gray"
    "hot"
    "hsv"
    "jet"
    "ocean"
    "pink"
    "rainbow"
    "spring"
    "summer"
    "white"
    "winter"
    ]
endfunction

The following function create a colormap given the number of colors and the name of a colormap.

function cmap = colormap_new(n,name)
    // Returns the color map given the number of colors and the name.
    // Copyright (C) 2011 - Michael Baudin
    cmapmatrix = colormap_all()
    // Check that the colormap is in the matrix
    k = find(name==cmapmatrix)
    if (k==[]) then
        error(gettext("%s: Undefined colormap %s\n"),"colormapnew",name)
    end
    // Compute the colormap
    instr = "cmap = "+name+"colormap(n)"
    ierr = execstr(instr,"errcatch")
    if ( ierr<> 0 ) then
        lerr = lasterror()
        error(gettext("%s: %s\n"),"colormapnew",lerr)
    end
endfunction

The getcolor function is not very convenient, because it not only plot the colormap, but also forces an interaction with the user. It also customize the graphics window, which may not be necessary in some situations. Moreover, it creates a new window, while it may be interesting to plot the colormap within the current graphics window.

The following function prints the current colormap on the current figure. The algorithm is extracted from the code of the getcolor function.

 function colormap_print()
     // Prints the colormap on the current figure.
     // Copyright (C) 2011 - Michael Baudin
     sdf;
     sda;
     fig = gcf();
     cmap = fig.color_map;

     N = size(cmap,1);
     wdim = [1,1];
     r = wdim(1)/wdim(2);
     n = round(sqrt(N/r));
     m = int(n*r);
     H = m*45; // These numbers set the size of the getcolor window
     W = n*45;
     fig.figure_size = [H,W];

     dx = wdim(1)/m;
     dy = wdim(2)/n;
     x = -dx;
     y = wdim(2);
     R = [0;0;dx*0.95;dy*0.95];
     rects = [];
     for k = 1:N
         if modulo(k,n)==1 then
             x = x+dx;
             y = wdim(2);
         end;
         rects = [rects,R+[x;y;0;0]];
         y = y-dy;
     end;

     xsetech([-1/8,-1/8,1+1/6,1+1/6],[0,0,wdim(1),wdim(2)]);
     // rectangles with the colors
     xrects(rects,1:N);
     // frame around the colors
     r = m*n-N;
     ddx = 0.05*dx;
     ddy = 0.05*dy;
     if r==0 then
         xpoly([-ddx,1,1,-ddx],[0,0,1+ddy,1+ddy],"lines",1);
     else
         xpoly([-ddx,1-1/n,1-1/n,1,1,-ddx],[0,0,r/m,r/m,1+ddy,1+ddy],"lines",1);
     end;

 endfunction

The following script performs a loop over the available colormaps. It creates a figure to plot the colormap and, if required, exports the content into a file.

 cmapmatrix = colormap_all();
 for c = cmapmatrix'
    mprintf("Colormap=%s\n",c);
    h=scf();
    h.color_map = colormap_new(n,c);
    colormap_print();
    // xs2png(h,c+".png"); Uncomment to create the files
    close(h);
 end

The figures

The following figures presents the colormaps available in Scilab.

autumn: attachment:autumn.png

bone: attachment:bone.png

cool: attachment:cool.png

copper: attachment:copper.png

gray: attachment:gray.png

hot: attachment:hot.png

hsv: attachment:hsv.png

jet: attachment:jet.png

ocean: attachment:ocean.png

pink: attachment:pink.png

rainbow: attachment:rainbow.png

sprint: attachment:spring.png

summer: attachment:summer.png

white: attachment:white.png

winter: attachment:winter.png

A side note

Since we are really lazy, we updated the current wiki page by using Scilab to generate the links to the figures. The following script generates the content of the wiki page which is required.

 for c = cmapmatrix'
    mprintf("[[ImageLink(%s.png,height=100)]]\n",c);
 end

The previous script produces the following output.

[[ImageLink(autumn.png,height=100)]]
[[ImageLink(bone.png,height=100)]]
[[ImageLink(cool.png,height=100)]]
[[ImageLink(copper.png,height=100)]]
[[ImageLink(gray.png,height=100)]]
[[ImageLink(hot.png,height=100)]]
[[ImageLink(hsv.png,height=100)]]
[[ImageLink(jet.png,height=100)]]
[[ImageLink(ocean.png,height=100)]]
[[ImageLink(pink.png,height=100)]]
[[ImageLink(rainbow.png,height=100)]]
[[ImageLink(spring.png,height=100)]]
[[ImageLink(summer.png,height=100)]]
[[ImageLink(white.png,height=100)]]
[[ImageLink(winter.png,height=100)]]

public: howto/colormaps (last edited 2011-03-30 16:18:00 by localhost)