"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagickColor = void 0;
const image_magick_1 = require("./image-magick");
const quantum_1 = require("./quantum");
const string_1 = require("./internal/native/string");
class MagickColor {
    constructor(colorOrRed, g, b, aOrK, a) {
        this._r = 0;
        this._g = 0;
        this._b = 0;
        this._a = 0;
        this._k = 0;
        this._isCmyk = false;
        if (colorOrRed === undefined)
            return;
        if (typeof colorOrRed === 'string') {
            let instance = 0;
            try {
                instance = image_magick_1.ImageMagick._api._MagickColor_Create();
                string_1._withString(colorOrRed, colorPtr => {
                    if (image_magick_1.ImageMagick._api._MagickColor_Initialize(instance, colorPtr) === 0)
                        throw new Error('invalid color specified');
                    this.initialize(instance);
                });
            }
            finally {
                image_magick_1.ImageMagick._api._free(instance);
            }
        }
        else {
            this._r = colorOrRed;
            this._g = g !== null && g !== void 0 ? g : 0;
            this._b = b !== null && b !== void 0 ? b : 0;
            if (a === undefined) {
                this._a = aOrK !== null && aOrK !== void 0 ? aOrK : quantum_1.Quantum.max;
            }
            else {
                this._k = aOrK !== null && aOrK !== void 0 ? aOrK : 0;
                this._a = a;
                this._isCmyk = true;
            }
        }
    }
    static _create(colorPtr) {
        const color = new MagickColor();
        color.initialize(colorPtr);
        return color;
    }
    get r() { return this._r; }
    set r(value) { this._r = value; }
    get g() { return this._g; }
    set g(value) { this._g = value; }
    get b() { return this._b; }
    set b(value) { this._b = value; }
    get a() { return this._a; }
    set a(value) { this._a = value; }
    get k() { return this._k; }
    set k(value) { this._k = value; }
    get isCmyk() { return this._isCmyk; }
    toShortString() {
        if (this._a !== quantum_1.Quantum.max)
            return this.toString();
        if (this.isCmyk)
            return `cmyka(${this._r},${this._g},${this._b},${this._k})`;
        return `#${this.toHex(this._r)}${this.toHex(this._g)}${this.toHex(this._b)}`;
    }
    toString() {
        if (this.isCmyk)
            return `cmyka(${this._r},${this._g},${this._b},${this._k},${(this._a / quantum_1.Quantum.max).toFixed(4)})`;
        return `#${this.toHex(this._r)}${this.toHex(this._g)}${this.toHex(this._b)}${this.toHex(this._a)}`;
    }
    _use(func) {
        let instance = 0;
        try {
            instance = image_magick_1.ImageMagick._api._MagickColor_Create();
            image_magick_1.ImageMagick._api._MagickColor_Red_Set(instance, this._r);
            image_magick_1.ImageMagick._api._MagickColor_Green_Set(instance, this._g);
            image_magick_1.ImageMagick._api._MagickColor_Blue_Set(instance, this._b);
            image_magick_1.ImageMagick._api._MagickColor_Alpha_Set(instance, this._a);
            image_magick_1.ImageMagick._api._MagickColor_IsCMYK_Set(instance, this._isCmyk ? 1 : 0);
            func(instance);
        }
        finally {
            image_magick_1.ImageMagick._api._free(instance);
        }
    }
    initialize(instance) {
        this._r = image_magick_1.ImageMagick._api._MagickColor_Red_Get(instance);
        this._g = image_magick_1.ImageMagick._api._MagickColor_Green_Get(instance);
        this._b = image_magick_1.ImageMagick._api._MagickColor_Blue_Get(instance);
        this._a = image_magick_1.ImageMagick._api._MagickColor_Alpha_Get(instance);
        this._isCmyk = image_magick_1.ImageMagick._api._MagickColor_IsCMYK_Get(instance) === 1;
    }
    toHex(value) {
        return value.toString(16).padStart(2, '0');
    }
}
exports.MagickColor = MagickColor;
