(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "three"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("three"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.three);
    global.Lut = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.Lut = _exports.ColorMapKeywords = void 0;

  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

  var Lut = /*#__PURE__*/function () {
    function Lut(colormap) {
      var count = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 32;

      _classCallCheck(this, Lut);

      this.lut = [];
      this.map = [];
      this.n = 0;
      this.minV = 0;
      this.maxV = 1;
      this.setColorMap(colormap, count);
    }

    _createClass(Lut, [{
      key: "set",
      value: function set(value) {
        if (value.isLut === true) {
          this.copy(value);
        }

        return this;
      }
    }, {
      key: "setMin",
      value: function setMin(min) {
        this.minV = min;
        return this;
      }
    }, {
      key: "setMax",
      value: function setMax(max) {
        this.maxV = max;
        return this;
      }
    }, {
      key: "setColorMap",
      value: function setColorMap(colormap) {
        var count = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 32;
        this.map = ColorMapKeywords[colormap] || ColorMapKeywords.rainbow;
        this.n = count;
        var step = 1.0 / this.n;
        this.lut.length = 0;

        for (var i = 0; i <= 1; i += step) {
          for (var j = 0; j < this.map.length - 1; j++) {
            if (i >= this.map[j][0] && i < this.map[j + 1][0]) {
              var min = this.map[j][0];
              var max = this.map[j + 1][0];
              var minColor = new _three.Color(this.map[j][1]);
              var maxColor = new _three.Color(this.map[j + 1][1]);
              var color = minColor.lerp(maxColor, (i - min) / (max - min));
              this.lut.push(color);
            }
          }
        }

        return this;
      }
    }, {
      key: "copy",
      value: function copy(lut) {
        this.lut = lut.lut;
        this.map = lut.map;
        this.n = lut.n;
        this.minV = lut.minV;
        this.maxV = lut.maxV;
        return this;
      }
    }, {
      key: "getColor",
      value: function getColor(alpha) {
        if (alpha <= this.minV) {
          alpha = this.minV;
        } else if (alpha >= this.maxV) {
          alpha = this.maxV;
        }

        alpha = (alpha - this.minV) / (this.maxV - this.minV);
        var colorPosition = Math.round(alpha * this.n);
        colorPosition == this.n ? colorPosition -= 1 : colorPosition;
        return this.lut[colorPosition];
      }
    }, {
      key: "addColorMap",
      value: function addColorMap(name, arrayOfColors) {
        ColorMapKeywords[name] = arrayOfColors;
        return this;
      }
    }, {
      key: "createCanvas",
      value: function createCanvas() {
        var canvas = document.createElement('canvas');
        canvas.width = 1;
        canvas.height = this.n;
        this.updateCanvas(canvas);
        return canvas;
      }
    }, {
      key: "updateCanvas",
      value: function updateCanvas(canvas) {
        var ctx = canvas.getContext('2d', {
          alpha: false
        });
        var imageData = ctx.getImageData(0, 0, 1, this.n);
        var data = imageData.data;
        var k = 0;
        var step = 1.0 / this.n;

        for (var i = 1; i >= 0; i -= step) {
          for (var j = this.map.length - 1; j >= 0; j--) {
            if (i < this.map[j][0] && i >= this.map[j - 1][0]) {
              var min = this.map[j - 1][0];
              var max = this.map[j][0];
              var minColor = new _three.Color(this.map[j - 1][1]);
              var maxColor = new _three.Color(this.map[j][1]);
              var color = minColor.lerp(maxColor, (i - min) / (max - min));
              data[k * 4] = Math.round(color.r * 255);
              data[k * 4 + 1] = Math.round(color.g * 255);
              data[k * 4 + 2] = Math.round(color.b * 255);
              data[k * 4 + 3] = 255;
              k += 1;
            }
          }
        }

        ctx.putImageData(imageData, 0, 0);
        return canvas;
      }
    }]);

    return Lut;
  }();

  _exports.Lut = Lut;
  Lut.prototype.isLut = true;
  var ColorMapKeywords = {
    'rainbow': [[0.0, 0x0000FF], [0.2, 0x00FFFF], [0.5, 0x00FF00], [0.8, 0xFFFF00], [1.0, 0xFF0000]],
    'cooltowarm': [[0.0, 0x3C4EC2], [0.2, 0x9BBCFF], [0.5, 0xDCDCDC], [0.8, 0xF6A385], [1.0, 0xB40426]],
    'blackbody': [[0.0, 0x000000], [0.2, 0x780000], [0.5, 0xE63200], [0.8, 0xFFFF00], [1.0, 0xFFFFFF]],
    'grayscale': [[0.0, 0x000000], [0.2, 0x404040], [0.5, 0x7F7F80], [0.8, 0xBFBFBF], [1.0, 0xFFFFFF]]
  };
  _exports.ColorMapKeywords = ColorMapKeywords;
});