---
title: "ColorMatrix"
description: "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 <code>[[R, G, B, A]]</code>, 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 <code>[[R', G', B', A']]</code> then has each channel clamped to the
<code>0</code> to <code>255</code> range.

The sample ColorMatrix below inverts incoming colors by scaling each channel by <code>-1</code>,
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]"
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
value class ColorMatrix(
    val values: FloatArray =
        floatArrayOf(1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f)
)
```


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 <code>``R, G, B, A``</code>, 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 <code>``R', G', B', A'``</code> then has each channel clamped to the
<code>0</code> to <code>255</code> range.

The sample ColorMatrix below inverts incoming colors by scaling each channel by <code>-1</code>,
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`


## Functions

```kotlin
inline operator fun get(row: Int, column: Int) = values[(row * 5) + 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

| | |
| --- | --- |
| row | Row index to query the ColorMatrix value. Range is from 0 to 3 as `ColorMatrix` is represented as a 4 x 5 matrix |
| column | Column index to query the ColorMatrix value. Range is from 0 to 4 as `ColorMatrix` is represented as a 4 x 5 matrix |



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


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 | Row index to query the ColorMatrix value. Range is from 0 to 3 as `ColorMatrix` is represented as a 4 x 5 matrix |
| column | Column index to query the ColorMatrix value. Range is from 0 to 4 as `ColorMatrix` is represented as a 4 x 5 matrix |
| v | value to update at the given `row` and `column` |



```kotlin
inline fun 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
```


```kotlin
fun set(src: ColorMatrix)
```


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


```kotlin
operator fun timesAssign(colorMatrix: ColorMatrix)
```


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


```kotlin
fun setToSaturation(sat: Float)
```


Set the matrix to affect the saturation of colors.

#### Parameters

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



```kotlin
fun 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

#### Parameters

| | |
| --- | --- |
| redScale | Desired scale parameter for the red channel |
| greenScale | Desired scale parameter for the green channel |
| blueScale | Desired scale parameter for the blue channel |
| alphaScale | Desired scale parameter for the alpha channel |



```kotlin
fun setToRotateRed(degrees: Float)
```


Rotate by `degrees` along the red color axis


```kotlin
fun setToRotateGreen(degrees: Float)
```


Rotate by `degrees` along the green color axis


```kotlin
fun setToRotateBlue(degrees: Float)
```


Rotate by `degrees` along the blue color axis


```kotlin
fun convertRgbToYuv()
```


Set the matrix to convert RGB to YUV


```kotlin
fun convertYuvToRgb()
```


Set the matrix to convert from YUV to RGB



