Re: Color table from TextAdapter

Hi Don, hi all,

sorry for the delay on this.

Don Murray wrote:
Hi Ugo-

Ugo Taddei wrote:

Hello everyone,

I want to load a user-defined color table from a text file. For that I want to define the color table as a VisAD Data object and read it off from a text file using VisAD's TextAdapter. (Actually, there's no need to define anything here, because the data object will be automagically created by the adapter...and I just want to get the RGB values as float[3][ N ].)

Has anyone got a snippet of code (that does exactly that) to donate to an open source project?

We define color tables using XML, but that entails adapting some
of our other code.  Although Bill doesn't officially endorse it,

Yes, I've seen it in your IDV and found that very neat. But for my little project it's a bit of overkill to do it with XML.

there is a setFunction method of BaseColorControl.  So, you
maybe could define your text file to be a FlatField of
(index -> (r, g, b)) and then use the setFunction method.
(I'll let you work out the format of the file. ;-))

Nah, that wasn't the hard stuff. Hard is to find why the attached code is not working...

I'm attaching a test program and a color table. The program creates some 2D data, displays with rgb and then pops up a file chooser for the user to load a color table. As said, the table comes free with this email and looks like this:

r g b
1 1 1
1 0 0
0 1 0
0 0 1
0 0 1
1 0 1
1 1 0
0 0 0

It's a white, r,g,b,c,m,y, black table. The problem is that the color control (or whatever!) ignores all colors but the initial white (1,1,1) and red (1,0,0).

Note: colors must be between 0.0 and 1.0 (as specified in the documentation).

Can anyone tell me what's wrong or what I'm doing wrong?



I think Donna Gresh has used this method successfully.

Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307

import javax.swing.JFrame;
import javax.swing.JFileChooser;
import visad.*;
import visad.java2d.*;

public class TestColorTable extends JFrame {

  public TestColorTable()
    throws Exception{

    // create some data

    float[][] values = new float[16][16];
    for (int j = 0; j < values.length; j++)
      for (int i = 0; i < values[0].length; i++) {
        values[j][i] = (float)Math.random();

    FlatField ff = visad.util.DataUtility.makeImage(values);

    // create display
    DisplayImpl display = new DisplayImplJ2D("display");

    ScalarMap[] maps = ff.getType().guessMaps(false);

    for (int i = 0; i < maps.length; i++) {

    display.addMap(new ScalarMap( 
    display.addMap(new ScalarMap( 

    ScalarMap rgbMap = new ScalarMap( 

    // data reference
    DataReferenceImpl dataRef = new DataReferenceImpl("data");

    // show display in frame

    // load a new table from file
    Function f = null;

    // ...choose file...
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File("."));

    int returnVal = fc.showOpenDialog(new JFrame());

    if (returnVal == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile();
        System.out.println("Opening: " + file.getAbsolutePath() );

        try {
          String fileName = file.getAbsolutePath();

          // here is where the hard work is done
          f = (Function) loadColorTable(fileName);

        }catch (Exception ex) {
    } else {
        System.out.println("Open command cancelled by user.");

    // now try to set the new function...
    if(f != null){

      ColorControl cc = (ColorControl)rgbMap.getControl();
System.out.println("cc " + cc);



  public Function loadColorTable(String s)
    throws Exception{

    Data d = new;


    return (Function)d;

  public static void main(String[] args)
  throws Exception{
    TestColorTable testColorTable1 = new TestColorTable();
r g b
1 1 1
1 0 0
0 1 0
0 0 1
0 0 1
1 0 1
1 1 0
0 0 0
  • 2003 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: