{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ColorMatrix

value public final class 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

ColorMatrix(@NonNull float[] values)

Public methods

final void

Set the matrix to convert RGB to YUV

final void

Set the matrix to convert from YUV to RGB

final float
get(int row, int column)

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

final @NonNull float[]
final void

Set this colormatrix to identity:

final void

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

final void
set(int row, int column, float v)

Set the matrix value at the given row and column.

final void
setToRotateBlue(float degrees)

Rotate by degrees along the blue color axis

final void
setToRotateGreen(float degrees)

Rotate by degrees along the green color axis

final void
setToRotateRed(float degrees)

Rotate by degrees along the red color axis

final void
setToSaturation(float sat)

Set the matrix to affect the saturation of colors.

final void
setToScale(
    float redScale,
    float greenScale,
    float blueScale,
    float alphaScale
)

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

final void

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

Public constructors

ColorMatrix

public ColorMatrix(@NonNull float[] values)

Public methods

convertRgbToYuv

public final void convertRgbToYuv()

Set the matrix to convert RGB to YUV

convertYuvToRgb

public final void convertYuvToRgb()

Set the matrix to convert from YUV to RGB

get

public final float get(int row, int column)

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
int row

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

int column

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

getValues

public final @NonNull float[] getValues()

reset

public final void reset()

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

public final void set(@NonNull ColorMatrix src)

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

set

public final void set(int row, int column, float v)

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
int row

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

int column

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

setToRotateBlue

public final void setToRotateBlue(float degrees)

Rotate by degrees along the blue color axis

setToRotateGreen

public final void setToRotateGreen(float degrees)

Rotate by degrees along the green color axis

setToRotateRed

public final void setToRotateRed(float degrees)

Rotate by degrees along the red color axis

setToSaturation

public final void setToSaturation(float sat)

Set the matrix to affect the saturation of colors.

Parameters
float sat

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

setToScale

public final void setToScale(
    float redScale,
    float greenScale,
    float blueScale,
    float alphaScale
)

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

Parameters
float redScale

Desired scale parameter for the red channel

float greenScale

Desired scale parameter for the green channel

float blueScale

Desired scale parameter for the blue channel

float alphaScale

Desired scale parameter for the alpha channel

timesAssign

public final void timesAssign(@NonNull ColorMatrix colorMatrix)

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