ColorMatrix


4x5 matrix for transforming the color and alpha components of a source. The matrix can be passed as single array, and is treated as follows:

[ a, b, c, d, e,
f, g, h, i, j,
k, l, m, n, o,
p, q, r, s, t ]

When applied to a color [R, G, B, A], the resulting color is computed as:

R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;</pre>

That resulting color [R', G', B', A'] then has each channel clamped to the 0 to 255 range.

The sample ColorMatrix below inverts incoming colors by scaling each channel by -1, and then shifting the result up by 255 to remain in the standard color space.

[ -1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0 ]

This is often used as input for ColorFilter.colorMatrix and applied at draw time through Paint.colorFilter

Summary

Public constructors

Cmn

Public functions

Unit

Set the matrix to convert RGB to YUV

Cmn
Unit

Set the matrix to convert from YUV to RGB

Cmn
inline operator Float
get(row: Int, column: Int)

Obtain an instance of the matrix value at the given row and column.

Cmn
Unit

Set this colormatrix to identity:

Cmn
Unit

Assign the src colormatrix into this matrix, copying all of its values.

Cmn
inline operator Unit
set(row: Int, column: Int, v: Float)

Set the matrix value at the given row and column.

Cmn
Unit

Rotate by degrees along the blue color axis

Cmn
Unit

Rotate by degrees along the green color axis

Cmn
Unit

Rotate by degrees along the red color axis

Cmn
Unit

Set the matrix to affect the saturation of colors.

Cmn
Unit
setToScale(
    redScale: Float,
    greenScale: Float,
    blueScale: Float,
    alphaScale: Float
)

Create a ColorMatrix with the corresponding scale parameters for the red, green, blue and alpha axes

Cmn
operator Unit
timesAssign(colorMatrix: ColorMatrix)

Multiply this matrix by colorMatrix and assign the result to this matrix.

Cmn

Public properties

FloatArray
Cmn

Public constructors

ColorMatrix

ColorMatrix(
    values: FloatArray = floatArrayOf( 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f )
)

Public functions

convertRgbToYuv

fun convertRgbToYuv(): Unit

Set the matrix to convert RGB to YUV

convertYuvToRgb

fun convertYuvToRgb(): Unit

Set the matrix to convert from YUV to RGB

get

inline operator fun get(row: Int, column: Int): Float

Obtain an instance of the matrix value at the given row and column. ColorMatrix follows row major order in regards to the positions of matrix values within the flattened array. That is, content order goes from left to right then top to bottom as opposed to column major order.

Parameters
row: Int

Row index to query the ColorMatrix value. Range is from 0 to 3 as ColorMatrix is represented as a 4 x 5 matrix

column: Int

Column index to query the ColorMatrix value. Range is from 0 to 4 as ColorMatrix is represented as a 4 x 5 matrix

reset

fun reset(): Unit

Set this colormatrix to identity:

[ 1 0 0 0 0   - red vector
0 1 0 0 0 - green vector
0 0 1 0 0 - blue vector
0 0 0 1 0 ] - alpha vector

set

fun set(src: ColorMatrix): Unit

Assign the src colormatrix into this matrix, copying all of its values.

set

inline operator fun set(row: Int, column: Int, v: Float): Unit

Set the matrix value at the given row and column. ColorMatrix follows row major order in regards to the positions of matrix values within the flattened array. That is, content order goes from left to right then top to bottom as opposed to column major order.

Parameters
row: Int

Row index to query the ColorMatrix value. Range is from 0 to 3 as ColorMatrix is represented as a 4 x 5 matrix

column: Int

Column index to query the ColorMatrix value. Range is from 0 to 4 as ColorMatrix is represented as a 4 x 5 matrix

setToRotateBlue

fun setToRotateBlue(degrees: Float): Unit

Rotate by degrees along the blue color axis

setToRotateGreen

fun setToRotateGreen(degrees: Float): Unit

Rotate by degrees along the green color axis

setToRotateRed

fun setToRotateRed(degrees: Float): Unit

Rotate by degrees along the red color axis

setToSaturation

fun setToSaturation(sat: Float): Unit

Set the matrix to affect the saturation of colors.

Parameters
sat: Float

A value of 0 maps the color to gray-scale. 1 is identity.

setToScale

fun setToScale(
    redScale: Float,
    greenScale: Float,
    blueScale: Float,
    alphaScale: Float
): Unit

Create a ColorMatrix with the corresponding scale parameters for the red, green, blue and alpha axes

Parameters
redScale: Float

Desired scale parameter for the red channel

greenScale: Float

Desired scale parameter for the green channel

blueScale: Float

Desired scale parameter for the blue channel

alphaScale: Float

Desired scale parameter for the alpha channel

timesAssign

operator fun timesAssign(colorMatrix: ColorMatrix): Unit

Multiply this matrix by colorMatrix and assign the result to this matrix.

Public properties

values

val valuesFloatArray