Class ColorSpace
- Direct Known Subclasses:
ColorSpace.Rgb
A ColorSpace is used to identify a specific organization of colors.
Each color space is characterized by a color model that defines
how a color value is represented (for instance the RGB color
model defines a color value as a triplet of numbers).
Each component of a color must fall within a valid range, specific to each
color space, defined by getMinValue(int) and getMaxValue(int)
This range is commonly \([0..1]\). While it is recommended to use values in the
valid range, a color space always clamps input and output values when performing
operations such as converting to a different color space.
Using color spaces
This implementation provides a pre-defined set of common color spaces
described in the ColorSpace.Named enum. To obtain an instance of one of the
pre-defined color spaces, simply invoke get(Named):
ColorSpace sRgb = ColorSpace.get(ColorSpace.Named.SRGB);
The get(Named) method always returns the same instance for a given
name. Color spaces with an RGB color model can be safely
cast to ColorSpace.Rgb. Doing so gives you access to more APIs to query various
properties of RGB color models: color gamut primaries, transfer functions,
conversions to and from linear space, etc. Please refer to ColorSpace.Rgb for
more information.
The documentation of ColorSpace.Named provides a detailed description of the
various characteristics of each available color space.
Color space conversions
To allow conversion between color spaces, this implementation uses the CIE
XYZ profile connection space (PCS). Color values can be converted to and from
this PCS using toXyz(float[]) and fromXyz(float[]).
For color space with a non-RGB color model, the white point of the PCS
must be the CIE standard illuminant D50. RGB color spaces use their
native white point (D65 for sRGB for instance and must
undergo chromatic adaptation as necessary.
Since the white point of the PCS is not defined for RGB color space, it is
highly recommended to use the variants of the connect(ColorSpace, ColorSpace)
method to perform conversions between color spaces. A color space can be
manually adapted to a specific white point using adapt(ColorSpace, float[]).
Please refer to the documentation of RGB color spaces for more
information. Several common CIE standard illuminants are provided in this
class as reference (see ILLUMINANT_D65 or ILLUMINANT_D50
for instance).
Here is an example of how to convert from a color space to another:
// Convert from DCI-P3 to Rec.2020
ColorSpace.Connector connector = ColorSpace.connect(
ColorSpace.get(ColorSpace.Named.DCI_P3),
ColorSpace.get(ColorSpace.Named.BT2020));
float[] bt2020 = connector.transform(p3r, p3g, p3b);
You can easily convert to sRGB by omitting the second
parameter:
// Convert from DCI-P3 to sRGB
ColorSpace.Connector connector = ColorSpace.connect(ColorSpace.get(ColorSpace.Named.DCI_P3));
float[] sRGB = connector.transform(p3r, p3g, p3b);
Conversions also work between color spaces with different color models:
// Convert from CIE L*a*b* (color model Lab) to Rec.709 (color model RGB)
ColorSpace.Connector connector = ColorSpace.connect(
ColorSpace.get(ColorSpace.Named.CIE_LAB),
ColorSpace.get(ColorSpace.Named.BT709));
Color spaces and multi-threading
Color spaces and other related classes (ColorSpace.Connector for instance)
are immutable and stateless. They can be safely used from multiple concurrent
threads.
Public static methods provided by this class, such as get(Named)
and connect(ColorSpace, ColorSpace), are also guaranteed to be
thread-safe.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic classstatic enumA color model is required by aColorSpaceto describe the way colors can be represented as tuples of numbers.static enumList of common, named color spaces.static enumA render intent determines how aconnectormaps colors from one color space to another.static class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final float[]Standard CIE 1931 2° illuminant A, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant B, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant C, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant D50, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant D55, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant D60, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant D65, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant D75, encoded in xyY.static final float[]Standard CIE 1931 2° illuminant E, encoded in xyY.static final intThe maximum ID value a color space can have.static final intThe minimum ID value a color space can have. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull ColorSpaceadapt(@NonNull ColorSpace colorSpace, float @NonNull [] whitePoint) Performs the chromatic adaptation of a color space from its native white point to the specified white point.static @NonNull ColorSpaceadapt(@NonNull ColorSpace colorSpace, float @NonNull [] whitePoint, @NonNull ColorSpace.Adaptation adaptation) Performs the chromatic adaptation of a color space from its native white point to the specified white point.static float @NonNull []adaptToIlluminantD50(float @NonNull [] origWhitePoint, float @NonNull [] origTransform) Helper method for internal color space transformation.static float @NonNull []cctToXyz(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int cct) Computes the chromaticity coordinates of a specified correlated color temperature (CCT) on the Planckian locus.static float @NonNull []chromaticAdaptation(@NonNull ColorSpace.Adaptation adaptation, float @NonNull [] srcWhitePoint, float @NonNull [] dstWhitePoint) Computes the chromatic adaptation transform from the specified source white point to the specified destination white point.static @NonNull ColorSpace.Connectorconnect(@NonNull ColorSpace source) Connects the specified color spaces to sRGB.static @NonNull ColorSpace.Connectorconnect(@NonNull ColorSpace source, @NonNull ColorSpace destination) Connects two color spaces to allow conversion from the source color space to the destination color space.static @NonNull ColorSpace.Connectorconnect(@NonNull ColorSpace source, @NonNull ColorSpace destination, @NonNull ColorSpace.RenderIntent intent) Connects two color spaces to allow conversion from the source color space to the destination color space.static @NonNull ColorSpace.Connectorconnect(@NonNull ColorSpace source, @NonNull ColorSpace.RenderIntent intent) Connects the specified color spaces to sRGB.booleanabstract float @NonNull []fromXyz(float @NonNull [] v) Converts tristimulus values from the CIE XYZ space to this color space's color model.float @NonNull []fromXyz(float x, float y, float z) Converts tristimulus values from the CIE XYZ space to this color space's color model.static @NonNull ColorSpaceget(@NonNull ColorSpace.Named name) Returns an instance ofColorSpaceidentified by the specified name.@org.jetbrains.annotations.Range(from=1L, to=4L) intReturns the number of components that form a color value according to this color space's color model.@org.jetbrains.annotations.Range(from=-1L, to=63L) intgetId()Returns the ID of this color space.abstract floatgetMaxValue(@org.jetbrains.annotations.Range(from=0L, to=3L) int component) Returns the maximum valid value for the specified component of this color space's color model.abstract floatgetMinValue(@org.jetbrains.annotations.Range(from=0L, to=3L) int component) Returns the minimum valid value for the specified component of this color space's color model.@NonNull ColorSpace.ModelgetModel()Return the color model of this color space.@NonNull StringgetName()Returns the name of this color space.inthashCode()booleanisSrgb()Indicates whether this color space is the sRGB color space or equivalent to the sRGB color space.abstract booleanReturns whether this color space is a wide-gamut color space.static @Nullable ColorSpacematch(float @NonNull [] toXYZD50, @NonNull ColorSpace.Rgb.TransferParameters function) Returns aColorSpace.Namedinstance ofColorSpacethat matches the specified RGB to CIE XYZ transform and transfer functions.@NonNull StringtoString()Returns a string representation of the object.abstract float @NonNull []toXyz(float @NonNull [] v) Converts a color value from this color space's model to tristimulus CIE XYZ values.float @NonNull []toXyz(float r, float g, float b) Converts a color value from this color space's model to tristimulus CIE XYZ values.
-
Field Details
-
ILLUMINANT_A
public static final float[] ILLUMINANT_AStandard CIE 1931 2° illuminant A, encoded in xyY. This illuminant has a color temperature of 2856K. -
ILLUMINANT_B
public static final float[] ILLUMINANT_BStandard CIE 1931 2° illuminant B, encoded in xyY. This illuminant has a color temperature of 4874K. -
ILLUMINANT_C
public static final float[] ILLUMINANT_CStandard CIE 1931 2° illuminant C, encoded in xyY. This illuminant has a color temperature of 6774K. -
ILLUMINANT_D50
public static final float[] ILLUMINANT_D50Standard CIE 1931 2° illuminant D50, encoded in xyY. This illuminant has a color temperature of 5003K. This illuminant is used by the profile connection space in ICC profiles. -
ILLUMINANT_D55
public static final float[] ILLUMINANT_D55Standard CIE 1931 2° illuminant D55, encoded in xyY. This illuminant has a color temperature of 5503K. -
ILLUMINANT_D60
public static final float[] ILLUMINANT_D60Standard CIE 1931 2° illuminant D60, encoded in xyY. This illuminant has a color temperature of 6004K. -
ILLUMINANT_D65
public static final float[] ILLUMINANT_D65Standard CIE 1931 2° illuminant D65, encoded in xyY. This illuminant has a color temperature of 6504K. This illuminant is commonly used in RGB color spaces such as sRGB, BT.209, etc. -
ILLUMINANT_D75
public static final float[] ILLUMINANT_D75Standard CIE 1931 2° illuminant D75, encoded in xyY. This illuminant has a color temperature of 7504K. -
ILLUMINANT_E
public static final float[] ILLUMINANT_EStandard CIE 1931 2° illuminant E, encoded in xyY. This illuminant has a color temperature of 5454K. -
MIN_ID
public static final int MIN_IDThe minimum ID value a color space can have.- See Also:
-
MAX_ID
public static final int MAX_IDThe maximum ID value a color space can have.- See Also:
-
-
Method Details
-
getName
Returns the name of this color space. The name is never null and contains always at least 1 character.
Color space names are recommended to be unique but are not guaranteed to be. There is no defined format but the name usually falls in one of the following categories:
- Generic names used to identify color spaces in non-RGB
color models. For instance:
Generic L*a*b*. - Names tied to a particular specification. For instance:
sRGB IEC61966-2.1orSMPTE ST 2065-1:2012 ACES. - Ad-hoc names, often generated procedurally or by the user during a calibration workflow. These names often contain the make and model of the display.
Because the format of color space names is not defined, it is not recommended to programmatically identify a color space by its name alone. Names can be used as a first approximation.
It is however perfectly acceptable to display color space names to users in a UI, or in debuggers and logs. When displaying a color space name to the user, it is recommended to add extra information to avoid ambiguities: color model, a representation of the color space's gamut, white point, etc.
- Returns:
- A non-null String of length >= 1
- Generic names used to identify color spaces in non-RGB
color models. For instance:
-
getId
public @org.jetbrains.annotations.Range(from=-1L, to=63L) int getId()Returns the ID of this color space. Positive IDs match the color spaces enumerated inColorSpace.Named. A negative ID indicates a color space created by calling one of the public constructors. -
getModel
Return the color model of this color space.- Returns:
- A non-null
ColorSpace.Model - See Also:
-
getComponentCount
public @org.jetbrains.annotations.Range(from=1L, to=4L) int getComponentCount()Returns the number of components that form a color value according to this color space's color model.- Returns:
- An integer between 1 and 4
- See Also:
-
isWideGamut
public abstract boolean isWideGamut()Returns whether this color space is a wide-gamut color space. An RGB color space is wide-gamut if its gamut entirely contains thesRGBgamut and if the area of its gamut is 90% of greater than the area of theNTSCgamut.- Returns:
- True if this color space is a wide-gamut color space, false otherwise
-
isSrgb
public boolean isSrgb()Indicates whether this color space is the sRGB color space or equivalent to the sRGB color space.
A color space is considered sRGB if it meets all the following conditions:
- Its color model is
ColorSpace.Model.RGB. -
Its primaries are within 1e-3 of the true
sRGBprimaries. -
Its white point is within 1e-3 of the CIE standard
illuminant
D65. - Its opto-electronic transfer function is not linear.
- Its electro-optical transfer function is not linear.
- Its transfer functions yield values within 1e-3 of
ColorSpace.Named.SRGB. - Its range is \([0..1]\).
This method always returns true for
ColorSpace.Named.SRGB.- Returns:
- True if this color space is the sRGB color space (or a close approximation), false otherwise
- Its color model is
-
getMinValue
public abstract float getMinValue(@org.jetbrains.annotations.Range(from=0L, to=3L) int component) Returns the minimum valid value for the specified component of this color space's color model.- Parameters:
component- The index of the component- Returns:
- A floating point value less than
getMaxValue(int) - See Also:
-
getMaxValue
public abstract float getMaxValue(@org.jetbrains.annotations.Range(from=0L, to=3L) int component) Returns the maximum valid value for the specified component of this color space's color model.- Parameters:
component- The index of the component- Returns:
- A floating point value greater than
getMinValue(int) - See Also:
-
toXyz
Converts a color value from this color space's model to tristimulus CIE XYZ values. If the color model of this color space is not
RGB, it is assumed that the target CIE XYZ space uses aD50standard illuminant.This method is a convenience for color spaces with a model of 3 components (
.RGBorColorSpace.Model.LABfor instance). With color spaces using fewer or more components, usetoXyz(float[])instead- Parameters:
r- The first component of the value to convert from (typically R in RGB)g- The second component of the value to convert from (typically G in RGB)b- The third component of the value to convert from (typically B in RGB)- Returns:
- A new array of 3 floats, containing tristimulus XYZ values
- See Also:
-
toXyz
Converts a color value from this color space's model to tristimulus CIE XYZ values. If the color model of this color space is not
RGB, it is assumed that the target CIE XYZ space uses aD50standard illuminant.The specified array's length must be at least equal to to the number of color components as returned by
ColorSpace.Model.getComponentCount().- Parameters:
v- An array of color components containing the color space's color value to convert to XYZ, and large enough to hold the resulting tristimulus XYZ values- Returns:
- The array passed in parameter
- See Also:
-
fromXyz
Converts tristimulus values from the CIE XYZ space to this color space's color model.
- Parameters:
x- The X component of the color valuey- The Y component of the color valuez- The Z component of the color value- Returns:
- A new array whose size is equal to the number of color
components as returned by
ColorSpace.Model.getComponentCount() - See Also:
-
fromXyz
Converts tristimulus values from the CIE XYZ space to this color space's color model. The resulting value is passed back in the specified array.
The specified array's length must be at least equal to to the number of color components as returned by
ColorSpace.Model.getComponentCount(), and its first 3 values must be the XYZ components to convert from.- Parameters:
v- An array of color components containing the XYZ values to convert from, and large enough to hold the number of components of this color space's model- Returns:
- The array passed in parameter
- See Also:
-
hashCode
public int hashCode() -
equals
-
toString
Returns a string representation of the object. This method returns a string equal to the value of:
getName() + "(id=" + getId() + ", model=" + getModel() + ")"
For instance, the string representation of the
sRGBcolor space is equal to the following value:sRGB IEC61966-2.1 (id=0, model=RGB)
-
connect
public static @NonNull ColorSpace.Connector connect(@NonNull ColorSpace source, @NonNull ColorSpace destination) Connects two color spaces to allow conversion from the source color space to the destination color space. If the source and destination color spaces do not have the same profile connection space (CIE XYZ with the same white point), they are chromatically adapted to use the CIE standard illuminant
D50as needed.If the source and destination are the same, an optimized connector is returned to avoid unnecessary computations and loss of precision.
Colors are mapped from the source color space to the destination color space using the
perceptualrender intent.- Parameters:
source- The color space to convert colors fromdestination- The color space to convert colors to- Returns:
- A non-null connector between the two specified color spaces
- See Also:
-
connect
public static @NonNull ColorSpace.Connector connect(@NonNull ColorSpace source, @NonNull ColorSpace destination, @NonNull ColorSpace.RenderIntent intent) Connects two color spaces to allow conversion from the source color space to the destination color space. If the source and destination color spaces do not have the same profile connection space (CIE XYZ with the same white point), they are chromatically adapted to use the CIE standard illuminant
D50as needed.If the source and destination are the same, an optimized connector is returned to avoid unnecessary computations and loss of precision.
- Parameters:
source- The color space to convert colors fromdestination- The color space to convert colors tointent- The render intent to map colors from the source to the destination- Returns:
- A non-null connector between the two specified color spaces
- See Also:
-
connect
Connects the specified color spaces to sRGB. If the source color space does not use CIE XYZ D65 as its profile connection space, the two spaces are chromatically adapted to use the CIE standard illuminant
D50as needed.If the source is the sRGB color space, an optimized connector is returned to avoid unnecessary computations and loss of precision.
Colors are mapped from the source color space to the destination color space using the
perceptualrender intent.- Parameters:
source- The color space to convert colors from- Returns:
- A non-null connector between the specified color space and sRGB
- See Also:
-
connect
public static @NonNull ColorSpace.Connector connect(@NonNull ColorSpace source, @NonNull ColorSpace.RenderIntent intent) Connects the specified color spaces to sRGB. If the source color space does not use CIE XYZ D65 as its profile connection space, the two spaces are chromatically adapted to use the CIE standard illuminant
D50as needed.If the source is the sRGB color space, an optimized connector is returned to avoid unnecessary computations and loss of precision.
- Parameters:
source- The color space to convert colors fromintent- The render intent to map colors from the source to the destination- Returns:
- A non-null connector between the specified color space and sRGB
- See Also:
-
adapt
public static @NonNull ColorSpace adapt(@NonNull ColorSpace colorSpace, @Size(min=2L,max=3L) float @NonNull [] whitePoint) Performs the chromatic adaptation of a color space from its native white point to the specified white point.
The chromatic adaptation is performed using the
ColorSpace.Adaptation.BRADFORDmatrix.The color space returned by this method always has an ID of
MIN_ID.- Parameters:
colorSpace- The color space to chromatically adaptwhitePoint- The new white point- Returns:
- A
ColorSpaceinstance with the same name, primaries, transfer functions and range as the specified color space - See Also:
-
adapt
public static @NonNull ColorSpace adapt(@NonNull ColorSpace colorSpace, @Size(min=2L,max=3L) float @NonNull [] whitePoint, @NonNull ColorSpace.Adaptation adaptation) Performs the chromatic adaptation of a color space from its native white point to the specified white point. If the specified color space does not have an
RGBcolor model, or if the color space already has the target white point, the color space is returned unmodified.The chromatic adaptation is performed using the von Kries method described in the documentation of
ColorSpace.Adaptation.The color space returned by this method always has an ID of
MIN_ID.- Parameters:
colorSpace- The color space to chromatically adaptwhitePoint- The new white pointadaptation- The adaptation matrix- Returns:
- A new color space if the specified color space has an RGB model and a white point different from the specified white point; the specified color space otherwise
- See Also:
-
get
Returns an instance of
ColorSpaceidentified by the specified name. The list of names provided in theColorSpace.Namedenum gives access to a variety of common RGB color spaces.This method always returns the same instance for a given name.
This method is thread-safe.
- Parameters:
name- The name of the color space to get an instance of- Returns:
- A non-null
ColorSpaceinstance
-
match
public static @Nullable ColorSpace match(@Size(9L) float @NonNull [] toXYZD50, @NonNull ColorSpace.Rgb.TransferParameters function) Returns a
ColorSpace.Namedinstance ofColorSpacethat matches the specified RGB to CIE XYZ transform and transfer functions. If no instance can be found, this method returns null.The color transform matrix is assumed to target the CIE XYZ space a
D50standard illuminant.- Parameters:
toXYZD50- 3x3 column-major transform matrix from RGB to the profile connection space CIE XYZ as an array of 9 floats, cannot be nullfunction- Parameters for the transfer functions- Returns:
- A non-null
ColorSpaceif a match is found, null otherwise
-
adaptToIlluminantD50
@Size(9L) public static float @NonNull [] adaptToIlluminantD50(@Size(2L) float @NonNull [] origWhitePoint, @Size(9L) float @NonNull [] origTransform) Helper method for internal color space transformation.This essentially calls adapt on a ColorSpace that has not been fully created. It also does not fully create the adapted ColorSpace, but just returns the transform.
-
cctToXyz
@Size(3L) public static float @NonNull [] cctToXyz(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int cct) Computes the chromaticity coordinates of a specified correlated color temperature (CCT) on the Planckian locus. The specified CCT must be greater than 0. A meaningful CCT range is [1667, 25000].
The transform is computed using the methods in Kang et al., Design of Advanced Color - Temperature Control System for HDTV Applications, Journal of Korean Physical Society 41, 865-871 (2002).
- Parameters:
cct- The correlated color temperature, in Kelvin- Returns:
- Corresponding XYZ values
- Throws:
IllegalArgumentException- If cct is invalid
-
chromaticAdaptation
@Size(9L) public static float @NonNull [] chromaticAdaptation(@NonNull ColorSpace.Adaptation adaptation, @Size(min=2L,max=3L) float @NonNull [] srcWhitePoint, @Size(min=2L,max=3L) float @NonNull [] dstWhitePoint) Computes the chromatic adaptation transform from the specified source white point to the specified destination white point.
The transform is computed using the von Kries method, described in more details in the documentation of
ColorSpace.Adaptation. TheColorSpace.Adaptationenum provides different matrices that can be used to perform the adaptation.- Parameters:
adaptation- The adaptation methodsrcWhitePoint- The white point to adapt fromdstWhitePoint- The white point to adapt to- Returns:
- A 3x3 matrix as a non-null array of 9 floats
-