import { rgbValues } from "@emerson-eps/color-tables/";
import { createDefaultContinuousColorScale } from "@emerson-eps/color-tables/dist/component/Utils/legendCommonFunction";
/**
 * Creates an array of colors as RGB triplets in range [0, 1] using the colormap definition.
 * @param colormapDef Definition of the colormap.
 * @param colormapSize Number of colors in the color map.
 * @param discreteColormapFunction If true, the color map function is targeting indices ranging from 0 to colormapSize.
 * @returns Array of colors.
 */
export function getImageData(colormapDef, colormapSize = 256, discreteColormapFunction = false) {
    if (colormapDef instanceof Uint8Array) {
        // If colormapProps is a Uint8Array, return it directly
        return colormapDef;
    }
    const isFunctionDefined = colormapDef !== undefined;
    const isColorTableDef = "colormapName" in colormapDef &&
        "colorTables" in colormapDef &&
        !!colormapDef.colorTables;
    let colormap = createDefaultContinuousColorScale();
    if (isColorTableDef) {
        if (colormapDef.colormapName) {
            colormap = (value) => rgbValues(value, colormapDef.colormapName, colormapDef.colorTables);
        }
    }
    else if (isFunctionDefined) {
        colormap =
            typeof colormapDef === "function"
                ? colormapDef
                : (() => colormapDef);
    }
    const data = new Uint8Array(colormapSize * 3);
    const scaling = discreteColormapFunction
        ? 1
        : 1 / Math.max(colormapSize - 1, 1);
    for (let i = 0; i < colormapSize; i++) {
        const color = colormap ? colormap(scaling * i) : [1, 0, 0];
        if (color) {
            data[3 * i + 0] = color[0];
            data[3 * i + 1] = color[1];
            data[3 * i + 2] = color[2];
        }
    }
    return data;
}
const DEFAULT_TEXTURE_PARAMETERS = {
    addressModeU: "clamp-to-edge",
    addressModeV: "clamp-to-edge",
    minFilter: "linear",
    magFilter: "linear",
};
const DISCRETE_TEXTURE_PARAMETERS = {
    minFilter: "nearest",
    magFilter: "nearest",
    addressModeU: "clamp-to-edge",
    addressModeV: "clamp-to-edge",
};
export function createColormapTexture(colormap, context, colormapHints) {
    const textureProps = {
        sampler: DEFAULT_TEXTURE_PARAMETERS,
        width: 256,
        height: 1,
        format: "rgb8unorm-webgl",
    };
    if (colormapHints.discreteData) {
        if (colormapHints.colormapSize === 0) {
            const colormapTexture = context.device.createTexture(Object.assign(Object.assign({}, textureProps), { sampler: DISCRETE_TEXTURE_PARAMETERS, width: colormapHints.colormapSize, data: new Uint8Array([0, 0, 0, 0, 0, 0]) }));
            return colormapTexture;
        }
        const colormapData = colormap instanceof Uint8Array
            ? colormap
            : getImageData(colormap, colormapHints.colormapSize, colormapHints.discreteData);
        const colormapTexture = context.device.createTexture(Object.assign(Object.assign({}, textureProps), { sampler: DISCRETE_TEXTURE_PARAMETERS, width: colormapHints.colormapSize, data: colormapData }));
        return colormapTexture;
    }
    const data = getImageData(colormap);
    const colormapTexture = context.device.createTexture(Object.assign(Object.assign({}, textureProps), { data: data }));
    return colormapTexture;
}
//# sourceMappingURL=colormapTools.js.map