import { Common, VMath } from "v-eris";
//
export var InlineRGBA2CoordHSVA2 = function (rgba) {
    var r = rgba.r, g = rgba.g, b = rgba.b, a = rgba.a;
    var max = (r > g && r > b) ? r : (g > b) ? g : b;
    var min = (r < g && r < b) ? r : (g < b) ? g : b;
    if (max < 0.0001)
        return { h: 0.0, s: 0.0, v: 0.0, a: a };
    var h, s, v;
    h = s = (max + min) / 2.0;
    v = max;
    if (max == min) {
        h = s = 0.0;
    }
    else {
        var d = max - min;
        s = d / max;
        if (d < 0.0001)
            return { h: h, s: s, v: v, a: a };
        if (r > g && r > b)
            h = (g - b) / d + (g < b ? 6.0 : 0.0);
        else if (g > b)
            h = (b - r) / d + 2.0;
        else
            h = (r - g) / d + 4.0;
        h /= 6.0;
        if (h < 0.0)
            h += 1.0;
    }
    ;
    return { h: h, s: s, v: v, a: a };
};
//^
export var InlineRGBA2CoordHSVA = function (rgba) {
    var r = rgba.r, g = rgba.g, b = rgba.b, a = rgba.a;
    var h, s, v;
    var min = (r < g && r < b) ? r : (g < b) ? g : b;
    var max = (r > g && r > b) ? r : (g > b) ? g : b;
    v = max;
    var delta = max - min;
    if (max != 0)
        s = delta / max;
    else {
        s = 0;
        h = -1;
        return { h: h, s: s, v: v, a: a };
    }
    ;
    if (r == max)
        h = (g - b) / delta;
    else if (g == max) {
        h = 2.0 + (b - r) / delta;
    }
    else {
        h = 4.0 + (r - g) / delta;
    }
    h *= 60.0;
    if (h < 0)
        h += 360;
    return { h: h, s: s, v: v, a: a };
};
export var RGBA2CoordHSVA = function (rgba) {
    return InlineRGBA2CoordHSVA2({ r: rgba.r / 255.0, g: rgba.g / 255.0, b: rgba.b / 255.0, a: rgba.a });
};
//
export var InlineCoordHSVA2RGBA2 = function (hsva) {
    if (hsva.s < 0.0)
        return { r: hsva.v, g: hsva.v, b: hsva.v, a: hsva.a };
    var h = VMath.clamp(hsva.h, 0.000, 1.0);
    var z = VMath.clamp(hsva.v, 0.000, 1.0);
    h *= 6.0;
    var index = Common.uint(h);
    var f = h - index;
    var p = z * (1.0 - hsva.s);
    var q = z * (1.0 - (hsva.s * f));
    var t = z * (1.0 - (hsva.s * (1.0 - f)));
    if (index == 0) {
        return { r: z, g: t, b: p, a: hsva.a };
    }
    else if (index == 1) {
        return { r: q, g: z, b: p, a: hsva.a };
    }
    else if (index == 2) {
        return { r: p, g: z, b: t, a: hsva.a };
    }
    else if (index == 3) {
        return { r: p, g: q, b: z, a: hsva.a };
    }
    else if (index == 4) {
        return { r: t, g: p, b: z, a: hsva.a };
    }
    else if (index == 5) {
        return { r: z, g: p, b: q, a: hsva.a };
    }
    ;
    return { r: z, g: z, b: z, a: hsva.a };
};
//^
export var InlineCoordHSVA2RGBA = function (hsva) {
    var h = hsva.h;
    var v = hsva.v;
    var s = hsva.s;
    var a = hsva.a;
    if (s == 0) {
        return { r: v, g: v, b: v, a: a };
    }
    ;
    h /= 60.0;
    var index = Math.floor(h);
    var f = h - index;
    var p = v * (1.0 - s);
    var q = v * (1.0 - (s * f));
    var t = v * (1.0 - (s * (1.0 - f)));
    if (index == 0) {
        return { r: v, g: t, b: p, a: hsva.a };
    }
    else if (index == 1) {
        return { r: q, g: v, b: p, a: hsva.a };
    }
    else if (index == 2) {
        return { r: p, g: v, b: t, a: hsva.a };
    }
    else if (index == 3) {
        return { r: p, g: q, b: v, a: hsva.a };
    }
    else if (index == 4) {
        return { r: t, g: p, b: v, a: hsva.a };
    }
    else if (index == 5) {
        return { r: v, g: p, b: q, a: hsva.a };
    }
    ;
    return { r: v, g: p, b: q, a: hsva.a };
};
export var CoordHSVA2RGBA = function (hsva) {
    var result = InlineCoordHSVA2RGBA2(hsva);
    result.r *= 255.0;
    result.g *= 255.0;
    result.b *= 255.0;
    return { r: Math.round(result.r), g: Math.round(result.g), b: Math.round(result.b), a: result.a };
};
export var HexToInt = function (value) {
    var code = value.charCodeAt(0);
    if (code > 96 && code < 103) {
        return code - 87;
    }
    else if (code > 64 && code < 71) {
        return code - 55;
    }
    else if (code > 47 && code < 58) {
        return code - 48;
    }
    ;
    return 0;
};
export var ParseColor = function (value) {
    var type = Common.type(value);
    if (type == "array") {
        return { r: Common.uint(value[0]), g: Common.uint(value[1]), b: Common.uint(value[2]), a: Common.float(value[3]) };
    }
    else if (type == "object" && value.x !== undefined) {
        return { r: Common.uint(value.x), g: Common.uint(value.y), b: Common.uint(value.z), a: Common.float(value.w) };
    }
    else if (type == "object" && value.r !== undefined) {
        return { r: Common.uint(value.r), g: Common.uint(value.g), b: Common.uint(value.b), a: Common.float(value.a) };
    }
    ;
    var v = Common.string(value).trim();
    var result = { r: 0, g: 0, b: 0, a: 0.0 };
    if (v.length < 4)
        return result;
    if (v[0] == "#") {
        if (v.length >= 9) {
            result = {
                r: HexToInt(v[1]) * 16 + HexToInt(v[2]),
                g: HexToInt(v[3]) * 16 + HexToInt(v[4]),
                b: HexToInt(v[5]) * 16 + HexToInt(v[6]),
                a: (HexToInt(v[7]) * 16 + HexToInt(v[8])) / 255.0
            };
        }
        else if (v.length >= 7) {
            result = {
                r: HexToInt(v[1]) * 16 + HexToInt(v[2]),
                g: HexToInt(v[3]) * 16 + HexToInt(v[4]),
                b: HexToInt(v[5]) * 16 + HexToInt(v[6]),
                a: 1.0
            };
        }
        else if (v.length >= 4) {
            result = {
                r: HexToInt(v[1]) * 16 + HexToInt(v[1]),
                g: HexToInt(v[2]) * 16 + HexToInt(v[2]),
                b: HexToInt(v[3]) * 16 + HexToInt(v[3]),
                a: 1.0
            };
        }
        ;
    }
    else if (v[0] == "r" && v[1] == "g" && v[2] == "b") {
        var shift = 3;
        if (v[3] == "a")
            shift = 4;
        v = v.slice(shift).trim();
        var s = v.replace(/\s|\)|\(/g, "").split(/,/g);
        result.r = Common.uint(s[0]);
        result.g = Common.uint(s[1]);
        result.b = Common.uint(s[2]);
        if (shift > 3)
            result.a = Common.float(s[3]);
        else
            result.a = 1.0;
    }
    ;
    return result;
};
//# sourceMappingURL=colors.js.map