001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.colormap;
025    
026    import java.awt.Color;
027    
028    
029    public class ColorMapper {
030    
031            private double min = 0.0;
032    
033            private double max = 10.0;
034    
035            private double a = 10;
036    
037            private double b = 0;
038    
039            private ColorMap colorMap = null;
040    
041            private int colorCount = 0;
042    
043            public ColorMap getColorMap() {
044                    return colorMap;
045            }
046    
047            public void setColorMap(ColorMap colorMap) {
048                    this.colorMap = colorMap;
049                    this.colorCount = colorMap.size();
050                    this.a = colorCount / (max - min);
051                    this.b = -min * a;
052            }
053    
054            public double getMax() {
055                    return max;
056            }
057    
058            public void setMax(double max) {
059                    if (this.max >= max) {
060                            return;
061                    }
062                    if (max == this.min) {
063                            this.min = max - 10;
064                    }
065                    this.max = max;
066                    this.a = colorCount / (max - min);
067                    this.b = -min * a;
068            }
069    
070            public double getMin() {
071                    return min;
072            }
073    
074            public void setMin(double min) {
075                    if (this.min <= min) {
076                            return;
077                    }
078                    if (min == this.max) {
079                            this.max = min + 10;
080                    }
081                    this.min = min;
082                    this.a = colorCount / (max - min);
083                    this.b = -min * a;
084            }
085    
086            public ColorMapper() {
087                    colorMap = ColorMap.DEFAULT;
088                    colorCount = colorMap.size();
089                    setMax(1);
090                    setMin(0);
091            }
092    
093            public ColorMapper(double minValue, double maxValue) {
094                    this();
095                    setMax(maxValue);
096                    setMin(minValue);
097            }
098    
099            public Color fromDouble2(double v) {
100                    return colorMap.get(getIndex(v));
101            }
102    
103            public static final Color fromDouble(double v) {
104                    // inf = 255 255 0 yellow
105                    // 1 = 0 255 0 green
106                    // 0 = 0 0 0 black
107                    // -1 = 255 0 0 red
108                    // -inf = 255 0 255 magenta
109                    // nan = 0 255 255 cyan
110                    if (v == Double.MIN_VALUE || Double.isNaN(v))
111                            return (Color.MAGENTA);
112                    else if (Double.isInfinite(v))
113                            return (Color.CYAN);
114                    else if (v > 1.0)
115                            return (ColorMap.colorGreenToYellow[(int) (255.0 * Math.tanh((v - 1.0) / 10.0))]);
116                    else if (v > 0.0)
117                            return (ColorMap.colorBlackToGreen[(int) (255.0 * v)]);
118                    else if (v > -1.0)
119                            return (ColorMap.colorRedToBlack[(int) (255.0 * (v + 1.0))]);
120                    else
121                            return (ColorMap.colorRedToMagenta[(int) (255.0 * Math.tanh((-v - 1.0) / 10.0))]);
122            }
123    
124            public Color fromString(String s) {
125                    if (s == null)
126                            return Color.black;
127                    int hc = Math.abs(hash(s.hashCode()));
128                    int r = 192 + (hc % 256) / 4;
129                    hc = hc / 256;
130                    int g = 192 + (hc % 256) / 4;
131                    hc = hc / 256;
132                    int b = 192 + (hc % 256) / 4;
133                    return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
134            }
135    
136            private static int hash(int h) {
137                    h ^= (h >>> 20) ^ (h >>> 12);
138                    return h ^ (h >>> 7) ^ (h >>> 4);
139            }
140    
141            public Color fromObject(Object v) {
142                    if (v == null) {
143                            return Color.black;
144                    }
145                    if (v instanceof Double) {
146                            return fromDouble((Double) v);
147                    }
148                    return fromString(v.toString());
149            }
150    
151            private int getIndex(double v) {
152                    int index = (int) (v * a + b);
153                    return index;
154            }
155    
156    }