Reimplement ChatColorGradient.pickColorAt() to make it less error-prone

This commit is contained in:
Marc Baloup 2023-07-04 23:15:06 +02:00
parent 79e4bb90f7
commit 9e7d89cf70
2 changed files with 28 additions and 17 deletions

View File

@ -4,12 +4,21 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.NotNull;
/** /**
* A custom gradient with at least 2 colors in it. * A custom gradient with at least 2 colors in it.
*/ */
public class ChatColorGradient { public class ChatColorGradient {
private record GradientColor(float location, TextColor color) { } private record GradientColor(
float location,
TextColor color
) implements Comparable<GradientColor> {
@Override
public int compareTo(@NotNull ChatColorGradient.GradientColor o) {
return Float.compare(location(), o.location());
}
}
private final List<GradientColor> colors = new ArrayList<>(); private final List<GradientColor> colors = new ArrayList<>();
@ -21,6 +30,7 @@ public class ChatColorGradient {
*/ */
public synchronized ChatColorGradient add(float gradientLocation, TextColor gradientColor) { public synchronized ChatColorGradient add(float gradientLocation, TextColor gradientColor) {
colors.add(new GradientColor(gradientLocation, gradientColor)); colors.add(new GradientColor(gradientLocation, gradientColor));
colors.sort(null);
return this; return this;
} }
@ -31,25 +41,26 @@ public class ChatColorGradient {
*/ */
public synchronized TextColor pickColorAt(float gradientLocation) { public synchronized TextColor pickColorAt(float gradientLocation) {
if (colors.isEmpty()) if (colors.isEmpty())
throw new IllegalStateException("Must define at least one color in this ChatValueGradient instance."); throw new IllegalStateException("Must define at least one color in this ChatColorGradient instance.");
if (colors.size() == 1) if (colors.size() == 1)
return colors.get(0).color(); return colors.get(0).color();
colors.sort((p1, p2) -> Float.compare(p1.location(), p2.location())); int i = 0;
for (; i < colors.size(); i++) {
if (gradientLocation <= colors.get(0).location()) if (gradientLocation <= colors.get(i).location())
return colors.get(0).color();
if (gradientLocation >= colors.get(colors.size() - 1).location())
return colors.get(colors.size() - 1).color();
int p1 = 1;
for (; p1 < colors.size(); p1++) {
if (colors.get(p1).location() >= gradientLocation)
break; break;
} }
int p0 = p1 - 1;
float v0 = colors.get(p0).location(), v1 = colors.get(p1).location(); if (i == 0)
TextColor cc0 = colors.get(p0).color(), cc1 = colors.get(p1).color(); return colors.get(i).color();
return ChatColorUtil.interpolateColor(v0, v1, gradientLocation, cc0, cc1); if (i == colors.size())
return colors.get(colors.size() - 1).color();
int p = i - 1;
float pLoc = colors.get(p).location();
float iLoc = colors.get(i).location();
TextColor pCol = colors.get(p).color();
TextColor iCol = colors.get(i).color();
return ChatColorUtil.interpolateColor(pLoc, iLoc, gradientLocation, pCol, iCol);
} }
} }

View File

@ -302,7 +302,7 @@ public class PerformanceAnalysisManager implements Listener {
for (int i = 58; i >= 0; i--) { for (int i = 58; i >= 0; i--) {
int t = tpsHistory[i]; int t = tpsHistory[i];
ChatColor newC = ChatColorUtil.toBungee(tps1sGradient.pickColorAt(t)); ChatColor newC = ChatColorUtil.toBungee(tps1sGradient.pickColorAt(t));
if (newC != prevC) { if (!newC.equals(prevC)) {
s.append(newC); s.append(newC);
prevC = newC; prevC = newC;
} }