Skip to content

Commit a32f909

Browse files
authored
Merge pull request #6708 from Bumblebee00/circular_color_lerp
lerpColor in HSL or HSB mode now can loop the color wheel if needed
2 parents f4cc7e1 + 3efb76c commit a32f909

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/color/creating_reading.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ p5.prototype.hue = function(c) {
380380
* @method lerpColor
381381
* @param {p5.Color} c1 interpolate from this color.
382382
* @param {p5.Color} c2 interpolate to this color.
383-
* @param {Number} amt number between 0 and 1.
383+
* @param {Number} amt number between 0 and 1.
384384
* @return {p5.Color} interpolated color.
385385
*
386386
* @example
@@ -429,7 +429,7 @@ p5.prototype.lerpColor = function(c1, c2, amt) {
429429
fromArray = c1.hsla;
430430
toArray = c2.hsla;
431431
} else {
432-
throw new Error(`${mode}cannot be used for interpolation.`);
432+
throw new Error(`${mode} cannot be used for interpolation.`);
433433
}
434434

435435
// Prevent extrapolation.
@@ -442,7 +442,22 @@ p5.prototype.lerpColor = function(c1, c2, amt) {
442442
}
443443

444444
// Perform interpolation.
445-
l0 = this.lerp(fromArray[0], toArray[0], amt);
445+
if (mode === constants.RGB) {
446+
l0 = this.lerp(fromArray[0], toArray[0], amt);
447+
}
448+
// l0 (hue) has to wrap around (and it's between 0 and 1)
449+
else {
450+
// find shortest path in the color wheel
451+
if (Math.abs(fromArray[0] - toArray[0]) > 0.5) {
452+
if (fromArray[0] > toArray[0]) {
453+
toArray[0] += 1;
454+
} else {
455+
fromArray[0] += 1;
456+
}
457+
}
458+
l0 = this.lerp(fromArray[0], toArray[0], amt);
459+
if (l0 >= 1) { l0 -= 1; }
460+
}
446461
l1 = this.lerp(fromArray[1], toArray[1], amt);
447462
l2 = this.lerp(fromArray[2], toArray[2], amt);
448463
l3 = this.lerp(fromArray[3], toArray[3], amt);

test/unit/color/creating_reading.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ suite('color/CreatingReading', function() {
159159
myp5.colorMode(myp5.HSL);
160160
var interA = myp5.lerpColor(fromColor, toColor, 0.33);
161161
var interB = myp5.lerpColor(fromColor, toColor, 0.66);
162-
assert.deepEqual(interA.levels, [66, 190, 44, 255]);
163-
assert.deepEqual(interB.levels, [53, 164, 161, 255]);
162+
assert.deepEqual(interA.levels, [190, 44, 63, 255]);
163+
assert.deepEqual(interB.levels, [164, 53, 162, 255]);
164164
});
165165
test('should correctly get lerp colors in HSB', function() {
166166
myp5.colorMode(myp5.HSB);
167167
var interA = myp5.lerpColor(fromColor, toColor, 0.33);
168168
var interB = myp5.lerpColor(fromColor, toColor, 0.66);
169-
assert.deepEqual(interA.levels, [69, 192, 47, 255]);
170-
assert.deepEqual(interB.levels, [56, 166, 163, 255]);
169+
assert.deepEqual(interA.levels, [192, 47, 66, 255]);
170+
assert.deepEqual(interB.levels, [166, 56, 164, 255]);
171171
});
172172
test('should not extrapolate', function() {
173173
var interA = myp5.lerpColor(fromColor, toColor, -0.5);
@@ -197,15 +197,15 @@ suite('color/CreatingReading', function() {
197197
myp5.colorMode(myp5.HSL);
198198
var interA = myp5.lerpColor(fromColor, toColor, 0.33);
199199
var interB = myp5.lerpColor(fromColor, toColor, 0.66);
200-
assert.deepEqual(interA.levels, [66, 190, 44, 99]);
201-
assert.deepEqual(interB.levels, [53, 164, 161, 149]);
200+
assert.deepEqual(interA.levels, [190, 44, 63, 99]);
201+
assert.deepEqual(interB.levels, [164, 53, 162, 149]);
202202
});
203203
test('should correctly get lerp colors in HSB with alpha', function() {
204204
myp5.colorMode(myp5.HSB);
205205
var interA = myp5.lerpColor(fromColor, toColor, 0.33);
206206
var interB = myp5.lerpColor(fromColor, toColor, 0.66);
207-
assert.deepEqual(interA.levels, [69, 192, 47, 99]);
208-
assert.deepEqual(interB.levels, [56, 166, 163, 149]);
207+
assert.deepEqual(interA.levels, [192, 47, 66, 99]);
208+
assert.deepEqual(interB.levels, [166, 56, 164, 149]);
209209
});
210210
test('should not extrapolate', function() {
211211
var interA = myp5.lerpColor(fromColor, toColor, -0.5);

0 commit comments

Comments
 (0)