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:

(index->(r,g,b))
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?

Thanks,

Ugo

I think Donna Gresh has used this method successfully.

Don
*************************************************************
Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307
http://www.unidata.ucar.edu/staff/donm
*************************************************************




import javax.swing.JFrame;
import javax.swing.JFileChooser;
import java.io.File;
import visad.*;
import visad.java2d.*;

public class TestColorTable extends JFrame {

  public TestColorTable()
    throws Exception{
    super("TestColorTable");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // 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++) {
      System.out.println(maps[i]);
      //display.addMap(maps[i]);
    }

    display.addMap(new ScalarMap( 
RealType.getRealType("ImageElement"),Display.XAxis));
    display.addMap(new ScalarMap( 
RealType.getRealType("ImageLine"),Display.YAxis));

    ScalarMap rgbMap = new ScalarMap( 
RealType.getRealType("ImageRadiance"),Display.RGB);
    display.addMap(rgbMap);

    // data reference
    DataReferenceImpl dataRef = new DataReferenceImpl("data");
    dataRef.setData(ff);
    display.addReference(dataRef);

    // show display in frame
    getContentPane().add(display.getComponent());
    pack();
    setVisible(true);


    // 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) {
        java.io.File 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) {
          ex.printStackTrace();
        }
    } 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);

      cc.setFunction(f);

    }
  }

  public Function loadColorTable(String s)
    throws Exception{

    Data d = new visad.data.text.TextAdapter(s).getData();

    visad.jmet.DumpType.dumpDataType(d,System.out);

    return (Function)d;
  }


  public static void main(String[] args)
  throws Exception{
    TestColorTable testColorTable1 = new TestColorTable();
  }
}
(index->(r,g,b))
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: