#746: Add option to use cached map color palette

This reduces the conversion time drastically with the cost of slightly more memory usage.

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2022-06-18 21:13:40 +10:00
parent 1a5ff83244
commit 2ca50720e4

View file

@ -1,5 +1,6 @@
package org.bukkit.map;
import com.google.common.base.Preconditions;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
@ -238,6 +239,10 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
if (color.getAlpha() < 128) return 0;
if (mapColorCache != null && mapColorCache.isCached()) {
return mapColorCache.matchColor(color);
}
int index = 0;
double best = -1;
@ -266,4 +271,44 @@ public final class MapPalette {
// Minecraft has 143 colors, some of which have negative byte representations
return colors[index >= 0 ? index : index + 256];
}
private static MapColorCache mapColorCache;
/**
* Sets the given MapColorCache.
*
* @param mapColorCache The map color cache to set
*/
public static void setMapColorCache(@NotNull MapColorCache mapColorCache) {
Preconditions.checkState(MapPalette.mapColorCache == null, "Map color cache already set");
MapPalette.mapColorCache = mapColorCache;
}
/**
* Holds cached information for matching map colors of a given RBG color.
*/
public interface MapColorCache {
/**
* Returns true if the MapColorCache has values cached, if not it will
* return false.
* A case where it might return false is when the cache is not build jet.
*
* @return true if this MapColorCache has values cached otherwise false
*/
boolean isCached();
/**
* Get the cached index of the closest matching color in the palette to the given
* color.
*
* @param color The Color to match.
* @return The index in the palette.
* @throws IllegalStateException if {@link #isCached()} returns false
* @deprecated Magic value
*/
@Deprecated
byte matchColor(@NotNull Color color);
}
}