View Javadoc

    1   package baseCode.gui;
    2
    3   import java.awt.Color;
    4
    5   /***
    6    * <p>
    7    * Title: ColorMap
    8    * </p>
    9    * <p>
    10   * Description: contains predefined color maps for visualization and color palette methods
    11   * </p>
    12   * <p>
    13   * Copyright (c) 2004
    14   * </p>
    15   * <p>
    16   * Institution:: Columbia University
    17   * </p>
    18   *
    19   * @author Will Braynen
    20   * @version $Id: ColorMap.java,v 1.7 2004/07/27 03:18:58 pavlidis Exp $
    21   */
    22
    23  public class ColorMap {
    24
    25     /*** first color in the current color map */
    26     protected Color m_minColor;
    27     /*** last color in the current color map */
    28     protected Color m_maxColor;
    29     protected Color[] m_customColorMap;
    30
    31     public static final int m_defaultSuggestedNumberOfColors = 64;
    32     public static final Color DARK_RED = new Color( 128, 0, 0 );
    33     public static final Color[] GREENRED_COLORMAP = {
    34           Color.green, Color.black, Color.red
    35     };
    36     public static final Color[] REDGREEN_COLORMAP = {
    37           Color.red, Color.black, Color.green
    38     };
    39     public static final Color[] BLACKBODY_COLORMAP = {
    40           Color.black, DARK_RED, Color.orange, Color.yellow, Color.white
    41     };
    42
    43     protected Color[] m_currentColorMap = GREENRED_COLORMAP; // reference to a
    44     // color map
    45     protected Color[] m_colorPalette;
    46
    47     public ColorMap() {
    48
    49        this( m_defaultSuggestedNumberOfColors );
    50     }
    51
    52     public ColorMap( int suggestedNumberOfColors ) {
    53
    54        this( suggestedNumberOfColors, GREENRED_COLORMAP );
    55     }
    56
    57     public ColorMap( Color[] colorMap ) {
    58
    59        this( m_defaultSuggestedNumberOfColors, colorMap );
    60     }
    61
    62     /***
    63      * Pre-condition: suggestedNumberOfColors > colorMap.length
    64      *
    65      * @param suggestedNumberOfColors int
    66      * @param colorMap Color[]
    67      */
    68     public ColorMap( int suggestedNumberOfColors, Color[] colorMap ) {
    69
    70        m_currentColorMap = colorMap;
    71        m_colorPalette = createColorPalette( suggestedNumberOfColors, colorMap );
    72     }
    73
    74     /***
    75      * Calculate how fast we have to change color components. Assume min and max colors are different!
    76      *
    77      * @param minColor red, green, or blue component of the RGB color
    78      * @param maxColor red, green, or blue component of the RGB color
    79      * @param totalColors int
    80      * @return positive or negative step size
    81      */
    82     protected int getStepSize( int minColor, int maxColor, int totalColors ) {
    83
    84        int colorRange = maxColor - minColor;
    85        double stepSize = colorRange / ( 1 == totalColors ? 1 : totalColors - 1 );
    86        return ( int ) Math.round( stepSize );
    87     }
    88
    89     /***
    90      * Allocates colors across a range.
    91      *
    92      * @param suggestedNumberOfColors palette resolution; if colorPalette.length does not evenly divide into this number,
    93      *        the actual number of colors in the palette will be rounded down.
    94      * @param colorMap the simplest color map is { minColor, maxColor }; you might, however, want to go through
    95      *        intermediate colors instead of following a straight-line route through the color space.
    96      * @return Color[] the color palette
    97      */
    98     protected Color[] createColorPalette( int suggestedNumberOfColors,
    99           Color[] colorMap ) {
    100
    101       Color[] colorPalette;
    102       Color minColor;
    103       Color maxColor;
    104
    105       // number of segments is one less than the number of points
    106       // dividing the line into segments; the color map contains points,
    107       // not segments, so for example, if the color map is trivially
    108       // { minColor, maxColor }, then there is only one segment
    109       int totalSegments = m_currentColorMap.length - 1;
    110
    111       // allocate colors across a range; distribute evenly
    112       // between intermediate points, if there are any
    113       int colorsPerSegment = suggestedNumberOfColors / totalSegments;
    114
    115       // make sure all segments are equal by rounding down
    116       // the total number of colors if necessary
    117       int totalColors = totalSegments * colorsPerSegment;
    118
    119       // create color map to return
    120       colorPalette = new Color[totalColors];
    121
    122       for ( int segment = 0; segment < totalSegments; segment++ ) {
    123          // the minimum color for each segment as defined by the current color
    124          // map
    125          minColor = colorMap[segment];
    126          int r = minColor.getRed();
    127          int g = minColor.getGreen();
    128          int b = minColor.getBlue();
    129
    130          // the maximum color for each segment and the step sizes
    131          maxColor = colorMap[segment + 1];
    132          int redStepSize = getStepSize( r, maxColor.getRed(), colorsPerSegment );
    133          int greenStepSize = getStepSize( g, maxColor.getGreen(),
    134                colorsPerSegment );
    135          int blueStepSize = getStepSize( b, maxColor.getBlue(),
    136                colorsPerSegment );
    137
    138          for ( int k, i = 0; i < colorsPerSegment; i++ ) {
    139             // clip
    140             r = Math.min( r, 255 );
    141             g = Math.min( g, 255 );
    142             b = Math.min( b, 255 );
    143
    144             // but also make sure it's not less than zero
    145             r = Math.max( r, 0 );
    146             g = Math.max( g, 0 );
    147             b = Math.max( b, 0 );
    148
    149             k = segment * colorsPerSegment + i;
    150             colorPalette[k] = new Color( r, g, b );
    151
    152             r += redStepSize;
    153             g += greenStepSize;
    154             b += blueStepSize;
    155          }
    156       }
    157
    158       return colorPalette;
    159
    160    } // end createColorPalette
    161
    162    public Color getColor( int i ) {
    163
    164       return m_colorPalette[i];
    165    }
    166
    167    public Color[] getPalette() {
    168
    169       return m_colorPalette;
    170    }
    171
    172    /***
    173     * @return the number of colors in the palette
    174     */
    175    public int getPaletteSize() {
    176
    177       return m_colorPalette.length;
    178    }
    179 }