import warning from 'warning';
import { clamp, roundToPrecision } from "../ColorHelpers";
export var ColorObject = /*#__PURE__*/function () {
  function ColorObject(rgb, alpha, type) {
    this.rgb = rgb;
    this.alpha = alpha;
    this.type = type;
  }
  var _proto = ColorObject.prototype;
  _proto.luma = function luma() {
    var r = this.rgb[0] / 255;
    var g = this.rgb[1] / 255;
    var b = this.rgb[2] / 255;
    r = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
    g = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
    b = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
    return 0.2126 * r + 0.7152 * g + 0.0722 * b;
  };
  _proto.toHEXString = function toHEXString() {
    warning(this.alpha === 1, "There is an alpha channel=" + this.alpha + ", converting to HEX will discard opacity");
    return '#' + this.rgb.map(function (c) {
      var clamped = clamp(Math.round(c), 255);
      return (clamped < 16 ? '0' : '') + clamped.toString(16);
    }).join('');
  };
  _proto.toRGB = function toRGB() {
    return {
      r: this.rgb[0],
      g: this.rgb[1],
      b: this.rgb[2],
      a: this.alpha
    };
  };
  _proto.toRGBString = function toRGBString() {
    return this.alpha < 1 ? "rgba(" + this.rgb[0] + ", " + this.rgb[1] + ", " + this.rgb[2] + ", " + this.alpha + ")" : "rgb(" + this.rgb[0] + ", " + this.rgb[1] + ", " + this.rgb[2] + ")";
  };
  _proto.toHSL = function toHSL() {
    var r = this.rgb[0] / 255;
    var g = this.rgb[1] / 255;
    var b = this.rgb[2] / 255;
    var max = Math.max(r, g, b);
    var min = Math.min(r, g, b);
    var hDeg = 0;
    var s = 0;
    var l = (max + min) / 2;
    if (max !== min) {
      var d = max - min;
      s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
      switch (max) {
        case r:
          hDeg = (g - b) / d + (g < b ? 6 : 0);
          break;
        case g:
          hDeg = (b - r) / d + 2;
          break;
        case b:
          hDeg = (r - g) / d + 4;
          break;
      }
      hDeg = Math.round(hDeg * 60);
    }
    return {
      h: hDeg,
      s: roundToPrecision(s, 2),
      l: roundToPrecision(l, 2),
      a: this.alpha
    };
  };
  _proto.toHSLString = function toHSLString() {
    var hsl = this.toHSL();
    return hsl.a < 1 ? "hsla(" + hsl.h + ", " + hsl.s * 100 + "%, " + hsl.l * 100 + "%, " + hsl.a + ")" : "hsl(" + hsl.h + ", " + hsl.s * 100 + "%, " + hsl.l * 100 + "%)";
  };
  _proto.toColorString = function toColorString(type) {
    if (type === void 0) {
      type = this.type;
    }
    switch (type) {
      case 'rgb':
      case 'rgba':
        {
          return this.toRGBString();
        }
      case 'hsl':
      case 'hsla':
        {
          return this.toHSLString();
        }
      case 'transparent':
        {
          return "transparent";
        }
      default:
        return this.toHEXString();
    }
  };
  _proto.clone = function clone() {
    var rgb = this.rgb,
      alpha = this.alpha,
      type = this.type;
    return new ColorObject(rgb, alpha, type);
  };
  return ColorObject;
}();