Skip to content

Commit

Permalink
api: Improve lerp JD, inputs TextColor -> RGBLike
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Jun 7, 2021
1 parent f519a33 commit 6805744
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions api/src/main/java/net/kyori/adventure/text/format/TextColor.java
Expand Up @@ -237,23 +237,26 @@ public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, S
/**
* Linearly interpolates between {@code a} and {@code b} by {@code t}.
*
* @param t the interpolation value, between {@code 0.0} and {@code 1.0}
* @param a the lower bound
* @param b the upper bound
* @return the interpolated value
* <p>This returns a color blended between color {@code a}, at {@code t=0.0}, and color {@code b}, at {@code t=1.0}.</p>
*
* @param t the interpolation value, between {@code 0.0} and {@code 1.0} (both inclusive)
* @param a the lower bound ({@code t=0.0})
* @param b the upper bound ({@code t=1.0})
* @return the interpolated value, a color between the two input colors {@code a} and {@code b}
* @since 4.8.0
*/
static @NonNull TextColor lerp(final float t, final @NonNull TextColor a, final @NonNull TextColor b) {
static @NotNull TextColor lerp(final float t, final @NotNull RGBLike a, final @NotNull RGBLike b) {
final float clampedT = Math.min(1.0f, Math.max(0.0f, t)); // clamp between 0 and 1
final int ar = a.red();
final int br = b.red();
final int ag = a.green();
final int bg = b.green();
final int ab = a.blue();
final int bb = b.blue();
return color(
Math.round(ar + t * (br - ar)),
Math.round(ag + t * (bg - ag)),
Math.round(ab + t * (bb - ab))
Math.round(ar + clampedT * (br - ar)),
Math.round(ag + clampedT * (bg - ag)),
Math.round(ab + clampedT * (bb - ab))
);
}

Expand Down
Expand Up @@ -103,7 +103,7 @@ void testLerp() {
}

@Test
public void testEquality() {
void testEquality() {
new EqualsTester()
.addEqualityGroup(
TextColor.color(0xff0000),
Expand Down

0 comments on commit 6805744

Please sign in to comment.