Class ColorModelFactory

java.lang.Object
org.apache.sis.internal.coverage.j2d.ColorModelFactory

public final class ColorModelFactory extends Object
A factory for ColorModel objects built from a sequence of colors.
Since:
1.0
Version:
1.3
Author:
Martin Desruisseaux (IRD, Geomatys), Johann Sorel (Geomatys), Alexis Manin (Geomatys)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Color
    The fully transparent color.
  • Method Summary

    Modifier and Type
    Method
    Description
    static ColorModel
    createColorScale(int dataType, int numBands, int visibleBand, double lower, double upper, Color... colors)
    Returns a color model interpolated for the given range of values.
    static ColorModel
    createGrayScale(int dataType, int numComponents, int visibleBand, double minimum, double maximum)
    Creates a color model for opaque images storing pixels as real numbers.
    static ColorModel
    createGrayScale(SampleModel model, int visibleBand, NumberRange<?> range)
    Creates a color model for opaque images storing pixels using the given sample model.
    createIndexColorModel(int numBands, int visibleBand, int[] ARGB, boolean hasAlpha, int transparent)
    Returns a tolerant index color model for the specified ARGB codes.
    static ColorModel
    createPiecewise(int dataType, int numBands, int visibleBand, Map<NumberRange<?>,Color[]> colors)
    Returns a color model interpolated for the ranges in the given map entries.
    static ColorModel
    createRGB(int bitsPerSample, boolean packed, boolean hasAlpha)
    Creates a RGB color model.
    static ColorModel
    Creates a RGB color model for the given sample model.
    createSubset(ColorModel cm, int[] bands)
    Creates a color model with only a subset of the bands of the given color model.
    boolean
    equals(Object other)
    Public as an implementation side-effect.
    static void
    expand(int[] colors, int[] ARGB, int lower, int upper)
    Copies colors into ARGB array from index lower inclusive to index upper exclusive.
    static void
    Appends a description of the given color space in the given buffer.
    static int
    getBitCount(int mapSize)
    Returns a bit count for an IndexColorModel mapping mapSize colors.
    int
    Public as an implementation side-effect.
    Returns a string representation for debugging purposes.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • TRANSPARENT

      public static final Color TRANSPARENT
      The fully transparent color.
  • Method Details

    • hashCode

      public int hashCode()
      Public as an implementation side-effect.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code.
    • equals

      public boolean equals(Object other)
      Public as an implementation side-effect.
      Overrides:
      equals in class Object
      Parameters:
      other - the other object to compare for equality.
      Returns:
      whether the two objects are equal.
    • createPiecewise

      public static ColorModel createPiecewise(int dataType, int numBands, int visibleBand, Map<NumberRange<?>,Color[]> colors)
      Returns a color model interpolated for the ranges in the given map entries. Returned instances of ColorModel are shared among all callers in the running virtual machine.
      Parameters:
      dataType - the color model type. One of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_FLOAT or DataBuffer.TYPE_DOUBLE.
      numBands - the number of bands for the color model (usually 1). The returned color model will render only the visibleBand and ignore the others, but the existence of all numBands will be at least tolerated. Supplemental bands, even invisible, are useful for processing.
      visibleBand - the band to be made visible (usually 0). All other bands (if any) will be ignored.
      colors - the colors to use for each range of sample values. The map may contain null values, which means transparent.
      Returns:
      a color model suitable for RenderedImage objects with values in the given ranges.
      See Also:
    • createIndexColorModel

      public static IndexColorModel createIndexColorModel(int numBands, int visibleBand, int[] ARGB, boolean hasAlpha, int transparent)
      Returns a tolerant index color model for the specified ARGB codes. This color model accepts image with the specified number of bands.

      This methods caches previously created instances using weak references, because index color model may be big (up to 256 kb).

      Parameters:
      numBands - the number of bands.
      visibleBand - the band to display.
      ARGB - an array of ARGB values.
      hasAlpha - indicates whether alpha values are contained in the ARGB array.
      transparent - the transparent pixel, or -1 for auto-detection.
      Returns:
      An index color model for the specified array.
    • createColorScale

      public static ColorModel createColorScale(int dataType, int numBands, int visibleBand, double lower, double upper, Color... colors)
      Returns a color model interpolated for the given range of values. This is a convenience method for createPiecewise(int, int, int, Map) when the map contains only one element.
      Parameters:
      dataType - the color model type.
      numBands - the number of bands for the color model (usually 1).
      visibleBand - the band to be made visible (usually 0). All other bands (if any) will be ignored.
      lower - the minimum value, inclusive.
      upper - the maximum value, exclusive.
      colors - the colors to use for the range of sample values.
      Returns:
      a color model suitable for RenderedImage objects with values in the given ranges.
    • createGrayScale

      public static ColorModel createGrayScale(int dataType, int numComponents, int visibleBand, double minimum, double maximum)
      Creates a color model for opaque images storing pixels as real numbers. The color model can have an arbitrary number of bands, but in current implementation only one band is used.

      Warning: the use of this color model is very slow. It should be used only when no standard color model can be used.

      Parameters:
      dataType - the color model type as one of DataBuffer.TYPE_* constants.
      numComponents - the number of components.
      visibleBand - the band to use for computing colors.
      minimum - the minimal sample value expected.
      maximum - the maximal sample value expected.
      Returns:
      the color model for the given range of values.
      See Also:
    • createGrayScale

      public static ColorModel createGrayScale(SampleModel model, int visibleBand, NumberRange<?> range)
      Creates a color model for opaque images storing pixels using the given sample model. The color model can have an arbitrary number of bands, but in current implementation only one band is used.

      Warning: the use of this color model may be very slow and the color stretching may not be a good fit. This method should be used only when no standard color model can be used. This is a last resort method.

      Parameters:
      model - the sample model for which to create a gray scale color model.
      visibleBand - the band to use for computing colors.
      range - the minimal and maximal sample value expected, or null if none.
      Returns:
      the color model for the given range of values.
    • createRGB

      public static ColorModel createRGB(SampleModel model)
      Creates a RGB color model for the given sample model. The sample model shall use integer type and have 3 or 4 bands.
      Parameters:
      model - the sample model for which to create a color model.
      Returns:
      the color model.
    • createRGB

      public static ColorModel createRGB(int bitsPerSample, boolean packed, boolean hasAlpha)
      Creates a RGB color model. The packed argument should be true for color model used with SinglePixelPackedSampleModel, and false for color model used with BandedSampleModel.
      Parameters:
      bitsPerSample - number of bits per sample, between 1 and 8 inclusive.
      packed - whether sample values are packed in a single element.
      hasAlpha - whether the color model should have an alpha channel.
      Returns:
      the color model.
    • createSubset

      public static Optional<ColorModel> createSubset(ColorModel cm, int[] bands)
      Creates a color model with only a subset of the bands of the given color model. The returned color model is compatible with a subset sample model created with the same argument than the bands argument given to this method. This method might not produce a result in following cases:
      • Input color model is null.
      • Given color model is not assignable from the following types:
      • Input color model is recognized, but we cannot infer a proper color interpretation for given number of bands.
      Note about PackedColorModel and DirectColorModel: they're not managed for now, because they're really designed for a "rigid" case where data buffer values are interpreted directly by the color model.
      Parameters:
      cm - the color model, or null.
      bands - the bands to select. Must neither be null nor empty.
      Returns:
      the subset color model, or empty if input was null or if a subset cannot be deduced.
    • formatDescription

      @Debug public static void formatDescription(ColorSpace cs, StringBuilder buffer)
      Appends a description of the given color space in the given buffer. This is used for toString() method implementations.
      Parameters:
      cs - the color space to describe, or null.
      buffer - where to append the description.
    • getBitCount

      public static int getBitCount(int mapSize)
      Returns a bit count for an IndexColorModel mapping mapSize colors. It is guaranteed that the following relation is hold:
      Parameters:
      mapSize - the number of colors in the map.
      Returns:
      the number of bits to use.
    • expand

      public static void expand(int[] colors, int[] ARGB, int lower, int upper)
      Copies colors into ARGB array from index lower inclusive to index upper exclusive. If upper-lower is not equal to the length of colors array, then colors will be interpolated. The given colors array must be initialized with zero values in the lowerupper range.
      Parameters:
      colors - colors to copy into the ARGB array.
      ARGB - array of integer to write ARGB values into.
      lower - index (inclusive) of the first element of ARGB to change.
      upper - index (exclusive) of the last element of ARGB to change.
    • toString

      public String toString()
      Returns a string representation for debugging purposes.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this color factory.