MrZaiko/Polysmee

View on GitHub
app/src/main/java/io/github/polysmee/photo/editing/Filters.java

Summary

Maintainability
A
0 mins
Test Coverage
A
90%
package io.github.polysmee.photo.editing;

import android.graphics.ColorMatrix;

/**
 * Class used to implement the color matrices to be used for applying filters on images
 */
public final class Filters {
    public static ColorMatrix binaryFilter() {
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);

        float m = 255f;
        float t = -255 * 128f;
        ColorMatrix threshold = new ColorMatrix(new float[]{
                m, 0, 0, 1, t,
                0, m, 0, 1, t,
                0, 0, m, 1, t,
                0, 0, 0, 1, 0
        });

        // Convert to grayscale, then scale and clamp
        colorMatrix.postConcat(threshold);

        return colorMatrix;
    }

    public static ColorMatrix invertFilter() {
        return new ColorMatrix(new float[]{
                -1, 0, 0, 0, 255,
                0, -1, 0, 0, 255,
                0, 0, -1, 0, 255,
                0, 0, 0, 1, 0
        });
    }

    public static ColorMatrix sepiaFilter() {
        return new ColorMatrix(new float[]{
                0.393f, 0.769f, 0.189f, 0, 0,
                0.349f, 0.686f, 0.168f, 0, 0,
                0.272f, 0.534f, 0.131f, 0, 0,
                0, 0, 0, 1, 0,
        });
    }
}