Yatopia/patches/api/0011-Optimise-Bukkit-s-MapPalette.patch
Ivan Pekov b6b0e23f1a
More progress
we've got it to render using binarySearch but its not ideal and it has the same speed as without it....
2020-12-03 10:15:07 +02:00

72 lines
2.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: epserv <admin@epserv.ru>
Date: Wed, 2 Dec 2020 19:31:20 +0200
Subject: [PATCH] Optimise Bukkit's MapPalette
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..418fde209a2c4ce71dbb8e41af32cd87cfc71ed4 100644
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
@@ -23,6 +23,8 @@ public final class MapPalette {
}
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();
@@ -31,6 +33,16 @@ public final class MapPalette {
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
}
@NotNull
@@ -235,19 +247,31 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
if (color.getAlpha() < 128) return 0;
- int index = 0;
+ // Yatopia start - binary search first
+ int binaryIndex = java.util.Arrays.binarySearch(
+ colors,
+ color,
+ java.util.Comparator.comparingDouble(o -> getDistance(color, o))
+ );
+ if (binaryIndex != -1) {
+ return (byte) (binaryIndex < 128 ? binaryIndex : binaryIndex - 256);
+ }
+ // Yatopia end
+
+ short index = 0; // Yatopia
double best = -1;
- for (int i = 4; i < colors.length; i++) {
+ for (short i = 4; i < colors.length; i++) { // Yatopia
double distance = getDistance(color, colors[i]);
if (distance < best || best == -1) {
best = distance;
index = i;
+ if (distance == 0) { break; } // Yatopia
}
}
// Minecraft has 143 colors, some of which have negative byte representations
- return (byte) (index < 128 ? index : -129 + (index - 127));
+ return (byte) (index < 128 ? index : index - 256); // Yatopia
}
/**