"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Color = void 0;
var Color = /** @class */ (function () {
    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; }
        this._r = 0.0;
        this._g = 0.0;
        this._b = 0.0;
        this._a = 1.0;
        this._r = r;
        this._g = g;
        this._b = b;
        this._a = a;
    }
    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)
            }
        },
        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);
        }
        if (typeof value === 'string') {
            if (value.indexOf("#") === 0)
                this.setHexCssString(value);
            else
                console.error("未匹配的颜色数据");
        }
        return this;
    };
    Color.prototype.setHex = function (hex) {
        hex = Math.floor(hex);
        this._r = (hex >> 16 & 255) / 255;
        this._g = (hex >> 8 & 255) / 255;
        this._b = (hex & 255) / 255;
        return this;
    };
    Color.prototype.setHexCssString = function (style) {
        var m = /^\#([A-Fa-f\d]+)$/.exec(style);
        // hex color
        var hex = m[1];
        var size = hex.length;
        if (size === 3) {
            // #ff0
            this.r = parseInt(hex.charAt(0) + hex.charAt(0), 16) / 255;
            this.g = parseInt(hex.charAt(1) + hex.charAt(1), 16) / 255;
            this.b = parseInt(hex.charAt(2) + hex.charAt(2), 16) / 255;
            return this;
        }
        else if (size === 6) {
            // #ff0000
            this.r = parseInt(hex.charAt(0) + hex.charAt(1), 16) / 255;
            this.g = parseInt(hex.charAt(2) + hex.charAt(3), 16) / 255;
            this.b = parseInt(hex.charAt(4) + hex.charAt(5), 16) / 255;
            return this;
        }
        else if (size === 4) {
            // #ff0000
            this.r = parseInt(hex.charAt(0) + hex.charAt(0), 16) / 255;
            this.g = parseInt(hex.charAt(1) + hex.charAt(1), 16) / 255;
            this.b = parseInt(hex.charAt(2) + hex.charAt(2), 16) / 255;
            this.a = parseInt(hex.charAt(3) + hex.charAt(3), 16) / 255;
            return this;
        }
        else if (size === 8) {
            // #ff0000
            this.r = parseInt(hex.charAt(0) + hex.charAt(1), 16) / 255;
            this.g = parseInt(hex.charAt(2) + hex.charAt(3), 16) / 255;
            this.b = parseInt(hex.charAt(4) + hex.charAt(5), 16) / 255;
            this.a = parseInt(hex.charAt(6) + hex.charAt(7), 16) / 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;
        this._a = color.a;
        return this;
    };
    Color.prototype.equals = function (c) {
        return (c.r === this.r) && (c.g === this.g) && (c.b === this.b) && (c.a === this.a);
    };
    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.byteToFloat = function (val) {
        return val / 255.0;
    };
    ;
    Color.prototype.floatToByte = function (val) {
        return val === 1.0 ? 255.0 : (val * 256.0) | 0;
    };
    ;
    Color.prototype.getHexCssString = function () {
        var r = this.floatToByte(this._r).toString(16);
        if (r.length < 2) {
            r = "0" + r;
        }
        var g = this.floatToByte(this._g).toString(16);
        if (g.length < 2) {
            g = "0" + g;
        }
        var b = this.floatToByte(this._b).toString(16);
        if (b.length < 2) {
            b = "0" + b;
        }
        if (this._a < 1) {
            var hexAlpha = this.floatToByte(this._a).toString(16);
            if (hexAlpha.length < 2) {
                hexAlpha = "0" + hexAlpha;
            }
            return "#" + r + g + b + hexAlpha;
        }
        return "#" + r + g + b;
    };
    Color.prototype.toJSON = function () {
        return this.getHexCssString();
    };
    return Color;
}());
exports.Color = Color;
