Google is committed to advancing racial equity for Black communities. See how.

Rgb

class Rgb : ColorSpace
kotlin.Any
   ↳ androidx.compose.ui.graphics.colorspace.ColorSpace
   ↳ androidx.compose.ui.graphics.colorspace.Rgb

An RGB color space is an additive color space using the RGB color model (a color is therefore represented by a tuple of 3 numbers).

A specific RGB color space is defined by the following properties:

  • Three chromaticities of the red, green and blue primaries, which define the gamut of the color space.
  • A white point chromaticity that defines the stimulus to which color space values are normalized (also just called "white").
  • An opto-electronic transfer function, also called opto-electronic conversion function or often, and approximately, gamma function.
  • An electro-optical transfer function, also called electo-optical conversion function or often, and approximately, gamma function.
  • A range of valid RGB values (most commonly [0..1]).

The most commonly used RGB color space is sRGB.

Primaries and white point chromaticities

In this implementation, the chromaticity of the primaries and the white point of an RGB color space is defined in the CIE xyY color space. This color space separates the chromaticity of a color, the x and y components, and its luminance, the Y component. Since the primaries and the white point have full brightness, the Y component is assumed to be 1 and only the x and y components are needed to encode them.

For convenience, this implementation also allows to define the primaries and white point in the CIE XYZ space. The tristimulus XYZ values are internally converted to xyY.

sRGB primaries and white point

Transfer functions

A transfer function is a color component conversion function, defined as a single variable, monotonic mathematical function. It is applied to each individual component of a color. They are used to perform the mapping between linear tristimulus values and non-linear electronic signal value.

The opto-electronic transfer function (OETF or OECF) encodes tristimulus values in a scene to a non-linear electronic signal value. An OETF is often expressed as a power function with an exponent between 0.38 and 0.55 (the reciprocal of 1.8 to 2.6).

The electro-optical transfer function (EOTF or EOCF) decodes a non-linear electronic signal value to a tristimulus value at the display. An EOTF is often expressed as a power function with an exponent between 1.8 and 2.6.

Transfer functions are used as a compression scheme. For instance, linear sRGB values would normally require 11 to 12 bits of precision to store all values that can be perceived by the human eye. When encoding sRGB values using the appropriate OETF (see sRGB for an exact mathematical description of that OETF), the values can be compressed to only 8 bits precision.

When manipulating RGB values, particularly sRGB values, it is safe to assume that these values have been encoded with the appropriate OETF (unless noted otherwise). Encoded values are often said to be in "gamma space". They are therefore defined in a non-linear space. This in turns means that any linear operation applied to these values is going to yield mathematically incorrect results (any linear interpolation such as gradient generation for instance, most image processing functions such as blurs, etc.).

To properly process encoded RGB values you must first apply the EOTF to decode the value into linear space. After processing, the RGB value must be encoded back to non-linear ("gamma") space. Here is a formal description of the process, where f is the processing function to apply:

See RGB equation

If the transfer functions of the color space can be expressed as an ICC parametric curve as defined in ICC.1:2004-10, the numeric parameters can be retrieved from transferParameters. This can be useful to match color spaces for instance.

Some RGB color spaces, such as ColorSpaces.Aces and scRGB, are said to be linear because their transfer functions are the identity function: f(x) = x. If the source and/or destination are known to be linear, it is not necessary to invoke the transfer functions.

Range

Most RGB color spaces allow RGB values in the range [0..1]. There are however a few RGB color spaces that allow much larger ranges. For instance, scRGB is used to manipulate the range [-0.5..7.5] while ACES can be used throughout the range [-65504, 65504].

Extended sRGB and its large range

Converting between RGB color spaces

Conversion between two color spaces is achieved by using an intermediate color space called the profile connection space (PCS). The PCS used by this implementation is CIE XYZ. The conversion operation is defined as such:

See RGB equation

Where Tsrc is the RGB to XYZ transform of the source color space and Tdst^-1 the XYZ to RGB transform of the destination color space.

Many RGB color spaces commonly used with electronic devices use the standard illuminant D65. Care must be take however when converting between two RGB color spaces if their white points do not match. This can be achieved by either calling adapt to adapt one or both color spaces to a single common white point. This can be achieved automatically by calling ColorSpace.connect, which also handles non-RGB color spaces.

To learn more about the white point adaptation process, refer to the documentation of Adaptation.

Summary

Public constructors
<init>(@Size(1) name: String, @Size(9) toXYZ: FloatArray, oetf: (Double) -> Double, eotf: (Double) -> Double)

Creates a new RGB color space using a 3x3 column-major transform matrix.

<init>(@Size(1) name: String, @Size(6, 9) primaries: FloatArray, whitePoint: WhitePoint, oetf: (Double) -> Double, eotf: (Double) -> Double, min: Float, max: Float)

Creates a new RGB color space using a specified set of primaries and a specified white point.

<init>(@Size(1) name: String, @Size(9) toXYZ: FloatArray, function: TransferParameters)

Creates a new RGB color space using a 3x3 column-major transform matrix.

<init>(@Size(1) name: String, @Size(6, 9) primaries: FloatArray, whitePoint: WhitePoint, function: TransferParameters)

Creates a new RGB color space using a specified set of primaries and a specified white point.

<init>(@Size(1) name: String, @Size(9) toXYZ: FloatArray, gamma: Double)

Creates a new RGB color space using a 3x3 column-major transform matrix.

<init>(@Size(1) name: String, @Size(6, 9) primaries: FloatArray, whitePoint: WhitePoint, gamma: Double)

Creates a new RGB color space using a specified set of primaries and a specified white point.

Public methods
Boolean
equals(other: Any?)

FloatArray
fromLinear(r: Float, g: Float, b: Float)

Encodes an RGB value from linear space to this color space's "gamma space".

FloatArray
fromLinear(@Size(3) v: FloatArray)

Encodes an RGB value from linear space to this color space's "gamma space".

FloatArray
fromXyz(@Size(3) v: FloatArray)

Converts tristimulus values from the CIE XYZ space to this color space's color model.

FloatArray

Returns the inverse transform of this color space as a new array.

FloatArray
getInverseTransform(@Size(9) inverseTransform: FloatArray)

Copies the inverse transform of this color space in specified array.

Float
getMaxValue(component: Int)

Returns the maximum valid value for the specified component of this color space's color model.

Float
getMinValue(component: Int)

Returns the minimum valid value for the specified component of this color space's color model.

FloatArray

Returns the primaries of this color space as a new array of 6 floats.

FloatArray
getPrimaries(@Size(6) primaries: FloatArray)

Copies the primaries of this color space in specified array.

FloatArray

Returns the transform of this color space as a new array.

FloatArray
getTransform(@Size(9) transform: FloatArray)

Copies the transform of this color space in specified array.

Int

FloatArray
toLinear(r: Float, g: Float, b: Float)

Decodes an RGB value to linear space.

FloatArray
toLinear(@Size(3) v: FloatArray)

Decodes an RGB value to linear space.

FloatArray
toXyz(@Size(3) v: FloatArray)

Converts a color value from this color space's model to tristimulus CIE XYZ values.

Inherited functions
Inherited extension functions
From androidx.compose.ui.graphics.colorspace
ColorSpace
ColorSpace.adapt(whitePoint: WhitePoint, adaptation: Adaptation = Adaptation.Bradford)

Performs the chromatic adaptation of a color space from its native white point to the specified white point.

Connector
ColorSpace.connect(destination: ColorSpace = ColorSpaces.Srgb, intent: RenderIntent = RenderIntent.Perceptual)

Connects two color spaces to allow conversion from the source color space to the destination color space.

Properties
(Double) -> Double

Returns the electro-optical transfer function (EOTF) of this color space.

Boolean

Indicates whether this color space is the sRGB color space or equivalent to the sRGB color space.

Boolean

Returns whether this color space is a wide-gamut color space.

(Double) -> Double

Returns the opto-electronic transfer function (OETF) of this color space.

TransferParameters?

Returns the parameters used by the electro-optical and opto-electronic transfer functions.

WhitePoint

Inherited properties

Public constructors

<init>

Rgb(
    @Size(1) name: String,
    @Size(9) toXYZ: FloatArray,
    oetf: (Double) -> Double,
    eotf: (Double) -> Double)

Creates a new RGB color space using a 3x3 column-major transform matrix. The transform matrix must convert from the RGB space to the profile connection space CIE XYZ.

The range of the color space is imposed to be [0..1].

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
toXYZ: FloatArray 3x3 column-major transform matrix from RGB to the profile connection space CIE XYZ as an array of 9 floats, cannot be null
oetf: (Double) -> Double Opto-electronic transfer function, cannot be null
eotf: (Double) -> Double Electro-optical transfer function, cannot be null
Exceptions
IllegalArgumentException If any of the following conditions is met:

<init>

Rgb(
    @Size(1) name: String,
    @Size(6, 9) primaries: FloatArray,
    whitePoint: WhitePoint,
    oetf: (Double) -> Double,
    eotf: (Double) -> Double,
    min: Float,
    max: Float)

Creates a new RGB color space using a specified set of primaries and a specified white point.

The primaries and white point can be specified in the CIE xyY space or in CIE XYZ. The length of the arrays depends on the chosen space:

| Spaces | Primaries length | White point length |
|--------|------------------|--------------------|
| xyY    | 6                | 2                  |
| XYZ    | 9                | 3                  |

When the primaries and/or white point are specified in xyY, the Y component does not need to be specified and is assumed to be 1.0. Only the xy components are required.

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
primaries: FloatArray RGB primaries as an array of 6 (xy) or 9 (XYZ) floats
whitePoint: WhitePoint Reference white as an array of 2 (xy) or 3 (XYZ) floats
oetf: (Double) -> Double Opto-electronic transfer function, cannot be null
eotf: (Double) -> Double Electro-optical transfer function, cannot be null
min: Float The minimum valid value in this color space's RGB range
max: Float The maximum valid value in this color space's RGB range
Exceptions
IllegalArgumentException If any of the following conditions is met:

<init>

Rgb(
    @Size(1) name: String,
    @Size(9) toXYZ: FloatArray,
    function: TransferParameters)

Creates a new RGB color space using a 3x3 column-major transform matrix. The transform matrix must convert from the RGB space to the profile connection space CIE XYZ.

The range of the color space is imposed to be [0..1].

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
toXYZ: FloatArray 3x3 column-major transform matrix from RGB to the profile connection space CIE XYZ as an array of 9 floats, cannot be null
function: TransferParameters Parameters for the transfer functions
Exceptions
IllegalArgumentException If any of the following conditions is met:

<init>

Rgb(
    @Size(1) name: String,
    @Size(6, 9) primaries: FloatArray,
    whitePoint: WhitePoint,
    function: TransferParameters)

Creates a new RGB color space using a specified set of primaries and a specified white point.

The primaries and white point can be specified in the CIE xyY space or in CIE XYZ. The length of the arrays depends on the chosen space:

| Spaces | Primaries length | White point length |
|--------|------------------|--------------------|
| xyY    | 6                | 2                  |
| XYZ    | 9                | 3                  |

When the primaries and/or white point are specified in xyY, the Y component does not need to be specified and is assumed to be 1.0. Only the xy components are required.

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
primaries: FloatArray RGB primaries as an array of 6 (xy) or 9 (XYZ) floats
whitePoint: WhitePoint Reference white as an array of 2 (xy) or 3 (XYZ) floats
function: TransferParameters Parameters for the transfer functions
Exceptions
IllegalArgumentException If any of the following conditions is met:

<init>

Rgb(
    @Size(1) name: String,
    @Size(9) toXYZ: FloatArray,
    gamma: Double)

Creates a new RGB color space using a 3x3 column-major transform matrix. The transform matrix must convert from the RGB space to the profile connection space CIE XYZ.

The range of the color space is imposed to be [0..1].

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
toXYZ: FloatArray 3x3 column-major transform matrix from RGB to the profile connection space CIE XYZ as an array of 9 floats, cannot be null
gamma: Double Gamma to use as the transfer function
Exceptions
IllegalArgumentException If any of the following conditions is met:

See Also

<init>

Rgb(
    @Size(1) name: String,
    @Size(6, 9) primaries: FloatArray,
    whitePoint: WhitePoint,
    gamma: Double)

Creates a new RGB color space using a specified set of primaries and a specified white point.

The primaries and white point can be specified in the CIE xyY space or in CIE XYZ. The length of the arrays depends on the chosen space:

| Spaces | Primaries length | White point length |
|--------|------------------|--------------------|
| xyY    | 6                | 2                  |
| XYZ    | 9                | 3                  |

When the primaries and/or white point are specified in xyY, the Y component does not need to be specified and is assumed to be 1.0. Only the xy components are required.

Parameters
name: String Name of the color space, cannot be null, its length must be >= 1
primaries: FloatArray RGB primaries as an array of 6 (xy) or 9 (XYZ) floats
whitePoint: WhitePoint Reference white as an array of 2 (xy) or 3 (XYZ) floats
gamma: Double Gamma to use as the transfer function
Exceptions
IllegalArgumentException If any of the following conditions is met:

See Also

Public methods

equals

fun equals(other: Any?): Boolean

fromLinear

@Size(3) fun fromLinear(
    r: Float,
    g: Float,
    b: Float
):