Search code examples
functionmathcolorsprocessing.jslerp

How to make a lerpColor() function without ProcessingJS


I would like to create my own lerpColor(c1, c2, amount) function, with the same output as the one in ProcessingJS http://processingjs.org/reference/lerpColor_/

I am looking or something like this:

lerpColor = function (c1, c2, amount) {
    // Do some math stuff here
    return(newColor);
};

Any help would be greatly appreciated.


Solution

  • If this color interpolation works purely in RGB color space, you need to extract color components and apply the next arithmetics to each component

    r1 = red(c1)
    r2 = red(c2)
    result_red = (1-amount) * r1  + amount * r2
    or 
    result_red = r1 + amount * (r2 - r1)
    ...
    return(color(result_red, result_green, result_blue));