ColorMap.java

Go to the documentation of this file.
00001 /*
00002  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
00003  * Copyright (C) 2007 - INRIA - Jean-Baptiste Silvy
00004  * desc : Class to handle Scilab colormaps
00005  * 
00006  * This file must be used under the terms of the CeCILL.
00007  * This source file is licensed as described in the file COPYING, which
00008  * you should have received as part of this distribution.  The terms
00009  * are also available at    
00010  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
00011  *
00012  */
00013 
00014 package org.scilab.modules.renderer.utils;
00015 
00016 
00021 public class ColorMap {
00022         
00023         private static final String COMMA = ", ";
00024         
00026         private static final int NBCOL = 3;
00027         
00028         private static final int BLACK_INDEX = -1;
00029         private static final int WHITE_INDEX = -2;
00030         
00032         private double[] redChannel;
00034         private double[] greenChannel;
00036         private double[] blueChannel;
00038         private int colormapSize;
00039         
00043         public ColorMap() {
00044           redChannel   = null;
00045           greenChannel = null;
00046           blueChannel  = null;
00047           colormapSize = 0;
00048         }
00049         
00054         public static ColorMap create() {
00055                 return new ColorMap();
00056         }
00057         
00063         public void getColor(int colorIndex, double[] color) {
00064                 color[0] = getRedChannel(colorIndex);
00065                 color[1] = getGreenChannel(colorIndex);
00066                 color[2] = getBlueChannel(colorIndex);
00067         }
00068         
00074         public double[] getColor(int colorIndex) {
00075                 double[] res = new double[NBCOL];
00076                 getColor(colorIndex, res);
00077                 return res;
00078         }
00079         
00085         public double[][] getColors(int[] colorIndices) {
00086                 int nbIndices = colorIndices.length;
00087                 double[][] res = new double[nbIndices][];
00088                 for (int i = 0; i < nbIndices; i++) {
00089                         res[i] = getColor(colorIndices[i]);
00090                 }
00091                 return res;
00092         }
00093         
00098         public double getRedChannel(int colorIndex) {
00099                 return redChannel[clampColorIndex(colorIndex)];
00100         }
00101         
00106         public double getGreenChannel(int colorIndex) {
00107                 return greenChannel[clampColorIndex(colorIndex)];
00108         }
00109         
00114         public double getBlueChannel(int colorIndex) {
00115                 return blueChannel[clampColorIndex(colorIndex)];
00116         }
00117         
00125         public int clampColorIndex(int colorIndex) {
00126                 if (colorIndex <= 0) {
00127                         return 0;
00128                 } else if (colorIndex >= colormapSize + 1) {
00129                         return colormapSize + 1;
00130                 } else {
00131                         return colorIndex;
00132                 }
00133         }
00134         
00140         public void setData(double[] newData) {
00141                 
00142                 // new size
00143                 setSize(newData.length / NBCOL);
00144                 
00145                 // red channel
00146                 System.arraycopy(newData, 0, redChannel, 0, colormapSize);
00147                 
00148                 // green channel
00149                 System.arraycopy(newData, colormapSize, greenChannel, 0, colormapSize);
00150 
00151                 // blue channel
00152                 System.arraycopy(newData, 2 * colormapSize, blueChannel, 0, colormapSize);
00153 
00154         }
00155         
00161         public double[] getData() {
00162                 double[] res = new double[NBCOL * colormapSize];
00163                 getData(res);
00164                 return res;
00165         }
00166         
00172         public void getData(double[] data) {
00173                 // copy channels
00174                 System.arraycopy(redChannel  , 0, data,                0, colormapSize);
00175                 System.arraycopy(greenChannel, 0, data,     colormapSize, colormapSize);
00176                 System.arraycopy(blueChannel , 0, data, 2 * colormapSize, colormapSize);
00177         }
00178         
00183         public int getSize() {
00184                 return colormapSize;
00185         }
00186         
00191         public void setSize(int newSize) {
00192                 colormapSize = newSize;
00193                 redChannel = new double[colormapSize + 2]; // +2 for black and white
00194                 greenChannel = new double[colormapSize + 2]; // +2 for black and white
00195                 blueChannel = new double[colormapSize + 2]; // +2 for black and white
00196                 
00197                 // add black and whit colors
00198                 redChannel[colormapSize    ] = 0.0; // black
00199                 redChannel[colormapSize + 1] = 1.0; // white
00200                 
00201                 // green channel
00202                 greenChannel[colormapSize    ] = 0.0; // black
00203                 greenChannel[colormapSize + 1] = 1.0; // white
00204                 
00205                 // blue channel
00206                 blueChannel[colormapSize    ] = 0.0; // black
00207                 blueChannel[colormapSize + 1] = 1.0; // white
00208         }
00209         
00215         public void setColor(int index, double[] color) {
00216                 redChannel[index] = color[0];
00217                 greenChannel[index] = color[1];
00218                 blueChannel[index] = color[2];
00219         }
00220         
00226         public int convertScilabToColorMapIndex(int scilabIndex) {
00227                 if (scilabIndex == BLACK_INDEX || scilabIndex == 0) {
00228                         // black color
00229                         return colormapSize;
00230                 } else if (scilabIndex == WHITE_INDEX) {
00231                         // white color
00232                         return colormapSize + 1;
00233                 } else if (scilabIndex < WHITE_INDEX) {
00234                         // first index
00235                         return 0;
00236                 } else if (scilabIndex >= colormapSize) {
00237                         // last index
00238                         return colormapSize - 1;
00239                 } else {
00240                         return scilabIndex - 1;
00241                 }
00242         }
00243         
00247         public String toString() {
00248                 String res = "";
00249                 for (int i = 0; i < getSize() + 2; i++) {
00250                         res += "[" + redChannel[i] + COMMA + greenChannel[i] + COMMA + blueChannel[i] + "]\n";
00251                 }
00252                 return res;
00253         }
00254 }

Generated on Tue Sep 9 17:48:30 2008 for Scilab [trunk] by  doxygen 1.5.5