Skip to content

Commit 4b65796

Browse files
committed
added lerpColor(int, int, float) to PApplet
1 parent b67d55f commit 4b65796

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

core/src/processing/core/PApplet.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7471,12 +7471,20 @@ public final int color(float x, float y, float z, float a) {
74717471
}
74727472

74737473

7474+
public int lerpColor(int c1, int c2, float amt) {
7475+
if (g != null) {
7476+
return g.lerpColor(c1, c2, amt);
7477+
}
7478+
// use the default mode (RGB) if lerpColor is called before setup()
7479+
return PGraphics.lerpColor(c1, c2, amt, RGB);
7480+
}
7481+
7482+
74747483
static public int blendColor(int c1, int c2, int mode) {
74757484
return PImage.blendColor(c1, c2, mode);
74767485
}
74777486

74787487

7479-
74807488
//////////////////////////////////////////////////////////////
74817489

74827490
// MAIN

core/src/processing/core/PGraphics.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5702,13 +5702,16 @@ public int lerpColor(int c1, int c2, float amt) {
57025702

57035703
static float[] lerpColorHSB1;
57045704
static float[] lerpColorHSB2;
5705-
static float[] lerpColorHSB3;
5705+
static float[] lerpColorHSB3;
57065706

57075707
/**
57085708
* Interpolate between two colors. Like lerp(), but for the
57095709
* individual color components of a color supplied as an int value.
57105710
*/
57115711
static public int lerpColor(int c1, int c2, float amt, int mode) {
5712+
if (amt < 0) amt = 0;
5713+
if (amt > 1) amt = 1;
5714+
57125715
if (mode == RGB) {
57135716
float a1 = ((c1 >> 24) & 0xff);
57145717
float r1 = (c1 >> 16) & 0xff;
@@ -5719,10 +5722,10 @@ static public int lerpColor(int c1, int c2, float amt, int mode) {
57195722
float g2 = (c2 >> 8) & 0xff;
57205723
float b2 = c2 & 0xff;
57215724

5722-
return (((int) (a1 + (a2-a1)*amt) << 24) |
5723-
((int) (r1 + (r2-r1)*amt) << 16) |
5724-
((int) (g1 + (g2-g1)*amt) << 8) |
5725-
((int) (b1 + (b2-b1)*amt)));
5725+
return ((PApplet.round(a1 + (a2-a1)*amt) << 24) |
5726+
(PApplet.round(r1 + (r2-r1)*amt) << 16) |
5727+
(PApplet.round(g1 + (g2-g1)*amt) << 8) |
5728+
(PApplet.round(b1 + (b2-b1)*amt)));
57265729

57275730
} else if (mode == HSB) {
57285731
if (lerpColorHSB1 == null) {
@@ -5733,7 +5736,7 @@ static public int lerpColor(int c1, int c2, float amt, int mode) {
57335736

57345737
float a1 = (c1 >> 24) & 0xff;
57355738
float a2 = (c2 >> 24) & 0xff;
5736-
int alfa = ((int) (a1 + (a2-a1)*amt)) << 24;
5739+
int alfa = (PApplet.round(a1 + (a2-a1)*amt)) << 24;
57375740

57385741
Color.RGBToHSV((c1 >> 16) & 0xff, (c1 >> 8) & 0xff, c1 & 0xff,
57395742
lerpColorHSB1);
@@ -5765,23 +5768,21 @@ static public int lerpColor(int c1, int c2, float amt, int mode) {
57655768
}
57665769
float ho = (PApplet.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt)) % 1.0f;
57675770
*/
5771+
// float ho = PApplet.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt);
5772+
// float so = PApplet.lerp(lerpColorHSB1[1], lerpColorHSB2[1], amt);
5773+
// float bo = PApplet.lerp(lerpColorHSB1[2], lerpColorHSB2[2], amt);
57685774

5769-
// float ho = PActivity.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt);
5770-
// float so = PActivity.lerp(lerpColorHSB1[1], lerpColorHSB2[1], amt);
5771-
// float bo = PActivity.lerp(lerpColorHSB1[2], lerpColorHSB2[2], amt);
5772-
// return alfa | (Color.HSVtoRGB(ho, so, bo) & 0xFFFFFF);
5773-
// return Color.HSVToColor(alfa, new float[] { ho, so, bo });
5775+
// return alfa | (Color.RGBToHSV(ho, so, bo) & 0xFFFFFF);
57745776

57755777
lerpColorHSB3[0] = PApplet.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt);
57765778
lerpColorHSB3[1] = PApplet.lerp(lerpColorHSB1[1], lerpColorHSB2[1], amt);
57775779
lerpColorHSB3[2] = PApplet.lerp(lerpColorHSB1[2], lerpColorHSB2[2], amt);
5778-
return Color.HSVToColor(alfa, lerpColorHSB3);
5780+
return Color.HSVToColor(alfa, lerpColorHSB3);
57795781
}
57805782
return 0;
57815783
}
57825784

57835785

5784-
57855786
//////////////////////////////////////////////////////////////
57865787

57875788
// BEGINRAW/ENDRAW

0 commit comments

Comments
 (0)