/* -------------------------------------------------------------------------
 * $Id: UcarColorTable.java 1318 2018-12-18 16:24:57Z frup47043 $
 * Origin  : GFI INFORMATIQUE
 * Project : 3Dview IMPEx
 * -------------------------------------------------------------------------
 * HISTORY
 * VERSION : 24/02/2014 : LBE
 * Creation
 * END-HISTORY
 * -------------------------------------------------------------------------
 */
package org.fxyz3d.scene.paint;

 

import java.awt.Color;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Scanner;
import java.util.logging.Logger;
import org.fxyz3d.scene.paint.Palette.ColorPalette;


/**
 * A generic ucar table loader.
 * Available tables are at http://www.ncl.ucar.edu/Document/Graphics/color_table_gallery.shtml
 * @author LBE
 * @version 1.0
 */
public class UcarColorTable implements IColorTable, Serializable {
    private static final Logger LOGGER = Logger.getLogger(UcarColorTable.class
            .getName());
    //public final String COLOR_TABLE_LOCATION = "/fr/tlse/irap/controller/tools/color";
    public final String COLOR_TABLE_LOCATION = "/org/fxyz3d/scene/paint";
    private double min,max;
    private boolean invert;
    private int nbColors;
    private Color colorTable[];
    
    public UcarColorTable(double min, double max, ColorPalette pType) {
        this.min = min;
        this.max = max;
        loadColorTable(pType);
    }

    /**
     * Set to true to invert table colors;
     * @param pInvert 
     */
    public void invertColors(boolean pInvert) {
        invert = pInvert;
    }
    
    @Override
    public Color getColor(double pValue) {
        //System.out.println("********** Get Color with "+pValue+" from "+min+" to "+ max);
        if (Double.isNaN(pValue)) {
            return new Color(1, 1, 1, 0);
        } else {
            int colValue = (int) ((pValue - min) * (nbColors - 1) / (max - min));
            if (colValue >= nbColors) {
                colValue = (nbColors - 1);
            }
            if (colValue < 0) {
                colValue = 0;
            }
            if(invert) {
                return colorTable[colorTable.length-colValue-1];
            }
            return colorTable[colValue];

        }

    }
    
  
    /**
     * @return the min
     */
    @Override
    public double getMin() {
        return min;
    }

    /**
     * @param min the min to set
     */
    @Override
    public void setMin(double min) {
        this.min = min;
    }

    /**
     * @return the max
     */
    @Override
    public double getMax() {
        return max;
    }

    /**
     * @param max the max to set
     */
    @Override
    public void setMax(double max) {
        this.max = max;
    }

    
    private void loadColorTable(ColorPalette lType) {
            
            
            InputStream lIs = getClass().getResourceAsStream(
                    COLOR_TABLE_LOCATION+"/"+lType.name()+".rgb");
            //System.out.println("Url = "+COLOR_TABLE_LOCATION+"/"+lType.name()+".rgb");
//            if(lCTPath.toString().startsWith("jar")) {
//                lCTPath = ((JarURLConnection)lCTPath.openConnection()).getJarFileURL();
//            }
//            File lFile = new File(lCTPath.toURI());
            
            
            
            Scanner lScan = new Scanner(lIs);
            boolean startColorsScan = false;
            int colorIdx=0;
            while (lScan.hasNext()) {
                String lLine = lScan.nextLine().trim();
                if (lLine.startsWith("ncolors")) {
                    nbColors = Integer.valueOf(lLine.split("=")[1].trim());
                    //LOGGER.info("Found a "+nbColors+" color table for "+lType.name()+".");
                    colorTable = new Color[nbColors];
                    startColorsScan = true;
                    continue;//go to next record
                } 
                if(startColorsScan && !lLine.trim().startsWith("#")) {
                    String values[] = lLine.trim().split("\\s+");
                    if (values.length >= 3) {
                        colorTable[colorIdx++] = new Color(                                
                                Integer.parseInt(values[0]),
                                Integer.parseInt(values[1]),
                                Integer.parseInt(values[2])
                        );
                    }
                }
                
                
                
            }
        
    }

    
    
//    
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
////        UcarColorTable lTable = new UcarColorTable(0, 255, UcarColorTableType.rainbow);
////        System.out.println("Color middle = " + lTable.getColor(100));
//        
//        
//        final JFrame lf = new JFrame("List of Ucar color tables");
//        lf.setLayout(new FlowLayout());
//        for(UcarColorTableType lType : UcarColorTableType.values()) {
//            UcarColorTable lTable = new UcarColorTable(0, 10, lType);
//            JPanel lJp = new JColorBarPanel(lTable, lType.name());
//            lJp.addMouseListener(new MouseListener() {
//
//                @Override
//                public void mouseClicked(MouseEvent e) {
//                    lf.dispose();
//                }
//
//                @Override
//                public void mousePressed(MouseEvent e) {}
//                @Override
//                public void mouseReleased(MouseEvent e) {}
//                @Override
//                public void mouseEntered(MouseEvent e) {}
//                @Override
//                public void mouseExited(MouseEvent e) {}
//            })
//        ;
//            lJp.setPreferredSize(new Dimension(100,150));
//            lf.add(lJp);
//        }
//        lf.pack();
//        lf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//        lf.setVisible(true);
//
//        
//    }
    
}

