Class JGradientColorMap

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public final class JGradientColorMap extends JPanel
The JGradientColorMap class provides a gradient colour map.

A gradient colour map provides a visual display of a bar with a certain specified spectrum:

Bone:

Copper:

Discontinuous blue-white-green:

Discontinuous dark-red-yellow:

Black and white:

Gray scale:

Gray scale (trimmed):

Green-red diverging:

Hot:

Jet:

Hue/saturation/brightness (HSB):

Separated red/green/blue:

Red:

Green:

Blue:

Yellow:

Cyan:

Magenta:

Ultralight pastel:

Light pastel:

Dark pastel:

Greens:

Blues:

Yellow browns:

Violet purples:

Deep space:

Blue-white:

Custom:
Dependent on the colours specified.

Random:
A random selection of colours.

A gradient colour map can have four orientations (see JGradientColorMap.EOrientation):

  • horizontal left to right (tick marks are supported),
  • horizontal right to left (tick marks are supported),
  • vertical bottom to top
  • and vertical top to bottom.

This class has also one method that can be used to derive a Color that is linearly interpolated across the specified spectrum:

Color interpolatedColor = myGradientColorMap.interpolate(0.6);

This corresponds to the following interpolation scheme (dependent on the colour map):

The value can also be indicated on the colour map itself.

All documentation is written in British English, except for the API-code, which was kept in American English for compatability with the Java API interface.

Note that this class cannot be subclassed!

Version:
01/11/2016
Author:
Sven Maerivoet
See Also:
  • Constructor Details

    • JGradientColorMap

      public JGradientColorMap()
      Constructs a JGradientColorMap object.

      The gradient colour map has by default a horizontal orientation (going from left to right) with a width of 100 pixels and a height of 20 pixels; the jet colour map is used by default.

    • JGradientColorMap

      public JGradientColorMap(JGradientColorMap.EOrientation orientation, int width, int height)
      Constructs a JGradientColorMap object with the specified orientation and size.

      The jet colour map is used by default.

      Parameters:
      orientation - the orientation of the gradient colour map (JGradientColorMap.EOrientation)
      width - the width of the gradient colour map (expressed in pixels)
      height - the height of the gradient colour map (expressed in pixels)
    • JGradientColorMap

      public JGradientColorMap(JGradientColorMap.EColorMap colorMap)
      Constructs a JGradientColorMap object with a specified colour map.

      The gradient colour map has by default a horizontal orientation (going from left to right) with a width of 100 pixels and a height of 20 pixels.

      Parameters:
      colorMap - the colour map to use
    • JGradientColorMap

      public JGradientColorMap(JGradientColorMap.EOrientation orientation, JGradientColorMap.EColorMap colorMap, int width, int height)
      Constructs a JGradientColorMap object with the specified orientation and size.
      Parameters:
      orientation - the orientation of the gradient colour map (JGradientColorMap.EOrientation)
      colorMap - the colour map to use
      width - the width of the gradient colour map (expressed in pixels)
      height - the height of the gradient colour map (expressed in pixels)
  • Method Details

    • setColorMap

      public void setColorMap(JGradientColorMap.EColorMap colorMap)
      Changes the colour map that is used.
      Parameters:
      colorMap - the colour map to use
    • setRandomColorMap

      public void setRandomColorMap(int nrOfRandomColors)
      Sets up a random colour map.
      Parameters:
      nrOfRandomColors - the number of random colours to be used
    • getColorMap

      public JGradientColorMap.EColorMap getColorMap()
      Returns the colour map that is used.
      Returns:
      the colour map that is used
    • setTickMarks

      public void setTickMarks(double lowerTickValue, String lowerTickValuePrefix, double higherTickValue, String higherTickValuePrefix, String tickValuePrefix, String tickValueSuffix, int nrOfTickMarks, int nrOfDecimals)
      Sets the optional tick marks for the gradient colour map.

      Note that these tick marks only appear on horizontally-oriented gradient colour maps.

      Parameters:
      lowerTickValue - the value associated with the left colour
      lowerTickValuePrefix - the prefix label for the value associated with the left colour
      higherTickValue - the value associated with the right colour
      higherTickValuePrefix - the prefix label for the value associated with the right colour
      tickValuePrefix - the prefix label for each value
      tickValueSuffix - the suffix label for each value
      nrOfTickMarks - the number of tick marks to produce
      nrOfDecimals - the number of decimals to retain in the values beneath the tick marks
    • indicateValue

      public void indicateValue(double value)
      Instructs to indicate the specified value
      Parameters:
      value - the value to indicate
    • enableValueIndication

      public void enableValueIndication()
      Enables the indication of values.
    • disableValueIndication

      public void disableValueIndication()
      Disables the indication of values.
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • setMinimumSize

      public void setMinimumSize(Dimension dimension)
      Overrides:
      setMinimumSize in class JComponent
    • getMaximumSize

      public Dimension getMaximumSize()
      Overrides:
      getMaximumSize in class JComponent
    • setMaximumSize

      public void setMaximumSize(Dimension dimension)
      Overrides:
      setMaximumSize in class JComponent
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • setPreferredSize

      public void setPreferredSize(Dimension dimension)
      Overrides:
      setPreferredSize in class JComponent
    • paintComponent

      public void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • setCustomColorMapComponent

      public void setCustomColorMapComponent(int id, double level, Color color)
      Adds or updates a component in the custom colour map.
      Parameters:
      id - the ID of the component
      level - the level of the component (between 0.0 and 1.0)
      color - the Color to associate with the component
    • removeCustomColorMapComponent

      public void removeCustomColorMapComponent(int id)
      Removes a component from the custom colour map.
      Parameters:
      id - the ID of the component
    • clearAllCustomColorMapComponents

      public void clearAllCustomColorMapComponents()
      Clears all components from the custom colour map.
    • setAllCustomColorMapComponents

      public void setAllCustomColorMapComponents(TreeMap<Integer,JGradientColorMap.CustomColorMapComponent> colorMapComponents)
      Sets all components of the custom colour map.
      Parameters:
      colorMapComponents - a TreeMap containing all the custom colour map components
    • getAllCustomColorMapComponents

      public TreeMap<Integer,JGradientColorMap.CustomColorMapComponent> getAllCustomColorMapComponents()
      Returns all components of the custom colour map.
      Returns:
      a TreeMap containing all the custom colour map components
    • convertToComponents

      public TreeMap<Integer,JGradientColorMap.CustomColorMapComponent> convertToComponents(int nrOfComponents)
      Converts the current colour map to a specified number of components.
      Parameters:
      nrOfComponents - the specified number of components
      Returns:
      the converted colour map
    • plainTextLoadRandomColorMapComponents

      public final void plainTextLoadRandomColorMapComponents(TextFileParser tfp) throws FileParseException
      Loads the random colour map's components from a plain-text file.
      Parameters:
      tfp - a reference to the file parser
      Throws:
      FileParseException - in case a parse error occurs
    • streamLoadRandomColorMapComponents

      public final void streamLoadRandomColorMapComponents(DataInputStream dataInputStream) throws IOException
      Loads the random colour map's components from a file as a stream.
      Parameters:
      dataInputStream - a data inputstream
      Throws:
      IOException - in case a parse error occurs
    • plainTextSaveRandomColorMapComponents

      public final void plainTextSaveRandomColorMapComponents(TextFileWriter tfw) throws FileWriteException
      Saves the random colour map's components to a plain-text file.
      Parameters:
      tfw - a reference to the file writer
      Throws:
      FileWriteException - in case a write error occurs
    • streamSaveRandomColorMapComponents

      public final void streamSaveRandomColorMapComponents(DataOutputStream dataOutputStream) throws IOException
      Saves the random colour map's components to a file as a stream.
      Parameters:
      dataOutputStream - a data outputstream
      Throws:
      IOException - in case a write error occurs
    • plainTextLoadCustomColorMapComponents

      public final void plainTextLoadCustomColorMapComponents(TextFileParser tfp) throws FileParseException
      Loads the custom colour map's components from a plain-text file.
      Parameters:
      tfp - a reference to the file parser
      Throws:
      FileParseException - in case a parse error occurs
    • streamLoadCustomColorMapComponents

      public final void streamLoadCustomColorMapComponents(DataInputStream dataInputStream) throws IOException
      Loads the custom colour map's components from a file as a stream.
      Parameters:
      dataInputStream - a data inputstream
      Throws:
      IOException - in case a parse error occurs
    • plainTextSaveCustomColorMapComponents

      public final void plainTextSaveCustomColorMapComponents(TextFileWriter tfw) throws FileWriteException
      Saves the custom colour map's components to a plain-text file.
      Parameters:
      tfw - a reference to the file writer
      Throws:
      FileWriteException - in case a write error occurs
    • streamSaveCustomColorMapComponents

      public final void streamSaveCustomColorMapComponents(DataOutputStream dataOutputStream) throws IOException
      Saves the custom colour map's components to a plain-text file.
      Parameters:
      dataOutputStream - a data outputstream
      Throws:
      IOException - in case a write error occurs
    • interpolate

      public Color interpolate(double u)
      Derives a Color that is linearly interpolated across a spectrum.

      Note that the value of u is clipped in the interval [0,1].

      Parameters:
      u - the value to use when interpolating the spectrum
      Returns:
      a linearly interpolated value across a spectrum