"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Color = exports.LinearToSRGB = exports.SRGBToLinear = exports.hue2rgb = void 0;
var eventhandler_1 = require("src/render/eventhandler");
var THREE = __importStar(require("three"));
function hue2rgb(p, q, t) {
    if (t < 0)
        t += 1;
    if (t > 1)
        t -= 1;
    if (t < 1 / 6)
        return p + (q - p) * 6 * t;
    if (t < 1 / 2)
        return q;
    if (t < 2 / 3)
        return p + (q - p) * 6 * (2 / 3 - t);
    return p;
}
exports.hue2rgb = hue2rgb;
function SRGBToLinear(c) {
    return (c < 0.04045) ? c * 0.0773993808 : Math.pow(c * 0.9478672986 + 0.0521327014, 2.4);
}
exports.SRGBToLinear = SRGBToLinear;
function LinearToSRGB(c) {
    return (c < 0.0031308) ? c * 12.92 : 1.055 * (Math.pow(c, 0.41666)) - 0.055;
}
exports.LinearToSRGB = LinearToSRGB;
var Color = /** @class */ (function (_super) {
    __extends(Color, _super);
    function Color(_r, _g, _b, _a) {
        if (_r === void 0) { _r = 0; }
        if (_g === void 0) { _g = 0; }
        if (_b === void 0) { _b = 0; }
        if (_a === void 0) { _a = 1; }
        var _this = _super.call(this) || this;
        _this._r = _r;
        _this._g = _g;
        _this._b = _b;
        _this._a = _a;
        return _this;
    }
    Object.defineProperty(Color.prototype, "isColor", {
        get: function () {
            return true;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Color.prototype, "r", {
        get: function () {
            return this._r;
        },
        set: function (value) {
            if (this._r !== value) {
                this._r = value;
                this.fire('change', 'r', this._r, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Color.prototype, "g", {
        get: function () {
            return this._g;
        },
        set: function (value) {
            if (this._g !== value) {
                this._g = value;
                this.fire('change', 'g', this._g, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Color.prototype, "b", {
        get: function () {
            return this._g;
        },
        set: function (value) {
            if (this._b !== value) {
                this._b = value;
                this.fire('change', 'b', this._b, value);
            }
            THREE.Color;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Color.prototype, "a", {
        get: function () {
            return this._a;
        },
        set: function (value) {
            if (this._a !== value) {
                this._a = value;
                this.fire('change', 'a', this._a, value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Color.prototype.set = function (value) {
        if (value && value.isColor) {
            this.copy(value);
        }
        else if (typeof value === 'number') {
            this.setHex(value);
        }
        return this;
    };
    Color.prototype.setHex = function (hex) {
        hex = Math.floor(hex);
        if (hex > 0xffffff) {
            this._r = (hex >> 32 & 255) / 255;
            this._g = (hex >> 16 & 255) / 255;
            this._b = (hex >> 8 & 255) / 255;
            this._a = (hex & 255) / 255;
        }
        else {
            this._r = (hex >> 16 & 255) / 255;
            this._g = (hex >> 8 & 255) / 255;
            this._b = (hex & 255) / 255;
        }
        return this;
    };
    Color.prototype.setRGB = function (r, g, b, a) {
        if (a === void 0) { a = 1.0; }
        this._r = r;
        this._g = g;
        this._b = b;
        this._a = a;
        return this;
    };
    Color.prototype.clone = function () {
        return new this.constructor(this.r, this.g, this.b, this.a);
    };
    Color.prototype.copy = function (color) {
        this._r = color.r;
        this._g = color.g;
        this._b = color.b;
        return this;
    };
    Color.prototype.equals = function (c) {
        return (c.r === this.r) && (c.g === this.g) && (c.b === this.b);
    };
    Color.prototype.toArray = function (array, offset) {
        if (array === void 0) { array = []; }
        if (offset === void 0) { offset = 0; }
        array[offset] = this.r;
        array[offset + 1] = this.g;
        array[offset + 2] = this.b;
        array[offset + 3] = this.a;
        return array;
    };
    Color.prototype.getHex = function () {
        return (this.r * 255) << 32 ^ (this.g * 255) << 16 ^ (this.b * 255) << 8 ^ (this.a * 255) << 0;
    };
    Color.prototype.toJSON = function () {
        return this.getHex();
    };
    return Color;
}(eventhandler_1.EventHandler));
exports.Color = Color;
