package plugin.bonus;

import plugin.IPlugin;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class ChooseColor implements IPlugin {

    @Override
    public BufferedImage perform(BufferedImage img) {
        BufferedImage res = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
        Color result = chooseColor();
        float r = 1 - (255 / result.getRed());
        float g = 1 - (255 / result.getGreen());
        float b = 1 - (255 / result.getBlue());
        for (int i = 0; i < img.getWidth(); i++)
            for (int j = 0; j < img.getHeight(); j++) {
                Color c = new Color(img.getRGB(i, j));
                int red = (int) (c.getRed() * r);
                int green = (int) (c.getGreen() * g);
                int blue = (int) (c.getBlue() * b);
                c = new Color(red / 3, green / 3, blue /3);
                res.setRGB(i, j, c.getRGB());
            }
        return res;
    }

    @Override
    public String getName() {
        return "Only One Color";
    }

    private Color chooseColor() {
        return JColorChooser.showDialog(null, "Choose a color", Color.blue);
    }

}