/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.lobby.particle;

import java.awt.*;
import java.util.Arrays;
import java.util.List;

public class Gradient {

    private List<Color> colors;

    public Gradient(Color... colors) {
        this.colors = Arrays.asList(colors);
    }

    public Color getColor(double value, double maxValue) {
        if (colors.isEmpty()) {
            return Color.BLACK;
        }
        if (colors.size() == 1) {
            return colors.get(0);
        }

        value /= maxValue / (double) (colors.size() - 1);

        return lerpColor(
                colors.get((int) value),
                colors.get((int) value + 1),
                value - (int) value,
                1);
    }

    private Color lerpColor(Color start, Color end, double step, double maxStep) {
        double rStep = (end.getRed() - start.getRed()) / maxStep;
        double gStep = (end.getGreen() - start.getGreen()) / maxStep;
        double bStep = (end.getBlue() - start.getBlue()) / maxStep;

        return new Color(start.getRed() + (int) (rStep * step),
                start.getGreen() + (int) (gStep * step),
                start.getBlue() + (int) (bStep * step));
    }
}
