# ColorMatrix

Artifact: [androidx.compose.ui:ui-graphics](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/graphics/ColorMatrix.kt+class:androidx.compose.ui.graphics.ColorMatrix) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0) Common/AllAndroid/JVM
- 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:

```kotlin
[ 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:

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

```kotlin
[ -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 `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorFilter#colorMatrix(androidx.compose.ui.graphics.ColorMatrix)` and applied at draw time through `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Paint#colorFilter()`

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#ColorMatrix(kotlin.FloatArray)(values: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html)` | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#convertRgbToYuv()()` Set the matrix to convert RGB to YUV | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#convertYuvToRgb()()` Set the matrix to convert from YUV to RGB | Cmn |
| `inline operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#get(kotlin.Int,kotlin.Int)(row: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, column: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Obtain an instance of the matrix value at the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#get(kotlin.Int,kotlin.Int)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#get(kotlin.Int,kotlin.Int)`. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#reset()()` Set this colormatrix to identity: | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(androidx.compose.ui.graphics.ColorMatrix)(src: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix)` Assign the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(androidx.compose.ui.graphics.ColorMatrix)` colormatrix into this matrix, copying all of its values. | Cmn |
| `inline operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)(row: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, column: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, v: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Set the matrix value at the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateBlue(kotlin.Float)(degrees: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateBlue(kotlin.Float)` along the blue color axis | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateGreen(kotlin.Float)(degrees: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateGreen(kotlin.Float)` along the green color axis | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateRed(kotlin.Float)(degrees: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateRed(kotlin.Float)` along the red color axis | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToSaturation(kotlin.Float)(sat: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Set the matrix to affect the saturation of colors. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToScale(kotlin.Float,kotlin.Float,kotlin.Float,kotlin.Float)( redScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, greenScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, blueScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, alphaScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html )` Create a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` with the corresponding scale parameters for the red, green, blue and alpha axes | Cmn |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#timesAssign(androidx.compose.ui.graphics.ColorMatrix)(colorMatrix: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix)` Multiply this matrix by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#timesAssign(androidx.compose.ui.graphics.ColorMatrix)` and assign the result to this matrix. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float-array/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#values()` | Cmn |

## Public constructors

### ColorMatrix

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

```
fun convertRgbToYuv(): Unit
```

Set the matrix to convert RGB to YUV

### convertYuvToRgb

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

```
fun convertYuvToRgb(): Unit
```

Set the matrix to convert from YUV to RGB

### get

Cmn Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.1.0)

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

Obtain an instance of the matrix value at the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#get(kotlin.Int,kotlin.Int)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#get(kotlin.Int,kotlin.Int)`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Row index to query the ColorMatrix value. Range is from 0 to 3 as `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` is represented as a 4 x 5 matrix |
| `column: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Column index to query the ColorMatrix value. Range is from 0 to 4 as `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` is represented as a 4 x 5 matrix |

### reset

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

```
inline fun reset(): Unit
```

Set this colormatrix to identity:

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

Cmn Added in [1.10.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.10.0)

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

Assign the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(androidx.compose.ui.graphics.ColorMatrix)` colormatrix into this matrix, copying all of its values.

### set

Cmn Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.1.0)

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

Set the matrix value at the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Row index to query the ColorMatrix value. Range is from 0 to 3 as `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` is represented as a 4 x 5 matrix |
| `column: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Column index to query the ColorMatrix value. Range is from 0 to 4 as `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` is represented as a 4 x 5 matrix |
| `v: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | value to update at the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#set(kotlin.Int,kotlin.Int,kotlin.Float)` |

### setToRotateBlue

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateBlue(kotlin.Float)` along the blue color axis

### setToRotateGreen

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateGreen(kotlin.Float)` along the green color axis

### setToRotateRed

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Rotate by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#setToRotateRed(kotlin.Float)` along the red color axis

### setToSaturation

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Set the matrix to affect the saturation of colors.

| Parameters |
|---|---|
| `sat: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | A value of 0 maps the color to gray-scale. 1 is identity. |

### setToScale

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

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

Create a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix` with the corresponding scale parameters for the red, green, blue and alpha axes

| Parameters |
|---|---|
| `redScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | Desired scale parameter for the red channel |
| `greenScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | Desired scale parameter for the green channel |
| `blueScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | Desired scale parameter for the blue channel |
| `alphaScale: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | Desired scale parameter for the alpha channel |

### timesAssign

Cmn Added in [1.10.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.10.0)

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

Multiply this matrix by `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix#timesAssign(androidx.compose.ui.graphics.ColorMatrix)` and assign the result to this matrix.

## Public properties

### values

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val values: FloatArray
```