Skip to content

Commit a5f73c0

Browse files
author
Lauren McCarthy
committed
Merge pull request #615 from njoubert/correct_color_blend
Lerping Color Correctly
2 parents 3b81277 + 27b67a9 commit a5f73c0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/p5.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,20 +2100,21 @@ amdclean['colorcreating_reading'] = function (require, core, p5Color) {
21002100
return c.getHue();
21012101
};
21022102
p5.prototype.lerpColor = function (c1, c2, amt) {
2103+
amt = Math.max(Math.min(amt, 1), 0);
21032104
if (c1 instanceof Array) {
21042105
var c = [];
21052106
for (var i = 0; i < c1.length; i++) {
2106-
c.push(p5.prototype.lerp(c1[i], c2[i], amt));
2107+
c.push(Math.sqrt(p5.prototype.lerp(c1[i]*c1[i], c2[i]*c2[i], amt)));
21072108
}
21082109
return c;
21092110
} else if (c1 instanceof p5.Color) {
21102111
var pc = [];
21112112
for (var j = 0; j < 4; j++) {
2112-
pc.push(p5.prototype.lerp(c1.rgba[j], c2.rgba[j], amt));
2113+
pc.push(Math.sqrt(p5.prototype.lerp(c1.rgba[j]*c1.rgba[j], c2.rgba[j]*c2.rgba[j], amt)));
21132114
}
21142115
return new p5.Color(this, pc);
21152116
} else {
2116-
return p5.prototype.lerp(c1, c2, amt);
2117+
return Math.sqrt(p5.prototype.lerp(c1*c1, c2*c2, amt));
21172118
}
21182119
};
21192120
p5.prototype.red = function (c) {

0 commit comments

Comments
 (0)