Skip to content

Commit

Permalink
fix fadeIn()
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiringLich committed Mar 13, 2023
1 parent 8dcb430 commit 05bbff8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/paintingcanvas/animation/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static OpacityAnimation fadeOut() {
* @see #fadeOut()
*/
public static OpacityAnimation fadeIn() {
return new OpacityAnimation(1);
return new OpacityAnimation(255);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/paintingcanvas/animation/ColorAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ Color lerpColor(Color _a, Color _b, double delta) {
var b = _a.getBlue() + (_b.getBlue() - _a.getBlue()) * delta;
var a = _a.getAlpha() + (_b.getAlpha() - _a.getAlpha()) * delta;

return new Color(Misc.clamp((int) r, 0, 255), Misc.clamp((int) g, 0, 255), Misc.clamp((int) b, 0, 255), Misc.clamp((int) a, 0, 255));
return new Color(Misc.clamp(0, (int) r, 255), Misc.clamp(0, (int) g, 255), Misc.clamp(255, (int) b, 255), Misc.clamp(0, (int) a, 255));
}
}
18 changes: 14 additions & 4 deletions src/main/java/paintingcanvas/animation/OpacityAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@ public OpacityAnimation(double end) {
this.end = (int) (end * 255.0);
}

public OpacityAnimation(int end) {
super();
this.end = end;
}

@Override
public Animation copy() {
var out = new OpacityAnimation(end);
var out = new OpacityAnimation(this.end);
out.copy(this);
return out;
}

@Override
protected void updateAnimation(Drawable<? extends Drawable<?>> drawable, double progress) {
var t = easing.ease(progress);

Color color = drawable.color;
int alpha = (int) (start + (end - start) * easing.ease(progress));
int alpha = (int) (start + (end - start) * t);
// System.err.println(end);
alpha = Misc.clamp(0, alpha, 255);
drawable.color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);

Color outlineColor = drawable.outlineColor;
int outlineAlpha = (int) (start + (end - start) * easing.ease(progress));
outlineAlpha = Misc.clamp(outlineAlpha, 0, 255);
int outlineAlpha = (int) (start + (end - start) * t);
outlineAlpha = Misc.clamp(0, outlineAlpha, 255);
drawable.outlineColor = new Color(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue(), outlineAlpha);
}

@Override
protected void initAnimation(Drawable<? extends Drawable<?>> drawable) {
this.start = drawable.color.getAlpha();
// System.out.println(start);
this.outlineStart = drawable.outlineColor.getAlpha();
}
}
2 changes: 1 addition & 1 deletion src/main/java/paintingcanvas/misc/Misc.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package paintingcanvas.misc;

public class Misc {
public static <N extends Number> N clamp(N val, N min, N max) {
public static <N extends Number> N clamp(N min, N val, N max) {
if (val.doubleValue() > max.doubleValue()) return max;
if (val.doubleValue() < min.doubleValue()) return min;
return val;
Expand Down
19 changes: 11 additions & 8 deletions src/test/java/examples/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ public static void main(String[] args) {
Line l = new Line(0, 0, 100, 100, Color.RED).setColor(Color.blue);
Circle c = new Circle(canvas.getWidth() / 2, canvas.getHeight() / 2, 50, Color.RED);
while (true) {
c.moveBy(100, 1, 1);
canvas.sleep(1);
c.moveBy(0, 100, 1);
canvas.sleep(1);
c.moveBy(-100, 1, 1);
canvas.sleep(1);
c.moveBy(0, -100, 1);
canvas.sleep(1);
// c.moveHorizontalBy(100, 1);
// canvas.sleep(1);
// c.moveVerticalBy(100, 1);
// canvas.sleep(1);
// c.moveHorizontalBy(-100, 1);
// canvas.sleep(1);
// c.moveVerticalBy(-100, 1);
// canvas.sleep(1);
c.fadeOut(1);
c.fadeIn(1);
c.colorTo(Color.BLUE, 2);
}
}
}

0 comments on commit 05bbff8

Please sign in to comment.