Colormaps

ILPanels hosts all kind of graphs to visualize numerical data as lines, dots or surfaces etc. Often those graphs also use colors to visualize corresponding values. The way numerical values are mapped to a color is controled by a lookup table stored in the ILPanel control. Such colormaps define a fixed number of colors which are used to map the values from numerical range into colorspace.

Colormaps are realized by the class ILColormap. ILNumerics.Drawing comes with a range of predefined maps. It as also possible to define your own map and/or alter existing colormaps at runtime.

Since the current colormap is global for all graphs contained in the panel, changes to the current colormap will immediately be reflected by all existing graphs.

Predefined colormaps

ILNumerics.Drawing provides the Colormaps enumeration in the ILNumerics.Drawing.Misc namespace. Those values can be used to easily define most common colormappings. The Colormaps enumeration is mostly compatible with the colormappings used in Matlab.

The following lists displays a simple Möbius surface for all predefined colormaps.

Code used to produce the graphs above:

ILSubfigure m_subfigure;
private void Form1_Load(object sender, EventArgs e) {
    m_subfigure = new ILSubfigure(); 
    m_subfigure.Dock = DockStyle.Fill; 
    Controls.Add(m_subfigure); 
    this.Width = 320; 
    this.Height = 330; 
    this.Refresh(); 

    m_subfigure.ColorBar.Dock = DockStyle.Bottom;  
    m_subfigure.Title.Visible = true; 
    m_subfigure.Title.BackColor = Color.LightGray; 
    m_subfigure.Title.ForeColor = Color.DarkBlue; 

    Bitmap bmp = new Bitmap(m_subfigure.Width,m_subfigure.Height); 
    ILArray<double> X,Y,Z; 
    ILSpecialData.moebius(15,5,10,out X, out Y, out Z); 
    m_subfigure.Panel.Graphs.Clear(); 
    ILSurfaceGraph surf = m_subfigure.Panel.Graphs.AddSurfGraph(X,Y,Z,null);
    surf.Opacity = 100.0f;
    surf.Shading = ShadingStyles.Flat; 
    surf.Wireframe.Width = 1;
    surf.Wireframe.Color = Color.DarkSalmon;

    foreach (Colormaps cm in Enum.GetValues(typeof(Colormaps))) {
        try {
        if (cm == Colormaps.Colorcube) continue; 
        string cmName = Enum.GetName(typeof(Colormaps),cm);
        m_subfigure.Title.Text = "Colormap: " + cm; 
        m_subfigure.Panel.Colormap = new ILColormap(cm); 
        m_subfigure.DrawToBitmap(bmp,
                    new Rectangle(0,0,m_subfigure.Width,m_subfigure.Height));
        bmp.Save("Colormap_"+cmName+".bmp",
                    System.Drawing.Imaging.ImageFormat.Png); 
        } catch (Exception) {}
    }
}

Download the complete example as VS2005 solution here.

Modifying colormaps

Colormaps can also get retrieved from the panel.Colormap property. The class only provides 2 members: Length: the number of colors the color table is based on, and Data which gives access to the underlying data of the table. Note, for get access, only a reference of the internal data is returned. It therefore is not possible to directly alter the table entries by using ILArray's set indexer. In order to alter some values of the table, one can retrieve the complete map, alter it outside and store it back using the set access of the Colorbar.Data property. In the following snippet, the colormap Colormap.Pink is altered to contain an area of white:
ILArray cm = new ILColormap(Colormaps.Pink).Data; 
// Pink has 128 rows. set indices 20..30 to white
cm["20:30;:"] = 1f; 
panel.Colormap.Data = cm; 

Colormaps contain one row of 3 values for the R,G and B parts of a color. The number of rows is not fixed. Values to be mapped into a color will linearily interpolated between both nearest colors in the table. Values for the RGB triples must be in range 0..1.


Valid CSS! Valid XHTML 1.0 Transitional