Chintan Raghwani
Chintan Raghwani

Reputation: 3370

Merge Two ColorMatrix or Apply two or more ColorMatrix simultaneously on Imageview

I am changing Brightness, Contrast, Saturation and Hue of an ImageView. I have searched lot on it.

I got some code which works with ColorMatrix.

[1.] For Brightness ColorMatrix is some thing like

    float brightness = (-50F * 1.5F);
    ColorMatrix cmB = new ColorMatrix();
    cmB.set(new float[] { 1, 0, 0, 0, brightness,
    0, 1, 0, 0,brightness,
    0, 0, 1, 0, brightness,
    0, 0, 0, 1, 0 });
    myImageView.setColorFilter(new ColorMatrixColorFilter(cmB));

which works Properly.

[2.] For Contrast ColorMatrix is something Different, which also works properly.

BUT, all these ColorMatrix works individually. Means the effect of last applied ColorMatrix is only scene on ImageView, because it does exactly it, which removes effect of earlier applied ColorMatrix and setups last applies ColorMatrix.

Now I want to MERGE or mix-up ColorMatrix of All Simultaneously. Means want to apply ColorMatrix of Contrast on the effect of ColorMatrix of Brightnrs / Saturation / Hue.

Upvotes: 3

Views: 1949

Answers (2)

Sebastian Engel
Sebastian Engel

Reputation: 3695

You can apply multiple matrices like that:

ColorMatrix colorFilterMatrix = new ColorMatrix();
colorFilterMatrix.postConcat(getContrastMatrix(contrast));
colorFilterMatrix.postConcat(getBrightnessMatrix(brightness));

imageView.setColorFilter(new ColorMatrixColorFilter(colorFilterMatrix));

Upvotes: 1

Tim
Tim

Reputation: 35933

If you want to apply two color matrices, just multiply them together.

If you have color matrix A, and color matrix B, then :

C = B * A;
outpixel = C * inpixel 

is equivalent to

outpixel = B * A * inpixel

EDIT

I just noticed that those are 5x4 matrices (originally I thought they were 4x4). Since you can't multiply them directly, I think would be appropriate to add a 5th identity row (0,0,0,0,1) to both matrices before multiplying (to make them both 5x5), and discard the 5th row after multiplying.

Upvotes: 3

Related Questions