Class ColorMap<T>

Object
ColorMap<T>
Type Parameters:
T - type of color object: String or MeshLambertMaterial for GWT and Color for JRE
Direct Known Subclasses:
ColorMap.Gradient, ColorMap.Index, ColorMap3D, ColorMapCSS

public abstract class ColorMap<T> extends Object
Interface for mapping data to colors.

Colors are handled very differently in JRE and GWT as well as for HTML canvas'es and WebGL. This class provides a unified interface, which hides the implementation details.

Note:

  1. the interface provides a number of useful methods to blend colors.
  2. the implementation of all methods is optional because not all may be adequate for every data-to-color mapping.
  3. the translate(...) methods fill in arrays of type Object[] or return Object's, respectively. This ambiguity is required to render the interface agnostic to the different implementations.
  4. Color is emulated in GWT and can be used to set and manipulate colors but needs to be converted to to CSS (for canvas) or to Material (for WebGL), respectively, before applying to graphics or the GUI.
Author:
Christoph Hauert
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Abstract super class for color maps following a gradient.
    static class 
    One dimensional color gradient spanning two or more colors
    static class 
    Two dimensional color gradient with one color for each dimension.
    static class 
    Color gradient for N dimensional data with one color for each dimension.
    static class 
    Color gradient following the hue.
    static class 
    Associates integer indices with colors.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final double
    Useful constant for converting int representations of color channels (0-255) to floating point representations (0-1).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new color map.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Color[]
    addAlpha(Color[] colors, int alpha)
    Utility method to add (or override) the transparency in the array color.
    static Color
    addAlpha(Color color, int alpha)
    Utility method to add (or override) the transparency of color color.
    static Color
    addColors(Color one, Color two)
    Utility method for adding the red, green, blue and alpha components of two colors.
    static Color
    blendColors(Color[] colors, double[] weights)
    Utility method for the smooth, component-wise blending of multiple colors with respective weights (includes alpha-channel).
    static Color
    blendColors(Color one, Color two, double w1)
    Utility method for the smooth, component-wise blending of two colors, where color one has weight w1 (and color two has weight (1-w1)) and includes the alpha-channel.
    abstract T
    Utility method to convert the color into a color object of type T.
    translate(double[] data)
    Translate the multi-trait double[] array data to a color.
    boolean
    translate(double[][] data1, double[][] data2, T[] color)
    Translate the data1 and data2 arrays of double[] multi-trait values to colors and store the results in the color array.
    boolean
    translate(double[][] data, T[] color)
    Translate the data array of double[] multi-trait values to colors and store the results in the color array.
    boolean
    translate(double[][] data, T[] color, int dep)
    Translate the data array of double[] multi-trait values with dependent trait dep to colors and store the results in the color array.
    boolean
    translate(double[] data, T[] color)
    Translate the data array of double values to colors and store the results in the color array.
    boolean
    translate(int[] data, T[] color)
    Translate the data array of int values to colors and store the results in the color array.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INV255

      protected static final double INV255
      Useful constant for converting int representations of color channels (0-255) to floating point representations (0-1).
      See Also:
  • Constructor Details

    • ColorMap

      public ColorMap()
      Constructs a new color map.
  • Method Details

    • translate

      public boolean translate(int[] data, T[] color)
      Translate the data array of int values to colors and store the results in the color array. The type of the color array depends on the implementation.

      Note: for performance reasons no validity checks are performed on the data. If any data entry is negative or >nColors an ArrayIndexOutOfBoundsException is thrown.

      Parameters:
      data - the int[] array to convert to colors
      color - the array for the resulting colors
      Returns:
      true if translation successful
    • translate

      public T translate(double[] data)
      Translate the multi-trait double[] array data to a color. The type of object returned depends on the implementation.
      Parameters:
      data - the double[] array to convert to a color
      Returns:
      the color object
    • translate

      public boolean translate(double[] data, T[] color)
      Translate the data array of double values to colors and store the results in the color array. The type of the color array depends on the implementation.

      Note: whether data refers to a single or multiple traits is up to the implementation to decide. For example, CXPopulation stores multiple traits in a linear array.

      Parameters:
      data - the double[] array to convert to colors
      color - the array for the resulting colors
      Returns:
      true if translation successful
    • translate

      public boolean translate(double[][] data, T[] color)
      Translate the data array of double[] multi-trait values to colors and store the results in the color array. The type of the color array depends on the implementation.
      Parameters:
      data - the double[][] array to convert to colors
      color - the array for the resulting colors
      Returns:
      true if translation successful
    • translate

      public boolean translate(double[][] data, T[] color, int dep)
      Translate the data array of double[] multi-trait values with dependent trait dep to colors and store the results in the color array. The type of the color array depends on the implementation.
      Parameters:
      data - the double[] array to convert to colors
      color - the array for the resulting colors
      dep - index of dependent trait
      Returns:
      true if translation successful
    • translate

      public boolean translate(double[][] data1, double[][] data2, T[] color)
      Translate the data1 and data2 arrays of double[] multi-trait values to colors and store the results in the color array. The type of the color array depends on the implementation.

      For example, frequencies and fitnesses, say data1 and data2, respectively, yield the average fitness as data1·data2 (dot product), which then gets converted to a color.

      Parameters:
      data1 - the first double[] array to convert to colors
      data2 - the second double[] array to convert to colors
      color - the array for the resulting colors
      Returns:
      true if translation successful
    • addColors

      public static Color addColors(Color one, Color two)
      Utility method for adding the red, green, blue and alpha components of two colors.
      Parameters:
      one - the first color
      two - the second color
      Returns:
      component-wise addition of the two colors
    • addAlpha

      public static Color addAlpha(Color color, int alpha)
      Utility method to add (or override) the transparency of color color.
      Parameters:
      color - the color to add transparency
      alpha - the new transparency (0: opaque, 255: transparent)
      Returns:
      the new translucent color
    • addAlpha

      public static Color[] addAlpha(Color[] colors, int alpha)
      Utility method to add (or override) the transparency in the array color.
      Parameters:
      colors - the color array to add transparency
      alpha - the new transparency (0: opaque, 255: transparent)
      Returns:
      the new translucent color
    • blendColors

      public static Color blendColors(Color one, Color two, double w1)
      Utility method for the smooth, component-wise blending of two colors, where color one has weight w1 (and color two has weight (1-w1)) and includes the alpha-channel.
      Parameters:
      one - the first color
      two - the second color
      w1 - the weight of color one
      Returns:
      component-wise addition of the two colors
    • blendColors

      public static Color blendColors(Color[] colors, double[] weights)
      Utility method for the smooth, component-wise blending of multiple colors with respective weights (includes alpha-channel).
      Parameters:
      colors - the colors for blending
      weights - the weights of each color
      Returns:
      component-wise blending of colors
    • color2Color

      public abstract T color2Color(Color color)
      Utility method to convert the color into a color object of type T.
      Parameters:
      color - the color to convert
      Returns:
      the color represented as an object of type T