0

I have the following code that reduces an image's contrast and by 50% and turns down the saturation to 0. Works flawlessly. However, I need to overlay a give colour over the resulting image, much like the PorterDuff.Mode.OVERLAY filter.

I've been fiddling with this for hours, didn't get me anywhere close to what I wanted.

        float contrast = -0.5f;
        float scale = contrast + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;
        float[] array = new float[]{
                scale, 0, 0, 0, translate,
                0, scale, 0, 0, translate,
                0, 0, scale, 0, translate,
                0, 0, 0, 1, 0};
        ColorMatrix contrastMatrix = new ColorMatrix(array);
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(0);
        ColorMatrix matrix = new ColorMatrix();
        matrix.setConcat(contrastMatrix, saturationMatrix);
        // This is where I've been trying to overlay the colour by fiddling the colour matrix array.
        // Didn't get the effect I wanted.
        int primaryColor = context.getResources().getColor(R.color.primary500);
        matrix.postConcat(new ColorMatrix(
            new float[]{
                    Color.red(primaryColor)/255f, 0, 0, 0, 0,
                    0, Color.green(primaryColor)/255f, 0, 0, 0,
                    0, 0, Color.blue(primaryColor)/255f, 0, 0,
                    0, 0, 0, 1, 0}));
        imageView.setColorFilter(filter);

The array I've used above give a much dark tone of blue. Can't seem the get the it right for some reason.

Any help would be much appreciated. Many thanks!

fahmy
  • 3,543
  • 31
  • 47
  • 1
    See [this](http://stackoverflow.com/a/9149010/2649012) answer, for a Sepia toning. – Phantômaxx May 14 '15 at 13:12
  • You can change the values for different color toning. – Phantômaxx May 14 '15 at 13:39
  • @DerGolem yes, with that the colour overlaying works, but now can't get the contrast to drop to 50%... any ideas? – fahmy May 14 '15 at 13:57
  • Combine the ColorMatrices, as shown in the example: `matrixA.setConcat(matrixB, matrixA);`. By the way, your code already uses this trick. Just add another matrix and concatenate it to the previously concatenated 2 ones, so getting a concatentation of 3 matrices. – Phantômaxx May 14 '15 at 14:03

0 Answers0