mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-14 11:45:52 -07:00
#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:
@@ -1,5 +1,6 @@
|
|||||||
package org.bukkit.map;
|
package org.bukkit.map;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
@@ -238,6 +239,10 @@ public final class MapPalette {
|
|||||||
public static byte matchColor(@NotNull Color color) {
|
public static byte matchColor(@NotNull Color color) {
|
||||||
if (color.getAlpha() < 128) return 0;
|
if (color.getAlpha() < 128) return 0;
|
||||||
|
|
||||||
|
if (mapColorCache != null && mapColorCache.isCached()) {
|
||||||
|
return mapColorCache.matchColor(color);
|
||||||
|
}
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
double best = -1;
|
double best = -1;
|
||||||
|
|
||||||
@@ -266,4 +271,44 @@ public final class MapPalette {
|
|||||||
// Minecraft has 143 colors, some of which have negative byte representations
|
// Minecraft has 143 colors, some of which have negative byte representations
|
||||||
return colors[index >= 0 ? index : index + 256];
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user