diff options
author | Barnaby <[email protected]> | 2024-06-29 12:04:48 +0100 |
---|---|---|
committer | Barnaby <[email protected]> | 2024-06-29 12:04:48 +0100 |
commit | 14832a70e958012a3d5ed6c87540df565448b73c (patch) | |
tree | 3fdec8270cea0eb5d513ad6cebd251feb2c7c166 /paper-api/src | |
parent | 30b4dedabbf2c4db3368b7a60fcbbe6935fd1aaa (diff) | |
download | Paper-14832a70e958012a3d5ed6c87540df565448b73c.tar.gz Paper-14832a70e958012a3d5ed6c87540df565448b73c.zip |
Optimise color distance check in MapPalette by removing floating point math
Diffstat (limited to 'paper-api/src')
-rw-r--r-- | paper-api/src/main/java/org/bukkit/map/MapPalette.java | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/paper-api/src/main/java/org/bukkit/map/MapPalette.java b/paper-api/src/main/java/org/bukkit/map/MapPalette.java index 448f37249d..6995f9cc08 100644 --- a/paper-api/src/main/java/org/bukkit/map/MapPalette.java +++ b/paper-api/src/main/java/org/bukkit/map/MapPalette.java @@ -29,14 +29,19 @@ public final class MapPalette { } private static double getDistance(@NotNull Color c1, @NotNull Color c2) { - double rmean = (c1.getRed() + c2.getRed()) / 2.0; - double r = c1.getRed() - c2.getRed(); - double g = c1.getGreen() - c2.getGreen(); + // Paper start - Optimize color distance calculation by removing floating point math + int rsum = c1.getRed() + c2.getRed(); // Use sum instead of mean for no division + int r = c1.getRed() - c2.getRed(); + int g = c1.getGreen() - c2.getGreen(); int b = c1.getBlue() - c2.getBlue(); - double weightR = 2 + rmean / 256.0; - double weightG = 4.0; - double weightB = 2 + (255 - rmean) / 256.0; + // All weights are 512x their original to avoid floating point division + int weightR = 1024 + rsum; + int weightG = 2048; + int weightB = 1024 + (255*2 - rsum); + + // Division by 256 here is unnecessary as this won't change the result of the sort return weightR * r * r + weightG * g * g + weightB * b * b; + // Paper end } @NotNull |