ColorMatrix
-
Cmn
value 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(values: FloatArray) |
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 |
Obtain an instance of the matrix value at the given |
Cmn
|
inline Unit |
reset()Set this colormatrix to identity: |
Cmn
|
Unit |
set(src: ColorMatrix)Assign the |
Cmn
|
inline operator Unit |
Cmn
|
|
Unit |
setToRotateBlue(degrees: Float)Rotate by |
Cmn
|
Unit |
setToRotateGreen(degrees: Float)Rotate by |
Cmn
|
Unit |
setToRotateRed(degrees: Float)Rotate by |
Cmn
|
Unit |
setToSaturation(sat: Float)Set the matrix to affect the saturation of colors. |
Cmn
|
Unit |
setToScale(Create a |
Cmn
|
operator Unit |
timesAssign(colorMatrix: ColorMatrix)Multiply this matrix by |
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
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 |
column: Int |
Column index to query the ColorMatrix value. Range is from 0 to 4 as |
reset
inline 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 |
column: Int |
Column index to query the ColorMatrix value. Range is from 0 to 4 as |
v: Float |
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
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
timesAssign
operator fun timesAssign(colorMatrix: ColorMatrix): Unit
Multiply this matrix by colorMatrix and assign the result to this matrix.