Skip to content

Commit

Permalink
api: TextColor.lerp
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike authored and zml2008 committed Jun 7, 2021
1 parent 54d768b commit f519a33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 23 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/format/TextColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ public interface TextColor extends Comparable<TextColor>, Examinable, RGBLike, S
return this.value() & 0xff;
}

/**
* 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
* @since 4.8.0
*/
static @NonNull TextColor lerp(final float t, final @NonNull TextColor a, final @NonNull TextColor b) {
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))
);
}

@Override
default void styleApply(final Style.@NotNull Builder style) {
style.color(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ void testExtractComponents() {
}

@Test
void testEquality() {
void testLerp() {
assertEquals(TextColor.color(0x808080), TextColor.lerp(0.50f, TextColor.color(0xffffff), TextColor.color(0x000000)));
assertEquals(TextColor.color(0x3399FF), TextColor.lerp(0.50f, TextColor.color(0x3366FF), TextColor.color(0x33CCFF)));
}

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

0 comments on commit f519a33

Please sign in to comment.