package com.nokia.common.excel.entity;

import java.awt.Color;

import lombok.Data;

/**
 * 三色渐变色
 */
@Data
public class ThreeColorGradient {

    // 三种颜色 默认三色 FF63BE7B--FFFFEB84--FFF8696B 红/黄/绿
    private Color startColor = new Color(99, 190, 123, 255);
    private Color middleColor = new Color(255, 235, 132, 255);
    private Color endColor = new Color(248, 105, 107, 255);

    // 开始数、中位数、截止数
    private double startValue;
    private double middleValue;
    private double endValue;

    /**
     * 根据输入值返回一个颜色
     * 
     * @param d
     * @return
     */
    public Color getColor(double d) {
        if ((d >= startValue && d < middleValue) || (d <= startValue && d > middleValue)) {
            // 值在中位数和satartValue之间，计算起始颜色和中间颜色之间的渐进色
            double t = middleValue == startValue ? 0 : (d - startValue) / (middleValue - startValue);
            double red = (1 - t) * startColor.getRed() + t * middleColor.getRed();
            double green = (1 - t) * startColor.getGreen() + t * middleColor.getGreen();
            double blue = (1 - t) * startColor.getBlue() + t * middleColor.getBlue();
            double opacity = (1 - t) * startColor.getAlpha() + t * middleColor.getAlpha();
            return new Color((int) red, (int) green, (int) blue, (int) opacity);
        } else {
            // 值在中位数和endValue之间，计算中间亚瑟和结束颜色之间的渐进色
            double t = middleValue == endValue ? 0 : (d - endValue) / (middleValue - endValue);
            double red = (1 - t) * endColor.getRed() + t * middleColor.getRed();
            double green = (1 - t) * endColor.getGreen() + t * middleColor.getGreen();
            double blue = (1 - t) * endColor.getBlue() + t * middleColor.getBlue();
            double opacity = (1 - t) * endColor.getAlpha() + t * middleColor.getAlpha();
            return new Color((int) red, (int) green, (int) blue, (int) opacity);
        }
    }
}
