import java.awt.*; import java.util.Comparator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ColorMatcher { private static final Color[] MAP_COLORS; private static final Comparator COMPARATOR = new BasicEuclideanComparator(); private static final Map matchedColors = new ConcurrentHashMap<>(); static { final Color[] BaseMapColors = new Color[] { new Color(0, 0, 0, 0), // transparent new Color(127, 178, 56), // grass new Color(247, 233, 163), // sand new Color(199, 199, 199), // wool new Color(255, 0, 0), // fire new Color(160, 160, 255), // ice new Color(167, 167, 167), // metal new Color(0, 124, 0), // Plant new Color(255, 255, 255), // Snow new Color(164, 168, 184), // Clay new Color(151, 109, 77), // Dirt new Color(112, 112, 112), // Stone new Color(64, 64, 255), // Water new Color(143, 119, 72), // Wood //new 1.7 colors (13w42a/13w42b) new Color(255, 252, 245), // Quartz new Color(216, 127, 51), // COLOR_ORANGE new Color(178, 76, 216), // COLOR_MAGENTA new Color(102, 153, 216), // COLOR_LIGHT_BLUE new Color(229, 229, 51), // COLOR_YELLOW new Color(127, 204, 25), // COLOR_LIGHT_GREEN new Color(242, 127, 165), // COLOR_PINK new Color(76, 76, 76), // COLOR_GREY new Color(153, 153, 153), // COLOR_LIGHT_GREY new Color(76, 127, 153), // COLOR_CYAN new Color(127, 63, 178), // COLOR_PURPLE new Color(51, 76, 178), // COLOR_BLUE new Color(102, 76, 51), // COLOR_BROWN new Color(102, 127, 51), // COLOR_GREEN new Color(153, 51, 51), // COLOR_RED new Color(25, 25, 25), // COLOR_BLACK new Color(250, 238, 77), // GOLD new Color(92, 219, 213), // Diamond new Color(74, 128, 255), // lapis new Color(0, 217, 58), // emerald new Color(129, 86, 49), // podzol new Color(112, 2, 0), // NETHER //new 1.8 colors new Color(209, 177, 161), // TERRACORA_WHITE new Color(159, 82, 36), // TERRACORA_ORANGE new Color(149, 87, 108), // terracota magenta new Color(112, 108, 138), // terracota_light_blue new Color(186, 133, 36), // terracota_yellow new Color(103, 117, 53), // terracota_light_green new Color(160, 77, 78), // terracota_pink new Color(57, 41, 35), // terracota_grey new Color(135, 107, 98), // terracota_light_grey new Color(87, 92, 92), // terracota_cyan new Color(122, 73, 88), // terracota_purple new Color(76, 62, 92), // terracota_blue new Color(76, 50, 35), // terracota_brown new Color(76, 50, 35), // terracota_green new Color(142, 60, 46), // terracota_red new Color(37, 22, 16), // terracota_black //1.16 new Color(189, 48, 49), // CRIMson_nylium new Color(148, 63, 97), // crimson_stem new Color(92, 25, 29), // crimson_hyphae new Color(22, 126, 134), // warped_nylium new Color(58, 142, 140), // warped_stem new Color(86, 44, 62), // warped_hyphae new Color(20, 180, 133), // warped_wart_block // cliffs update (1.18 / 1.19) new Color(100, 100, 100), // deepslate new Color(216, 175, 147), // raw iron new Color(127, 167, 150), // glow lichen }; MAP_COLORS = new Color[BaseMapColors.length * 4]; for (int i = 0; i < BaseMapColors.length; ++i) { Color bc = BaseMapColors[i]; MAP_COLORS[i * 4] = new Color((int) (bc.getRed() * 180.0 / 255.0 + 0.5), (int) (bc.getGreen() * 180.0 / 255.0 + 0.5), (int) (bc.getBlue() * 180.0 / 255.0 + 0.5), bc.getAlpha()); MAP_COLORS[i * 4 + 1] = new Color((int) (bc.getRed() * 220.0 / 255.0 + 0.5), (int) (bc.getGreen() * 220.0 / 255.0 + 0.5), (int) (bc.getBlue() * 220.0 / 255.0 + 0.5), bc.getAlpha()); MAP_COLORS[i * 4 + 2] = bc; MAP_COLORS[i * 4 + 3] = new Color((int) (bc.getRed() * 135.0 / 255.0 + 0.5), (int) (bc.getGreen() * 135.0 / 255.0 + 0.5), (int) (bc.getBlue() * 135.0 / 255.0 + 0.5), bc.getAlpha()); } } public static byte getColor(Color color) { byte cached = matchedColors.getOrDefault(color, -1).byteValue(); if (cached != -1) { return cached; } int best = 0; int bestDistance = Integer.MAX_VALUE; for (int i = 0; i < MAP_COLORS.length; ++i) { int distance = COMPARATOR.compare(color, MAP_COLORS[i]); if (distance < bestDistance) { bestDistance = distance; best = i; } } matchedColors.put(color, best); return (byte) best; } }