p5.js lerpColor() Function

The lerpColor() function is used to interpolate two colors to find a third color between them. The amount of interpolation between the two colors can be set using the amt parameters. The color interpolation depends on the current color mode.
Syntax:
lerpColor(c1, c2, amt)
Parameters: This function accepts three parameters as mentioned above and described below:
- c1: It is a p5.Color which represents the first color from which the final color will be interpolated.
- c2: It is a p5.Color which represents the second color to which the final color will be interpolated.
- amt: It is a number between 0 and 1 which determines which color will be used more for the interpolation. A value near 0.1 would prefer the first color more and a value near 0.9 would prefer the second color for interpolation.
Return Value: It returns a p5.Color element with the interpolated color.
The example below illustrate the lerpColor() function in p5.js:
Example:
| functionsetup() {   createCanvas(500, 350);   textSize(18);    Â  text("From Color", 20, 20);   fromColor = color("red");  Â  text("Lerped Color", 150, 20);  Â  text("To Color", 300, 20);   toColor = color("blue");    Â  text("Adjust this slider to change the"+              " amount of lerping", 20,  200)   alphaSlider = createSlider(0, 100, 50);   alphaSlider.position(20, 220);   alphaSlider.style('width', '250px'); }  Âfunctiondraw() {   lerpedColor = lerpColor(fromColor, toColor, alphaSlider.value() / 100);  Â  fill(fromColor);   rect(30, 30, 50, 100);  Â  fill(lerpedColor);   rect(170, 30, 50, 100);  Â  fill(toColor);   rect(310, 30, 50, 100); }  | 
Output:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.zambiatek.com/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5/lerpColor
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



