From 25d5416102ad508903c094d0ac4d1691e9ce7e3d Mon Sep 17 00:00:00 2001 From: Niels Joubert Date: Fri, 8 May 2015 20:51:51 -0700 Subject: [PATCH 1/2] Lerping Color Correctly --- lib/p5.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/p5.js b/lib/p5.js index 1395b0a481..c9b93bb53d 100644 --- a/lib/p5.js +++ b/lib/p5.js @@ -2103,17 +2103,17 @@ amdclean['colorcreating_reading'] = function (require, core, p5Color) { if (c1 instanceof Array) { var c = []; for (var i = 0; i < c1.length; i++) { - c.push(p5.prototype.lerp(c1[i], c2[i], amt)); + c.push(Math.sqrt(p5.prototype.lerp(c1[i]*c1[i], c2[i]*c2[i], amt))); } return c; } else if (c1 instanceof p5.Color) { var pc = []; for (var j = 0; j < 4; j++) { - pc.push(p5.prototype.lerp(c1.rgba[j], c2.rgba[j], amt)); + pc.push(Math.sqrt(p5.prototype.lerp(c1.rgba[j]*c1.rgba[j], c2.rgba[j]*c2.rgba[j], amt))); } return new p5.Color(this, pc); } else { - return p5.prototype.lerp(c1, c2, amt); + return Math.sqrt(p5.prototype.lerp(c1*c1, c2*c2, amt)); } }; p5.prototype.red = function (c) { From 27b67a92cbd6269a9b7fb7115b3bb60c7470394c Mon Sep 17 00:00:00 2001 From: Niels Joubert Date: Fri, 8 May 2015 21:51:10 -0700 Subject: [PATCH 2/2] Clamp lerpColor amt value to [0,1] as the documentation suggests --- lib/p5.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/p5.js b/lib/p5.js index c9b93bb53d..5e53d2569e 100644 --- a/lib/p5.js +++ b/lib/p5.js @@ -2100,6 +2100,7 @@ amdclean['colorcreating_reading'] = function (require, core, p5Color) { return c.getHue(); }; p5.prototype.lerpColor = function (c1, c2, amt) { + amt = Math.max(Math.min(amt, 1), 0); if (c1 instanceof Array) { var c = []; for (var i = 0; i < c1.length; i++) {