public class Rgb internal constructor(
name: String,
primaries: FloatArray,
public val whitePoint: WhitePoint,
transform: FloatArray?,
oetf: DoubleFunction,
eotf: DoubleFunction,
private val min: Float,
private val max: Float,
/**
* Returns the parameters used by the [electro-optical][eotf] and [opto-electronic][oetf]
* transfer functions. If the transfer functions do not match the ICC parametric curves defined
* in ICC.1:2004-10 (section 10.15), this method returns null.
*
* See [TransferParameters] for a full description of the transfer functions.
*
* @return An instance of [TransferParameters] or null if this color space's transfer functions
* do not match the equation defined in [TransferParameters]
*/
public val transferParameters: TransferParameters?,
id: Int,
) : ColorSpace(name, ColorModel.Rgb, id)
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
- A white point chromaticity that defines the stimulus to which color space values are normalized
- An opto-electronic transfer function, also called opto-electronic conversion function or often,
- An electro-optical transfer function, also called electo-optical conversion function or often,
- A range of valid RGB values (most commonly `
0..1`).
space.
(also just called "white").
and approximately, gamma function.
and approximately, gamma function.
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:
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:
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.
Properties
oetf
public val oetf: (Double) -> Double = { x ->
oetfOrig(x).coerceIn(min.toDouble(), max.toDouble())
}
Returns the opto-electronic transfer function (OETF) of this color space. The inverse function is the electro-optical transfer function (EOTF) returned by eotf. These functions are defined to satisfy the following equality for x ∈ `0..1`:
OETF(EOTF(x) = EOTF(OETF(x)) = x
For RGB colors, this function can be used to convert from linear space to "gamma space" (gamma encoded). The terms gamma space and gamma encoded are frequently used because many OETFs can be closely approximated using a simple power function of the form x^γ (the approximation of the sRGB OETF uses γ = 2.2 for instance).
Return
A transfer function that converts from linear space to "gamma space"
eotf
public val eotf: (Double) -> Double = { x ->
eotfOrig(x.coerceIn(min.toDouble(), max.toDouble()))
}
Returns the electro-optical transfer function (EOTF) of this color space. The inverse function is the opto-electronic transfer function (OETF) returned by oetf. These functions are defined to satisfy the following equality for x in `0..1`:
OETF(EOTF(x) = EOTF(OETF(x)) = x
For RGB colors, this function can be used to convert from "gamma space" (gamma encoded) to linear space. The terms gamma space and gamma encoded are frequently used because many EOTFs can be closely approximated using a simple power function of the form x^γ (the approximation of the sRGB EOTF uses γ = 2.2 for instance).
Return
A transfer function that converts from "gamma space" to linear space
isWideGamut
public override val isWideGamut: Boolean
isSrgb
public override val isSrgb: Boolean
Functions
getPrimaries
public fun getPrimaries(@Size(min = 6) primaries: FloatArray): FloatArray
Copies the primaries of this color space in specified array. The Y component is assumed to be 1 and is therefore not copied into the destination. The x and y components of the first primary are written in the array at positions 0 and 1 respectively.
Parameters
| primaries | The destination array, cannot be null, its length must be >= 6 |
Return
primaries array, modified to contain the primaries of this color space.
getTransform
public fun getTransform(@Size(min = 9) transform: FloatArray): FloatArray
Copies the transform of this color space in specified array. The transform is used to convert from RGB to XYZ (with the same white point as this color space). To connect color spaces, you must first adapt them to the same white point.
It is recommended to use ColorSpace.connect to convert between color spaces.
Parameters
| transform | The destination array, cannot be null, its length must be >= 9 |
Return
transform, modified to contain the transform for this color space.
getInverseTransform
public fun getInverseTransform(@Size(min = 9) inverseTransform: FloatArray): FloatArray
Copies the inverse transform of this color space in specified array. The inverse transform is used to convert from XYZ to RGB (with the same white point as this color space). To connect color spaces, you must first adapt them to the same white point.
It is recommended to use ColorSpace.connect to convert between color spaces.
Parameters
| inverseTransform | The destination array, cannot be null, its length must be >= 9 |
Return
The inverseTransform array passed as a parameter, modified to contain the inverse transform of this color space.
toLinear
public fun toLinear(r: Float, g: Float, b: Float): FloatArray
Decodes an RGB value to linear space. This is achieved by applying this color space's electro-optical transfer function to the supplied values.
Refer to the documentation of Rgb for more information about transfer functions and their use for encoding and decoding RGB values.
Parameters
| r | The red component to decode to linear space |
| g | The green component to decode to linear space |
| b | The blue component to decode to linear space |
Return
A new array of 3 floats containing linear RGB values
toLinear
public fun toLinear(@Size(min = 3) v: FloatArray): FloatArray
Decodes an RGB value to linear space. This is achieved by applying this color space's electro-optical transfer function to the first 3 values of the supplied array. The result is stored back in the input array.
Refer to the documentation of Rgb for more information about transfer functions and their use for encoding and decoding RGB values.
Parameters
| v | A non-null array of non-linear RGB values, its length must be at least 3 |
Return
v, containing linear RGB values
fromLinear
public fun fromLinear(r: Float, g: Float, b: Float): FloatArray
Encodes an RGB value from linear space to this color space's "gamma space". This is achieved by applying this color space's opto-electronic transfer function to the supplied values.
Refer to the documentation of Rgb for more information about transfer functions and their use for encoding and decoding RGB values.
Parameters
| r | The red component to encode from linear space |
| g | The green component to encode from linear space |
| b | The blue component to encode from linear space |
Return
A new array of 3 floats containing non-linear RGB values
fromLinear
public fun fromLinear(@Size(min = 3) v: FloatArray): FloatArray
Encodes an RGB value from linear space to this color space's "gamma space". This is achieved by applying this color space's opto-electronic transfer function to the first 3 values of the supplied array. The result is stored back in the input array.
Refer to the documentation of Rgb for more information about transfer functions and their use for encoding and decoding RGB values.
Parameters
| v | A non-null array of linear RGB values, its length must be at least 3 |
Return
v, containing non-linear RGB values
toXy
override fun toXy(v0: Float, v1: Float, v2: Float): Long
toZ
override fun toZ(v0: Float, v1: Float, v2: Float): Float
xyzaToColor
override fun xyzaToColor(
x: Float,
y: Float,
z: Float,
a: Float,
colorSpace: ColorSpace,
): Color