const padCol = (str) => (str.length < 2 ? `0${str}` : str.slice(0, 2));
export const hexToRgb = (color) => {
    return [
        parseInt(color.slice(1, 3), 16),
        parseInt(color.slice(3, 5), 16),
        parseInt(color.slice(5, 7), 16),
    ];
};
export const applyAlpha = (color, alpha = 0.5) => {
    if (alpha > 1 || alpha < 0) {
        throw new Error("incorrect alpha");
    }
    const [r, g, b] = hexToRgb(color);
    const [r2, g2, b2] = [
        Math.floor(255 - alpha * (255 - r)),
        Math.floor(255 - alpha * (255 - g)),
        Math.floor(255 - alpha * (255 - b)),
    ];
    return `#${padCol(r2.toString(16))}${padCol(g2.toString(16))}${padCol(b2.toString(16))}`;
};
export const appendAlpha = (color, alpha = 0.5) => {
    if (alpha > 1 || alpha < 0) {
        throw new Error("incorrect alpha");
    }
    const [r, g, b] = hexToRgb(color);
    return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};
export const colorFromGradient = (value, gradient) => {
    const points = gradient.map(hexToRgb);
    const stopLength = 1 / (points.length - 1);
    const valueRatio = value / stopLength;
    const stopIndex = Math.floor(valueRatio);
    if (stopIndex === points.length - 1) {
        return gradient[gradient.length - 1];
    }
    const stopFraction = valueRatio % 1;
    const interpolated = interpolateRgb(points[stopIndex], points[stopIndex + 1], stopFraction);
    return `#${Math.floor(interpolated[0]).toString(16)}${Math.floor(interpolated[1]).toString(16)}${Math.floor(interpolated[2]).toString(16)}`;
};
export const interpolateRgb = (p1, p2, normalValue) => {
    return [
        p1[0] + (p2[0] - p1[0]) * normalValue,
        p1[1] + (p2[1] - p1[1]) * normalValue,
        p1[2] + (p2[2] - p1[2]) * normalValue,
    ];
};
export const composeStyle = (k, v) => (v ? `${k}: ${v};` : "");
export const trimBigNumber = (num) => {
    return num.toFixed(9).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, "$1");
};
export const shortEthAddress = (address) => {
    return `${address.slice(0, 4)}...${address.slice(-4)}`;
};
export const shortAccName = (name) => {
    if (name.length > 6) {
        return `${name.slice(0, 6)}...`;
    }
    return name;
};
export const delay = async (ms) => {
    await new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, ms);
    });
};
export const getSubPageName = () => {
    if (!globalThis.location)
        return "";
    return globalThis.location.pathname.split("/")[2];
};
//# sourceMappingURL=util.js.map