package filter.basic;

import java.awt.Color;
import java.awt.image.BufferedImage;
import filter.Filter;

/**
 * Created by Alexandre on 24/04/2016.
 */
public class Invert implements Filter
{
    @Override
    public BufferedImage perform(BufferedImage img)
    {
        BufferedImage res = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
        int w = img.getWidth();
        int h = img.getHeight();
        for (int i = 0; i < w; i++)
            for (int j = 0; j < h; j++)
                res.setRGB(i, j, revertColor(img.getRGB(i, j)));
        return res;
    }

    @Override
    public String getName()
    {
        return "Invert";
    }

    private int revertColor(int color)
    {
        Color c = new Color(color);
        return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue()).getRGB();
    }
}
