Package org.geotools.image.util
Class ColorUtilities
Object
ColorUtilities
A set of static methods for handling of colors informations. Some of those methods are useful, but not really
rigorous. This is why they do not appear in any "official" package, but instead in this private one.
Do not rely on this API!
It may change in incompatible way in any future version.
- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD), Simone Giannecchini
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ComponentColorModelstatic final ComponentColorModelstatic final ComponentColorModelstatic final ComponentColorModel -
Method Summary
Modifier and TypeMethodDescriptionstatic IndexColorModelapplyBackgroundColor(IndexColorModel icm, Color bg) Applies the specified background color to an index color model, building a new index color model (or returning the same index color mode if it has no transparency at all)static floatcolorDistance(float[] lab1, float[] lab2) Computes the distance E (CIE 1994) between two colors in LAB color space.static voidCopiescolorsinto arrayARGBfrom indexlowerinclusive to indexupperexclusive.static intfindColorIndex(Color bgColor, IndexColorModel icm) Looks for the specified color in the color modelstatic intgetBitCount(int mapSize) Returns a bit count for anIndexColorModelmappingmapSizecolors.static intgetColorIndex(IndexColorModel colors, Color color, int exclude) Returns the index of the specified color, excluding the specified one.static IndexColorModelgetIndexColorModel(int[] ARGB) Returns an index color model for specified ARGB codes.static IndexColorModelgetIndexColorModel(int[] ARGB, int numBands, int visibleBand) Returns a tolerant index color model for the specified ARGB code.static intgetIntFromColor(int r, int g, int b, int a) Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).static doublegetMaximum(int dataType) Provide the maximum allowe value for a certain data type.static doublegetMinimum(int dataType) Provide the minimum allowe value for a certain data type.static intgetNumBands(ColorModel model) Tries to guess the number of bands from the specified color model.static doublegetThreshold(int dataType) Returns a suitable threshold depending on theDataBuffertype.static intgetTransferType(int mapSize) Returns a suggered type for anIndexColorModelofmapSizecolors.static intgetTransparentPixel(IndexColorModel colors) Returns the most transparent pixel in the specified color model.static booleanisGrayPalette(IndexColorModel icm, boolean ignoreTransparents) Tells us if a specificIndexColorModelcontains only gray color or not, ignoring alpha information.static introundByte(double value) Rounds a float value and clamp the result between 0 and 255 inclusive.static Color[]Returns a subarray of the specified color array.static float[]XYZtoLAB(float[] color) Transforms a color from XYZ color space to LAB.
-
Field Details
-
GRAY_CM
-
GRAY_ALPHA_CM
-
RGB_CM
-
RGB_ALPHA_CM
-
-
Method Details
-
getIntFromColor
public static int getIntFromColor(int r, int g, int b, int a) Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).- Parameters:
r- the red componentg- the green componentb- the blue componenta- the alpha component- Throws:
IllegalArgumentException- if ,g,boraare outside of the range 0 to 255, inclusive.
-
subarray
Returns a subarray of the specified color array. Thelowerandupperindex will be clamb into thepaletterange. If they are completly out of range, or if they would result in an empty array, thennullis returned.This method is used by
org.geotools.cv.SampleDimensionas an heuristic approach for distributing palette colors into a list of categories.- Parameters:
palette- The color array (may benull).lower- The lower index, inclusive.upper- The upper index, inclusive.- Returns:
- The subarray (may be
paletteif the original array already fit), ornullif thelowerandupperindex are out ofpalettebounds.
-
expand
Copiescolorsinto arrayARGBfrom indexlowerinclusive to indexupperexclusive. Ifupper-loweris not equals to the length ofcolorsarray, then colors will be interpolated.Note: Profiling shows that this method is a "hot spot". It needs to be fast, which is why the implementation is not as straight-forward as it could.
- Parameters:
colors- Colors to copy into theARGBarray.ARGB- Array of integer to write ARGB values to.lower- Index (inclusive) of the first element ofARGBto change.upper- Index (exclusive) of the last element ofARGBto change.
-
roundByte
public static int roundByte(double value) Rounds a float value and clamp the result between 0 and 255 inclusive. -
getIndexColorModel
Returns an index color model for specified ARGB codes. If the specified array has not transparent color (i.e. all alpha values are 255), then the returned color model will be opaque. Otherwise, if the specified array has one and only one color with alpha value of 0, the returned color model will have only this transparent color. Otherwise, the returned color model will be translucent.- Parameters:
ARGB- An array of ARGB values.- Returns:
- An index color model for the specified array.
-
getIndexColorModel
Returns a tolerant index color model for the specified ARGB code. This color model accept image with the specified number of bands.- Parameters:
ARGB- An array of ARGB values.numBands- The number of bands.visibleBand- The band to display.- Returns:
- An index color model for the specified array.
-
getBitCount
public static int getBitCount(int mapSize) Returns a bit count for anIndexColorModelmappingmapSizecolors. It is guaranteed that the following relation is hold:(1 << getBitCount(mapSize)) >= mapSize
-
getTransferType
public static int getTransferType(int mapSize) Returns a suggered type for anIndexColorModelofmapSizecolors. This method returnsDataBuffer.TYPE_BYTEorDataBuffer.TYPE_USHORT. -
XYZtoLAB
public static float[] XYZtoLAB(float[] color) Transforms a color from XYZ color space to LAB. The color are transformed in place. This method returnscolorfor convenience. Reference: http://www.brucelindbloom.com/index.html?ColorDifferenceCalc.html -
colorDistance
public static float colorDistance(float[] lab1, float[] lab2) Computes the distance E (CIE 1994) between two colors in LAB color space. Reference: http://www.brucelindbloom.com/index.html?ColorDifferenceCalc.html -
getTransparentPixel
Returns the most transparent pixel in the specified color model. If many colors has the same alpha value, than the darkest one is returned. This method never returns a negative value (0 is returned if the color model has no colors).- Parameters:
colors- The color model in which to look for a transparent color.- Returns:
- The index of a transparent color, or 0.
-
getColorIndex
Returns the index of the specified color, excluding the specified one. If the color is not explicitly found, a close color is returned. This method never returns a negative value (0 is returned if the color model has no colors).- Parameters:
colors- The color model in which to look for a color index.color- The color to search for.exclude- An index to exclude from the search (usually the background or the transparent pixel), or -1 if none.- Returns:
- The index of the color, or 0.
-
getNumBands
Tries to guess the number of bands from the specified color model. The recommanded approach is to invokeSampleModel.getNumBands(). This method should be used only as a fallback when the sample model is not available. This method uses some heuristic rules for guessing the number of bands, so the return value may not be exact in all cases. -
isGrayPalette
Tells us if a specificIndexColorModelcontains only gray color or not, ignoring alpha information.- Parameters:
icm-IndexColorModelto be inspected.ignoreTransparents-trueif the RGB values of fully transparent pixels (the ones with an alpha value of 0) should not be taken in account during the check for gray color.- Returns:
trueif the palette is grayscale,falseotherwise.
-
getMinimum
public static double getMinimum(int dataType) Provide the minimum allowe value for a certain data type.- Parameters:
dataType- the data type to suggest a maximum value for.- Returns:
- the data type maximum value for.
-
getThreshold
public static double getThreshold(int dataType) Returns a suitable threshold depending on theDataBuffertype.Remember that the threshold works with >=.
- Parameters:
dataType- to create a low threshold for.- Returns:
- a minimum threshold value suitable for this data type.
-
findColorIndex
Looks for the specified color in the color model- Parameters:
bgColor- The color to be searchedicm- The color model to be searched into- Returns:
- The index of the color in the color model, or -1 if not found
-
applyBackgroundColor
Applies the specified background color to an index color model, building a new index color model (or returning the same index color mode if it has no transparency at all) -
getMaximum
public static double getMaximum(int dataType) Provide the maximum allowe value for a certain data type.- Parameters:
dataType- the data type to suggest a maximum value for.- Returns:
- the data type maximum value for.
-