LYGIA Shader Library

lerp (lygia/color/palette/lerp)

linear interpolation between colors in a palette

Dependencies:

Check it on Github




#ifndef PALETTE_LERP_MIX_FNC
#define PALETTE_LERP_MIX_FNC(A, B, T) mix(A, B, T)
#endif

#ifndef FNC_PALETTE_LERP
#define FNC_PALETTE_LERP

vec3 paletteLerp_get(vec3 a[PALETTE_LERP_SIZE], int index){
    index = int(mod(float(index), float(PALETTE_LERP_SIZE)));

    #if defined(PLATFORM_WEBGL)
    for (int i = 0; i < PALETTE_LERP_SIZE; i++)
        #ifdef PALETTE_LERP_SRGB
        if (i == index) return srgb2rgb(a[i]);
        #else
        if (i == index) return a[i];
        #endif
    #else
    #ifdef PALETTE_LERP_SRGB
    return srgb2rgb(a[index]);
    #else
    return a[index];
    #endif
    #endif
}

vec3 paletteLerp(vec3 a[PALETTE_LERP_SIZE], float t){
    float size = float(PALETTE_LERP_SIZE) - 1.0;
    float index = t * size;
    float index1 = floor(min(index + 1.0, size));
    vec3 result = PALETTE_LERP_MIX_FNC( paletteLerp_get(a, int(index1)), 
                                        paletteLerp_get(a, int(index)), 
                                        index1 - index);

    #ifdef PALETTE_LERP_SRGB
    return rgb2srgb(result);
    #else
    return result;
    #endif
}

#endif

Examples

Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository