Skip to content

Instantly share code, notes, and snippets.

@IllusionTheDev
Created October 11, 2024 02:27
Show Gist options
  • Save IllusionTheDev/c5d0f083696b8fc9b6cdb47acd24ac15 to your computer and use it in GitHub Desktop.
Save IllusionTheDev/c5d0f083696b8fc9b6cdb47acd24ac15 to your computer and use it in GitHub Desktop.
Beacon color glass calculation
import org.bukkit.Color;
import org.bukkit.Material;
public enum GlassColor {
RED(Material.RED_STAINED_GLASS_PANE, Color.fromARGB(0x00FF0000)),
ORANGE(Material.ORANGE_STAINED_GLASS_PANE, Color.fromARGB(0x00FFA500)),
YELLOW(Material.YELLOW_STAINED_GLASS_PANE, Color.fromARGB(0x00FFFF00)),
LIME(Material.LIME_STAINED_GLASS_PANE, Color.fromARGB(0x0000FF00)),
GREEN(Material.GREEN_STAINED_GLASS_PANE, Color.fromARGB(0x00008000)),
CYAN(Material.CYAN_STAINED_GLASS_PANE, Color.fromARGB(0x0000FFFF)),
BLUE(Material.BLUE_STAINED_GLASS_PANE, Color.fromARGB(0x000000FF)),
PURPLE(Material.PURPLE_STAINED_GLASS_PANE, Color.fromARGB(0x00800080)),
MAGENTA(Material.MAGENTA_STAINED_GLASS_PANE, Color.fromARGB(0x00FF00FF)),
PINK(Material.PINK_STAINED_GLASS_PANE, Color.fromARGB(0x00FFC0CB)),
WHITE(Material.WHITE_STAINED_GLASS_PANE, Color.fromARGB(0x00FFFFFF)),
LIGHT_GRAY(Material.LIGHT_GRAY_STAINED_GLASS_PANE, Color.fromARGB(0x00D3D3D3)),
GRAY(Material.GRAY_STAINED_GLASS_PANE, Color.fromARGB(0x00808080)),
BLACK(Material.BLACK_STAINED_GLASS_PANE, Color.fromARGB(0x00000000));
private final Material type;
private final Color color;
GlassColor(Material type, Color color) {
this.type = type;
this.color = color;
}
public Material getType() {
return this.type;
}
public Color getColor() {
return this.color;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Color;
public class GlassColorCache {
private static final Map<Color, List<GlassColor>> cache = cacheColors();
public static List<GlassColor> calculateGlassColors(Color color) {
// Find the closest list in the cache
List<GlassColor> closest = null;
double closestDistance = Double.MAX_VALUE;
for (final Map.Entry<Color, List<GlassColor>> entry : cache.entrySet()) {
final double distance = calculateDistance(color, entry.getKey());
if (distance < closestDistance) {
closest = entry.getValue();
closestDistance = distance;
}
}
return closest;
}
private static Map<Color, List<GlassColor>> cacheColors() {
// We can only have up to 4 glass colors, so we create every combination and calculate the closest color
final Map<Color, List<GlassColor>> cache = new HashMap<>();
for (int colors = 1; colors <= 4; colors++) {
final List<List<GlassColor>> combinations = getCombinations(colors);
for (final List<GlassColor> combination : combinations) {
final Color color = calculateColor(combination);
if (!cache.containsKey(color)) {
cache.put(color, combination);
continue;
}
}
}
return cache;
}
private static List<List<GlassColor>> getCombinations(int iterations) {
List<List<GlassColor>> combinations = new ArrayList<>();
if (iterations == 0) {
return combinations;
}
for (final GlassColor color : GlassColor.values()) {
final List<GlassColor> combination = new ArrayList<>();
combination.add(color);
combinations.add(combination);
}
for (int i = 1; i < iterations; i++) {
final List<List<GlassColor>> newCombinations = new ArrayList<>();
for (final List<GlassColor> combination : combinations) {
for (final GlassColor color : GlassColor.values()) {
final List<GlassColor> newCombination = new ArrayList<>(combination);
newCombination.add(color);
newCombinations.add(newCombination);
}
}
combinations = newCombinations;
}
return combinations;
}
private static Color calculateColor(List<GlassColor> colors) {
Color current = Color.fromRGB(0, 0, 0);
for (final GlassColor color : colors) {
final Color glassColor = color.getColor();
// The new color is the average between "current" and "glassColor"
// (The last value is half of the current and half of the new color)
current = Color.fromRGB(
(current.getRed() + glassColor.getRed()) / 2,
(current.getGreen() + glassColor.getGreen()) / 2,
(current.getBlue() + glassColor.getBlue()) / 2
);
}
return current;
}
private static double calculateDistance(Color color1, Color color2) {
return Math.sqrt(Math.pow(color1.getRed() - color2.getRed(), 2) + Math.pow(color1.getGreen() - color2.getGreen(), 2) + Math.pow(color1.getBlue() - color2.getBlue(), 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment