package io.gitlab.jfronny.ImgJava.util;

import java.awt.*;

public class MColor {
    public double r;
    public double g;
    public double b;
    public double a;

    public MColor(Color c) {
        this(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
    }

    public MColor(int r, int g, int b, int a) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }

    public MColor() {
        this(0, 0, 0, 0);
    }

    public Color get() {
        return new Color(clamp(r), clamp(g), clamp(b), clamp(a));
    }

    public static int clamp(double d) {
        return (int)Math.round(Math.max(Math.min(d, 255), 0));
    }

    public MColor add(Color c) {
        return this.add(new MColor(c));
    }

    public MColor add(MColor m) {
        r += m.r;
        g += m.g;
        b += m.b;
        a += m.a;
        return this;
    }

    public MColor addM(Color c, double factor) {
        return this.add(new MColor(c).mult(factor));
    }

    public MColor mult(double factor) {
        return tint(factor, factor, factor);
    }

    public MColor div(double factor) {
        r /= factor;
        g /= factor;
        b /= factor;
        a /= factor;
        return this;
    }

    //Alpha of tint is strength: 255 = equal to base, 0 = no effect
    public MColor tint(Color t) {
        double f = t.getAlpha() / 512d;
        this.mult(1 - f);
        r += t.getRed() * f;
        g += t.getGreen() * f;
        b += t.getBlue() * f;
        return this;
    }
    
    public MColor tint(double fR, double fG, double fB) {
        r *= fR;
        g *= fG;
        b *= fB;
        return this;
    }
    
    public MColor invert() {
        r = 255 - r;
        g = 255 - g;
        b = 255 - b;
        return this;
    }
}
