aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anthonyhilyard/iceberg/util/Easing.java
blob: 4146900e5f7bfaeb315231a36ea1598160b9bcbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.anthonyhilyard.iceberg.util;

import net.minecraft.util.text.Color;

/**
 * Helper functions for smooth easing/interpolation.  If you need linear, use net.minecraft.util.math.MathHelper.lerp instead.
 */
public final class Easing
{
	public static enum EasingType
	{
		None, // Produces step-wise interpolation.
		Linear,
		Quad,
		Cubic
	}

	public static enum EasingDirection
	{
		In,
		Out,
		InOut
	}

	public static float Ease(float a, float b, float t)
	{
		return Ease(a, b, t, EasingType.Quad);
	}

	public static float Ease(float a, float b, float t, EasingType type)
	{
		return Ease(a, b, t, type, EasingDirection.InOut);
	}

	public static Color Ease(Color a, Color b, float t, EasingType type)
	{
		// Helper function to ease between colors.
		// Ease each component individually.
		int aV = a.getValue(), bV = b.getValue();
		int aA = (aV >> 24) & 0xFF, aR = (aV >> 16) & 0xFF, aG = (aV >> 8) & 0xFF, aB = (aV >> 0) & 0xFF,
			bA = (bV >> 24) & 0xFF, bR = (bV >> 16) & 0xFF, bG = (bV >> 8) & 0xFF, bB = (bV >> 0) & 0xFF;
		return Color.fromRgb((int)Ease(aA, bA, t, type) << 24 |
							 (int)Ease(aR, bR, t, type) << 16 |
							 (int)Ease(aG, bG, t, type) << 8  |
							 (int)Ease(aB, bB, t, type) << 0);
	}

	public static float Ease(float a, float b, float t, EasingType type, EasingDirection direction)
	{
		switch (type)
		{
			case None:
			default:
				return None(a, b, t);
			case Linear:
				return Linear(a, b, t);
			case Quad:
				return Quad(a, b, t, direction);
			case Cubic:
				return Cubic(a, b, t, direction);
		}
	}

	private static float None(float a, float b, float t)
	{
		if (t < 0.5f)
		{
			return a;
		}
		else
		{
			return b;
		}
	}

	private static float Linear(float a, float b, float t)
	{
		return a + (b - a) * t;
	}

	private static float Quad(float a, float b, float t, EasingDirection direction)
	{
		switch (direction)
		{
			case In:
				return a + (b - a) * t * t;
			case Out:
				return a + (b - a) * (1.0f - (1.0f - t) * (1.0f - t));
			case InOut:
			default:
			{
				t *= 2.0f;
				if (t < 1.0f)
				{
					return a + (b - a) * 0.5f * t * t;
				}
				else
				{
					t -= 2.0f;
					return a + (a - b) * 0.5f * (t * t - 2.0f);
				}
			}
		}
	}

	private static float Cubic(float a, float b, float t, EasingDirection direction)
	{
		switch (direction)
		{
			case In:
				return a + (b - a) * t * t * t;
			case Out:
				return a + (b - a) * (1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t));
			case InOut:
			default:
			{
				t *= 2.0f;
				if (t < 1.0f)
				{
					return a + (b - a) * 0.5f * t * t * t;
				}
				else
				{
					t -= 2.0f;
					return a + (b - a) * 0.5f * (t * t * t + 2.0f);
				}
			}
		}
	}
}