Connector

Class

Common
open class Connector
/**
 * To connect between color spaces, we might need to use adapted transforms. This should be
 * transparent to the user so this constructor takes the original source and destinations (returned
 * by the getters), as well as possibly adapted color spaces used by transform().
 */
internal constructor(
    /**
     * Returns the source color space this connector will convert from.
     *
     * @return A non-null instance of [ColorSpace]
     * @see destination
     */
    val source: ColorSpace,
    /**
     * Returns the destination color space this connector will convert to.
     *
     * @return A non-null instance of [ColorSpace]
     * @see source
     */
    val destination: ColorSpace,
    private val transformSource: ColorSpace,
    private val transformDestination: ColorSpace,
    /**
     * Returns the render intent this connector will use when mapping the source color space to the
     * destination color space.
     *
     * @return A non-null [RenderIntent]
     * @see RenderIntent
     */
    val renderIntent: RenderIntent,
    private val transform: FloatArray?,
)

A connector transforms colors from a source color space to a destination color space.

A source color space is connected to a destination color space using the color transform C computed from their respective transforms noted Tsrc and Tdst in the following equation:

See equation(https://developer.android.com/reference/android/graphics/ColorSpace.Connector)

The transform C shown above is only valid when the source and destination color spaces have the same profile connection space (PCS). We know that instances of ColorSpace always use CIE XYZ as their PCS but their white points might differ. When they do, we must perform a chromatic adaptation of the color spaces' transforms. To do so, we use the von Kries method described in the documentation of Adaptation, using the CIE standard illuminant D50 as the target white point.

Example of conversion from sRGB to DCI-P3: val connector = ColorSpaces.Srgb.connect(ColorSpaces.DciP3); val p3 = connector.transform(1.0f, 0.0f, 0.0f); // p3 contains { 0.9473, 0.2740, 0.2076 }

Secondary Constructors

internal constructor(
    source: ColorSpace,
    destination: ColorSpace,
    intent: RenderIntent,
) : this(
    source,
    destination,
    if (source.model == ColorModel.Rgb) source.adapt(Illuminant.D50) else source,
    if (destination.model == ColorModel.Rgb) {
        destination.adapt(Illuminant.D50)
    } else {
        destination
    },
    intent,
    computeTransform(source, destination, intent),
)

Creates a new connector between a source and a destination color space.

Parameters

sourceThe source color space, cannot be null
destinationThe destination color space, cannot be null
intentThe render intent to use when compressing gamuts

Functions

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

Transforms the specified color from the source color space to a color in the destination color space. This convenience method assumes a source color model with 3 components (typically RGB). To transform from color models with more than 3 components, such as CMYK, use transform instead.

Parameters

rThe red component of the color to transform
gThe green component of the color to transform
bThe blue component of the color to transform

Returns

A new array of 3 floats containing the specified color transformed from the source space to the destination space
@Size(min = 3)
    open fun transform(@Size(min = 3) v: FloatArray): FloatArray

Transforms the specified color from the source color space to a color in the destination color space.

Parameters

vA non-null array of 3 floats containing the value to transform and that will hold the result of the transform

Returns

The v array passed as a parameter, containing the specified color transformed from the source space to the destination space

Companion Object