**MFColorMatrix**class from my

**MFLib**.

A color matrix is a 4x5 matrix for transforming the color and alpha components of a Bitmap.

The definition of the color matrix:

B4X:

```
Input R G B A O
e r l l f
d e u p f
e e h s
n a e
t
v v v v
Red < [01][02][03][04][05]
Output Green < [06][07][08][09][10]
Blue < [11][12][13][14][15]
Alpha < [16][17][18][19][20]
```

Therefore the columns defines the input for the RGBA values and the rows defines the output.

The parts of the color value (red, green, blue and alpha) will be multiplied with the corresponding matrix node, then all results will be added to define the output value.

To define the value for the red channel of the output color:

*The red value of the input color is multiplied with the value in matrix node [01].*

The green value of the input color is multiplied with the value in matrix node [02].

The blue value of the input color is multiplied with the value in matrix node [03].

The alpha value of the input color is multiplied with the value in matrix node [04].

Then all four results will be added and additionally added the offset value of node [05].

The green value of the input color is multiplied with the value in matrix node [02].

The blue value of the input color is multiplied with the value in matrix node [03].

The alpha value of the input color is multiplied with the value in matrix node [04].

Then all four results will be added and additionally added the offset value of node [05].

Same for the green channel with the nodes [06], [07], [08], [09] and [10].

Same for the blue channel with the nodes [11], [12], [13], [14] and [15].

Same for the alpha channel with the nodes [16], [17], [18], [19] and [20].

The values for the columns 1-4 must be from -1.0 to 2.0 (1.0 doesn't change anything).

The value for the column 5 must be from -255.0 to 255.0 (0.0 doesn't change anything).

The definition of a color matrix which does nothing:

B4X:

```
[1.0][0.0][0.0][0.0][0.0]
[0.0][1.0][0.0][0.0][0.0]
[0.0][0.0][1.0][0.0][0.0]
[0.0][0.0][0.0][1.0][0.0]
```

A color matrix which swaps the red and blue channel:

B4X:

```
[0.0][0.0][1.0][0.0][0.0]
[0.0][1.0][0.0][0.0][0.0]
[1.0][0.0][0.0][0.0][0.0]
[0.0][0.0][0.0][1.0][0.0]
```

A color matrix which inverts the color:

B4X:

```
[-1.0][ 0.0][ 0.0][0.0][255.0]
[ 0.0][-1.0][ 0.0][0.0][255.0]
[ 0.0][ 0.0][-1.0][0.0][255.0]
[ 0.0][ 0.0][ 0.0][1.0][ 0.0]
```

A color with red:224 green:96 blue:64 will result in red:31 green:159 blue:191

red channel:

**224 * -1.0 + 96 * 0.0 + 64 * 0.0 + 255.0 = 31**

green channel:

**224 * 0.0 + 96 * -1.0 + 64 * 0.0 + 255.0 = 159**

blue channel:

**224 * 0.0 + 96 * 0.0 + 64 * -1.0 + 255.0 = 191**

__Using the MFColorMatrix class__You must first create a class instance:

B4X:

```
Dim cm As MFColorMatrix
cm.Initialize
```

Then assign one or more matrices to it:

**Append(id As Int)**adds a predefined matrix

**Append2(id As Int, value As Float)**adds a predefined matrix (for conversions which needs an additional value)

With

**Append3()**and

**Append4()**you can create your own matrix (enter the values as numbered above).

**Append3(matrix() As Float)**(the array must contain exact 25 values)

**Append4(rr As Float, gr As Float, br As Float, ar As Float, roff As Float, rg As Float, gg As Float, bg As Float, ag As Float, goff As Float, rb As Float, gb As Float, bb As Float, ab As Float, boff As Float, ra As Float, ga As Float, ba As Float, aa As Float, aoff As Float)**

Finally assign a bitmat:

**Assign(bmp As Bitmap) As Boolean**changes the original bitmap

**Assign2(bmpIn As Bitmap, bmpOut As Bitmap) As Boolean**reads from bmpIn and write the changes in bmpOut

**Assign3(bmp As Bitmap) As Bitmap**reads from bmp and returns a new bitmap

__List of predefined color matrices (no value needed, used with Append):__

B4X:

```
MATRIX_BLACKANDWHITE
MATRIX_GRAYSCALE
MATRIX_INVERTED
MATRIX_SEPIA
MATRIX_ROSE
MATRIX_COLORSWAP_RGB_BGR
MATRIX_COLORSWAP_RGB_BRG
MATRIX_COLORSWAP_RGB_GBR
MATRIX_COLORSWAP_RGB_GRB
MATRIX_COLORSWAP_RGB_RBG
MATRIX_RED
MATRIX_REDTOGRAY
MATRIX_GREEN
MATRIX_GREENTOGRAY
MATRIX_BLUE
MATRIX_BLUETOGRAY
MATRIX_CYAN
MATRIX_MAGENTA
MATRIX_YELLOW
MATRIX_HUE
MATRIX_SATURATION
MATRIX_BRIGHTNESS
MATRIX_CONTRAST
MATRIX_COLORBOOST
MATRIX_ROTATE_RED
MATRIX_ROTATE_GREEN
MATRIX_ROTATE_BLUE
MATRIX_POLAROID
MATRIX_OLDPHOTO
MATRIX_RED_BLINDNESS
MATRIX_RED_DEBILITY
MATRIX_GREEN_BLINDNESS
MATRIX_GREEN_DEBILITY
MATRIX_BLUE_BLINDNESS
MATRIX_BLUE_DEBILITY
MATRIX_COLOR_BLINDNESS
MATRIX_COLOR_DEBILITY
MATRIX_LUMINANCE2ALPHA
MATRIX_LUMINANCE2ALPHA2
MATRIX_RGB2YUV
MATRIX_YUV2RGB
```

__List of predefined color matrices (value needed, used with Append2):__

B4X:

```
matrix value
MATRIX_HUE 0.0 - 360.0
MATRIX_SATURATION -1.0 - 1.0
MATRIX_BRIGHTNESS -1.0 - 1.0
MATRIX_CONTRAST -1.0 - 1.0
MATRIX_ROTATE_RED 0.0 - 360.0
MATRIX_ROTATE_GREEN 0.0 - 360.0
MATRIX_ROTATE_BLUE 0.0 - 360.0
MATRIX_LUMINANCE2ALPHA -1.0 - 1.0
MATRIX_LUMINANCE2ALPHA2 -1.0 - 1.0
```