export function colorScheme()

in src/infertypes.ts [209:238]


export function colorScheme(pd: ParamDef, value: any, alpha: number, defaultColorMap: string): string {
    if (pd.type == ParamType.CATEGORICAL) {
        compute_val2color(pd);
        var c = pd.__val2color[value];
        if (c === undefined) {
            return `rgb(100,100,100,${alpha})`;
        }
        console.assert((c.startsWith('rgb(') || c.startsWith('hsl(')), c);
        return c.slice(0, 3) + 'a' + c.slice(3, c.length - 1) + ',' + alpha + ')';
    }
    else {
        if (value === undefined || value === null || is_special_numeric(value)) {
            return `rgb(100,100,100,${alpha})`;
        }
        if (!pd.__colorscale || pd.__colorscale.__type !== pd.type) {
            pd.__colorscale = create_d3_scale_without_outliers(pd);
            pd.__colorscale.range([0, 1]);
            pd.__colorscale.__type = pd.type;
        }
        const colr = Math.max(0, Math.min(1, pd.__colorscale(value)));
        const interpColFn = getColorMap(pd, defaultColorMap);
        try {
            const code = interpColFn(colr);
            const rgb = color(code).rgb().object();
            return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`;
        } catch (err) {
            throw new Error(`Error below happened while computing color using color map "${pd.colormap}" for column ${pd.name}: is the colormap valid? (${err.toString()})`);
        }
    }
}