"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRoundNumber = exports.getGradientPercentColor = exports.getColorAlpha = exports.generateColor = exports.genAlphaColor = void 0;
var _vcColorPicker = require("../vc-color-picker");
var _color = require("./color");
const generateColor = color => {
  if (color instanceof _color.AggregationColor) {
    return color;
  }
  return new _color.AggregationColor(color);
};
exports.generateColor = generateColor;
const getRoundNumber = value => Math.round(Number(value || 0));
exports.getRoundNumber = getRoundNumber;
const getColorAlpha = color => getRoundNumber(color.toHsb().a * 100);
exports.getColorAlpha = getColorAlpha;
const genAlphaColor = (color, alpha) => {
  const rgba = color.toRgb();
  if (!rgba.r && !rgba.g && !rgba.b) {
    const hsba = color.toHsb();
    hsba.a = alpha || 1;
    return generateColor(hsba);
  }
  rgba.a = alpha || 1;
  return generateColor(rgba);
};
exports.genAlphaColor = genAlphaColor;
const getGradientPercentColor = (colors, percent) => {
  const filledColors = [{
    percent: 0,
    color: colors[0].color
  }, ...colors, {
    percent: 100,
    color: colors[colors.length - 1].color
  }];
  for (let i = 0; i < filledColors.length - 1; i += 1) {
    const startPtg = filledColors[i].percent;
    const endPtg = filledColors[i + 1].percent;
    const startColor = filledColors[i].color;
    const endColor = filledColors[i + 1].color;
    if (startPtg <= percent && percent <= endPtg) {
      const dist = endPtg - startPtg;
      if (dist === 0) {
        return startColor;
      }
      const ratio = (percent - startPtg) / dist * 100;
      const startVcColor = new _vcColorPicker.Color(startColor);
      const endVcColor = new _vcColorPicker.Color(endColor);
      return startVcColor.mix(endVcColor, ratio).toRgbString();
    }
  }
  return '';
};
exports.getGradientPercentColor = getGradientPercentColor;