Merge Two ColorMatrix or Apply two or more ColorMa

2019-03-31 23:00发布

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.

2条回答
Bombasti
2楼-- · 2019-03-31 23:46

You can apply multiple matrices like that:

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

imageView.setColorFilter(new ColorMatrixColorFilter(colorFilterMatrix));
查看更多
倾城 Initia
3楼-- · 2019-03-31 23:47

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.

查看更多
登录 后发表回答