Yatopia/patches/api/0011-Optimise-Bukkit-s-MapPalette.patch

111 lines
4.0 KiB
Diff
Raw Normal View History

2020-12-02 17:55:59 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: epserv <admin@epserv.ru>
2020-12-02 19:19:50 +01:00
Date: Wed, 2 Dec 2020 19:31:20 +0200
Subject: [PATCH] Optimise Bukkit's MapPalette
2020-12-02 17:55:59 +01:00
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
2020-12-02 20:41:07 +01:00
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..c7ebce72a97791da9b61dd04b8b7bb26c3ec7bce 100644
2020-12-02 17:55:59 +01:00
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
2020-12-02 19:19:50 +01:00
@@ -23,6 +23,8 @@ public final class MapPalette {
2020-12-02 17:55:59 +01:00
}
2020-12-02 20:41:07 +01:00
2020-12-02 19:19:50 +01:00
private static double getDistance(@NotNull Color c1, @NotNull Color c2) {
+ // Yatopia start - faster method
+ /*
double rmean = (c1.getRed() + c2.getRed()) / 2.0;
double r = c1.getRed() - c2.getRed();
double g = c1.getGreen() - c2.getGreen();
2020-12-02 20:41:07 +01:00
@@ -31,10 +33,20 @@ public final class MapPalette {
2020-12-02 19:19:50 +01:00
double weightG = 4.0;
double weightB = 2 + (255 - rmean) / 256.0;
return weightR * r * r + weightG * g * g + weightB * b * b;
+ */
+ if (c1.getRGB() == c2.getRGB()) {
+ return 0.0;
+ }
+ double r = c1.getRed() - c2.getRed(),
+ g = c1.getGreen() - c2.getGreen(),
+ b = c1.getBlue() - c2.getBlue(),
+ x = (c1.getRed() + c2.getRed()) / 512d + 2d;
+ return x * r * r + 4 * g * g + (4.99609375d - x) * b * b;
+ // Yatopia end
2020-12-02 17:55:59 +01:00
}
2020-12-02 20:41:07 +01:00
2020-12-02 17:55:59 +01:00
@NotNull
2020-12-02 20:41:07 +01:00
- static final Color[] colors = {
+ static Color[] colors = { // Yatopia
c(0, 0, 0), c(0, 0, 0), c(0, 0, 0), c(0, 0, 0),
c(89, 125, 39), c(109, 153, 48), c(127, 178, 56), c(67, 94, 29),
c(174, 164, 115), c(213, 201, 140), c(247, 233, 163), c(130, 123, 86),
@@ -96,6 +108,21 @@ public final class MapPalette {
2020-12-02 17:55:59 +01:00
c(14, 127, 93), c(17, 155, 114), c(20, 180, 133), c(10, 95, 70)
};
2020-12-02 20:41:07 +01:00
2020-12-02 19:19:50 +01:00
+ // Yatopia start - all colors as rgb integers
2020-12-02 17:55:59 +01:00
+ @NotNull
+ static final int[] rgbColors = new int[colors.length];
+ static {
2020-12-02 19:19:50 +01:00
+ for (int i = 0; i < colors.length; i++) {
2020-12-02 17:55:59 +01:00
+ rgbColors[i] = colors[i].getRGB();
2020-12-02 19:19:50 +01:00
+ }
+ java.util.Arrays.sort(rgbColors);
2020-12-02 20:41:07 +01:00
+ Color[] sortedColorsArray = java.util.Arrays.stream(colors)
+ .sorted(java.util.Comparator.comparingInt(Color::getRGB))
+ .toArray(Color[]::new);
+ colors = sortedColorsArray;
2020-12-02 17:55:59 +01:00
+ }
2020-12-02 19:19:50 +01:00
+ // Yatopia end
2020-12-02 17:55:59 +01:00
+
// Interface
/**
* @deprecated Magic value
2020-12-02 20:41:07 +01:00
@@ -235,19 +262,43 @@ public final class MapPalette {
2020-12-02 17:55:59 +01:00
public static byte matchColor(@NotNull Color color) {
if (color.getAlpha() < 128) return 0;
2020-12-02 20:41:07 +01:00
- int index = 0;
2020-12-02 19:19:50 +01:00
+ // Yatopia start - binary search first
+ int binaryIndex = java.util.Arrays.binarySearch(rgbColors, color.getRGB());
+ if (binaryIndex != -1) {
2020-12-02 17:55:59 +01:00
+ return (byte) (binaryIndex < 128 ? binaryIndex : binaryIndex - 256);
2020-12-02 19:19:50 +01:00
+ }
+ // Yatopia end
+
2020-12-02 20:41:07 +01:00
+ short index = 0; // Yatopia
2020-12-02 19:19:50 +01:00
double best = -1;
2020-12-02 20:41:07 +01:00
- for (int i = 4; i < colors.length; i++) {
+ for (short i = 4; i < colors.length; i++) { // Yatopia
2020-12-02 19:19:50 +01:00
double distance = getDistance(color, colors[i]);
+ // Yatopia start - optimise this
+ /*
if (distance < best || best == -1) {
2020-12-02 17:55:59 +01:00
best = distance;
index = i;
}
2020-12-02 19:19:50 +01:00
+ */
+ if (i == 4) {
+ if (distance == 0) {
+ return (byte) i;
+ }
+ best = distance;
+ index = i;
+ } else if (distance < best) {
+ if (distance == 0) {
2020-12-02 20:41:07 +01:00
+ return (byte) (i < 128 ? i : i - 256);
2020-12-02 19:19:50 +01:00
+ }
+ best = distance;
+ index = i;
+ }
+ // Yatopia end
2020-12-02 17:55:59 +01:00
}
2020-12-02 20:41:07 +01:00
2020-12-02 17:55:59 +01:00
// Minecraft has 143 colors, some of which have negative byte representations
- return (byte) (index < 128 ? index : -129 + (index - 127));
2020-12-02 19:19:50 +01:00
+ return (byte) (index < 128 ? index : index - 256); // Yatopia
2020-12-02 17:55:59 +01:00
}
/**