ColorSpace
public
abstract
class
ColorSpace
extends Object
| java.lang.Object | |
| ↳ | android.graphics.ColorSpace |
|
|
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.
Summary
Nested classes | |
|---|---|
enum |
ColorSpace.Adaptation
List of adaptation matrices that can be used for chromatic adaptation using the von Kries transform. |
class |
ColorSpace.Connector
A connector transforms colors from a source color space to a destination color space. |
enum |
ColorSpace.Model
A color model is required by a |
enum |
ColorSpace.Named
List of common, named color spaces. |
enum |
ColorSpace.RenderIntent
A render intent determines how a |
class |
ColorSpace.Rgb
An RGB color space is an additive color space using the
|
Constants | |
|---|---|
int |
MAX_ID
The maximum ID value a color space can have. |
int |
MIN_ID
The minimum ID value a color space can have. |
Fields | |
|---|---|
public
static
final
float[] |
ILLUMINANT_A
Standard CIE 1931 2° illuminant A, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_B
Standard CIE 1931 2° illuminant B, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_C
Standard CIE 1931 2° illuminant C, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_D50
Standard CIE 1931 2° illuminant D50, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_D55
Standard CIE 1931 2° illuminant D55, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_D60
Standard CIE 1931 2° illuminant D60, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_D65
Standard CIE 1931 2° illuminant D65, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_D75
Standard CIE 1931 2° illuminant D75, encoded in xyY. |
public
static
final
float[] |
ILLUMINANT_E
Standard CIE 1931 2° illuminant E, encoded in xyY. |
Public methods | |
|---|---|
static
ColorSpace
|
adapt(ColorSpace colorSpace, float[] whitePoint, ColorSpace.Adaptation adaptation)
Performs the chromatic adaptation of a color space from its native white point to the specified white point. |
static
ColorSpace
|
adapt(ColorSpace colorSpace, float[] whitePoint)
Performs the chromatic adaptation of a color space from its native white point to the specified white point. |
static
ColorSpace.Connector
|
connect(ColorSpace source, ColorSpace.RenderIntent intent)
Connects the specified color spaces to sRGB. |
static
ColorSpace.Connector
|
connect(ColorSpace source, ColorSpace destination, ColorSpace.RenderIntent intent)
Connects two color spaces to allow conversion from the source color space to the destination color space. |
static
ColorSpace.Connector
|
connect(ColorSpace source)
Connects the specified color spaces to sRGB. |
static
ColorSpace.Connector
|
connect(ColorSpace source, ColorSpace destination)
Connects two color spaces to allow conversion from the source color space to the destination color space. |
boolean
|
equals(Object o)
Indicates whether some other object is "equal to" this one. |
float[]
|
fromXyz(float x, float y, float z)
Converts tristimulus values from the CIE XYZ space to this color space's color model. |
abstract
float[]
|
fromXyz(float[] v)
Converts tristimulus values from the CIE XYZ space to this color space's color model. |
static
ColorSpace
|
get(ColorSpace.Named name)
Returns an instance of |
int
|
getComponentCount()
Returns the number of components that form a color value according to this color space's color model. |
int
|
getId()
Returns the ID of this color space. |
abstract
float
|
getMaxValue(int component)
Returns the maximum valid value for the specified component of this color space's color model. |
abstract
float
|
getMinValue(int component)
Returns the minimum valid value for the specified component of this color space's color model. |
ColorSpace.Model
|
getModel()
Return the color model of this color space. |
String
|
getName()
Returns the name of this color space. |
int
|
hashCode()
Returns a hash code value for the object. |
boolean
|
isSrgb()
Indicates whether this color space is the sRGB color space or equivalent to the sRGB color space. |
abstract
boolean
|
isWideGamut()
Returns whether this color space is a wide-gamut color space. |
static
ColorSpace
|
match(float[] toXYZD50, ColorSpace.Rgb.TransferParameters function)
Returns a |
String
|
toString()
Returns a string representation of the object. |
float[]
|
toXyz(float r, float g, float b)
Converts a color value from this color space's model to tristimulus CIE XYZ values. |
abstract
float[]
|
toXyz(float[] v)
Converts a color value from this color space's model to tristimulus CIE XYZ values. |
Inherited methods | |
|---|---|
java.lang.Object
| |
Constants
MAX_ID
int MAX_ID
The maximum ID value a color space can have.
See also:
Constant Value: 63 (0x0000003f)
MIN_ID
int MIN_ID
The minimum ID value a color space can have.
See also:
Constant Value: -1 (0xffffffff)
Fields
ILLUMINANT_A
float[] ILLUMINANT_A
Standard CIE 1931 2° illuminant A, encoded in xyY. This illuminant has a color temperature of 2856K.
ILLUMINANT_B
float[] ILLUMINANT_B
Standard CIE 1931 2° illuminant B, encoded in xyY. This illuminant has a color temperature of 4874K.
ILLUMINANT_C
float[] ILLUMINANT_C
Standard CIE 1931 2° illuminant C, encoded in xyY. This illuminant has a color temperature of 6774K.
ILLUMINANT_D50
float[] ILLUMINANT_D50
Standard 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
float[] ILLUMINANT_D55
Standard CIE 1931 2° illuminant D55, encoded in xyY. This illuminant has a color temperature of 5503K.
ILLUMINANT_D60
float[] ILLUMINANT_D60
Standard CIE 1931 2° illuminant D60, encoded in xyY. This illuminant has a color temperature of 6004K.
ILLUMINANT_D65
float[] ILLUMINANT_D65
Standard 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
float[] ILLUMINANT_D75
Standard CIE 1931 2° illuminant D75, encoded in xyY. This illuminant has a color temperature of 7504K.
ILLUMINANT_E
float[] ILLUMINANT_E
Standard CIE 1931 2° illuminant E, encoded in xyY. This illuminant has a color temperature of 5454K.
Public methods
adapt
ColorSpace adapt (ColorSpace colorSpace, float[] whitePoint, 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 RGB color 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 |
ColorSpace: The color space to chromatically adapt |
whitePoint |
float: The new white point |
adaptation |
ColorSpace.Adaptation: The adaptation matrix |
| Returns | |
|---|---|
ColorSpace |
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 |
adapt
ColorSpace adapt (ColorSpace colorSpace, float[] 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
BRADFORD matrix.
The color space returned by this method always has
an ID of MIN_ID.
| Parameters | |
|---|---|
colorSpace |
ColorSpace: The color space to chromatically adapt |
whitePoint |
float: The new white point |
| Returns | |
|---|---|
ColorSpace |
A ColorSpace instance with the same name, primaries,
transfer functions and range as the specified color space |
connect
ColorSpace.Connector connect (ColorSpace source, 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 D50 as needed.
If the source is the sRGB color space, an optimized connector is returned to avoid unnecessary computations and loss of precision.
| Parameters | |
|---|---|
source |
ColorSpace: The color space to convert colors from |
intent |
ColorSpace.RenderIntent: The render intent to map colors from the source to the destination |
| Returns | |
|---|---|
ColorSpace.Connector |
A non-null connector between the specified color space and sRGB |
connect
ColorSpace.Connector connect (ColorSpace source, ColorSpace destination, 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 D50 as needed.
If the source and destination are the same, an optimized connector is returned to avoid unnecessary computations and loss of precision.
| Parameters | |
|---|---|
source |
ColorSpace: The color space to convert colors from |
destination |
ColorSpace: The color space to convert colors to |
intent |
ColorSpace.RenderIntent: The render intent to map colors from the source to the destination |
| Returns | |
|---|---|
ColorSpace.Connector |
A non-null connector between the two specified color spaces |
connect
ColorSpace.Connector connect (ColorSpace source)
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 D50 as 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 perceptual render intent.
| Parameters | |
|---|---|
source |
ColorSpace: The color space to convert colors from |
| Returns | |
|---|---|
ColorSpace.Connector |
A non-null connector between the specified color space and sRGB |
connect
ColorSpace.Connector connect (ColorSpace source, 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 D50 as 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 perceptual render intent.
| Parameters | |
|---|---|
source |
ColorSpace: The color space to convert colors from |
destination |
ColorSpace: The color space to convert colors to |
| Returns | |
|---|---|
ColorSpace.Connector |
A non-null connector between the two specified color spaces |
equals
boolean equals (Object o)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation
on non-null object references:
- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
The equals method for class Object implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x and
y, this method returns true if and only
if x and y refer to the same object
(x == y has the value true).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode method, which states
that equal objects must have equal hash codes.
| Parameters | |
|---|---|
o |
Object: the reference object with which to compare. |
| Returns | |
|---|---|
boolean |
true if this object is the same as the obj
argument; false otherwise. |
fromXyz
float[] fromXyz (float x,
float y,
float z)
Converts tristimulus values from the CIE XYZ space to this color space's color model.
| Parameters | |
|---|---|
x |
float: The X component of the color value |
y |
float: The Y component of the color value |
z |
float: The Z component of the color value |
| Returns | |
|---|---|
float[] |
A new array whose size is equal to the number of color
components as returned by getComponentCount() |
fromXyz
float[] fromXyz (float[] v)
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
getComponentCount(), and its first 3 values must
be the XYZ components to convert from.
| Parameters | |
|---|---|
v |
float: 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 | |
|---|---|
float[] |
The array passed in parameter |
get
ColorSpace get (ColorSpace.Named name)
Returns an instance of ColorSpace identified by the specified
name. The list of names provided in the ColorSpace.Named enum 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 |
ColorSpace.Named: The name of the color space to get an instance of |
| Returns | |
|---|---|
ColorSpace |
A non-null ColorSpace instance
|
getComponentCount
int getComponentCount ()
Returns the number of components that form a color value according to this color space's color model.
| Returns | |
|---|---|
int |
An integer between 1 and 4 |
See also:
getId
int getId ()
Returns the ID of this color space. Positive IDs match the color
spaces enumerated in ColorSpace.Named. A negative ID indicates a
color space created by calling one of the public constructors.
| Returns | |
|---|---|
int |
An integer between MIN_ID and MAX_ID
|
getMaxValue
float getMaxValue (int component)
Returns the maximum valid value for the specified component of this color space's color model.
| Parameters | |
|---|---|
component |
int: The index of the component |
| Returns | |
|---|---|
float |
A floating point value greater than getMinValue(int) |
See also:
getMinValue
float getMinValue (int component)
Returns the minimum valid value for the specified component of this color space's color model.
| Parameters | |
|---|---|
component |
int: The index of the component |
| Returns | |
|---|---|
float |
A floating point value less than getMaxValue(int) |
See also:
getModel
ColorSpace.Model getModel ()
Return the color model of this color space.
| Returns | |
|---|---|
ColorSpace.Model |
A non-null ColorSpace.Model |
See also:
getName
String 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 | |
|---|---|
String |
A non-null String of length >= 1 |
hashCode
int hashCode ()
Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
HashMap.
The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)method, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
equals(java.lang.Object)method, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
Java™ programming language.)
| Returns | |
|---|---|
int |
a hash code value for this object. |
isSrgb
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
RGB. -
Its primaries are within 1e-3 of the true
sRGBprimaries. -
Its white point is withing 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 range is \([0..1]\).
This method always returns true for SRGB.
| Returns | |
|---|---|
boolean |
True if this color space is the sRGB color space (or a close approximation), false otherwise |
isWideGamut
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
the sRGB gamut and if the area of its gamut is
90% of greater than the area of the NTSC
gamut.
| Returns | |
|---|---|
boolean |
True if this color space is a wide-gamut color space, false otherwise |
match
ColorSpace match (float[] toXYZD50, ColorSpace.Rgb.TransferParameters function)
Returns a ColorSpace.Named instance of ColorSpace that 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 D50 standard illuminant.
| Parameters | |
|---|---|
toXYZD50 |
float: 3x3 column-major transform matrix from RGB to the profile
connection space CIE XYZ as an array of 9 floats, cannot be null |
function |
ColorSpace.Rgb.TransferParameters: Parameters for the transfer functions |
| Returns | |
|---|---|
ColorSpace |
A non-null ColorSpace if a match is found, null otherwise
|
toString
String 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 sRGB
color space is equal to the following value:
sRGB IEC61966-2.1 (id=0, model=RGB)
| Returns | |
|---|---|
String |
A string representation of the object |
toXyz
float[] toXyz (float r,
float g,
float b)
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 a D50
standard illuminant.
This method is a convenience for color spaces with a model
of 3 components (RGB or LAB
for instance). With color spaces using fewer or more components,
use toXyz(float[]) instead
| Parameters | |
|---|---|
r |
float: The first component of the value to convert from (typically R in RGB) |
g |
float: The second component of the value to convert from (typically G in RGB) |
b |
float: The third component of the value to convert from (typically B in RGB) |
| Returns | |
|---|---|
float[] |
A new array of 3 floats, containing tristimulus XYZ values |
toXyz
float[] toXyz (float[] v)
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 a D50
standard illuminant.
The specified array's length must be at least
equal to to the number of color components as returned by
getComponentCount().
| Parameters | |
|---|---|
v |
float: 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 | |
|---|---|
float[] |
The array passed in parameter |
Interfaces
Classes
- Bitmap
- BitmapFactory
- BitmapFactory.Options
- BitmapRegionDecoder
- BitmapShader
- BlurMaskFilter
- Camera
- Canvas
- Color
- ColorFilter
- ColorMatrix
- ColorMatrixColorFilter
- ColorSpace
- ColorSpace.Connector
- ColorSpace.Rgb
- ColorSpace.Rgb.TransferParameters
- ComposePathEffect
- ComposeShader
- CornerPathEffect
- DashPathEffect
- DiscretePathEffect
- DrawFilter
- EmbossMaskFilter
- ImageFormat
- Interpolator
- LightingColorFilter
- LinearGradient
- MaskFilter
- Matrix
- Movie
- NinePatch
- Outline
- Paint
- Paint.FontMetrics
- Paint.FontMetricsInt
- PaintFlagsDrawFilter
- Path
- PathDashPathEffect
- PathEffect
- PathMeasure
- Picture
- PixelFormat
- Point
- PointF
- PorterDuff
- PorterDuffColorFilter
- PorterDuffXfermode
- RadialGradient
- Rect
- RectF
- Region
- RegionIterator
- Shader
- SumPathEffect
- SurfaceTexture
- SweepGradient
- Typeface
- Typeface.Builder
- Xfermode
- YuvImage
Enums
- Bitmap.CompressFormat
- Bitmap.Config
- BlurMaskFilter.Blur
- Canvas.EdgeType
- Canvas.VertexMode
- ColorSpace.Adaptation
- ColorSpace.Model
- ColorSpace.Named
- ColorSpace.RenderIntent
- Interpolator.Result
- Matrix.ScaleToFit
- Paint.Align
- Paint.Cap
- Paint.Join
- Paint.Style
- Path.Direction
- Path.FillType
- Path.Op
- PathDashPathEffect.Style
- PorterDuff.Mode
- Region.Op
- Shader.TileMode
Exceptions