From 58182ce5cf598459877cd032ffaf6d24a683f1a0 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Tue, 28 May 2024 21:27:35 -0700 Subject: [PATCH] waypoint color converter --- .../java/com/github/rfresh2/ColorHelper.java | 55 +++++++++++++++++++ .../rfresh2/JourneyMapWaypointsToXaero.java | 6 +- .../github/rfresh2/model/XaeroWaypoint.java | 3 - 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/rfresh2/ColorHelper.java diff --git a/src/main/java/com/github/rfresh2/ColorHelper.java b/src/main/java/com/github/rfresh2/ColorHelper.java new file mode 100644 index 0000000..94d3d6b --- /dev/null +++ b/src/main/java/com/github/rfresh2/ColorHelper.java @@ -0,0 +1,55 @@ +package com.github.rfresh2; + +import java.awt.*; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ColorHelper { + /** + * Color indexes: + * 0 = black + * 1 = dark blue + * 2 = dark green + * 3 = dark aqua + * 4 = dark red + * 5 = dark purple + * 6 = gold + * 7 = gray + * 8 = dark gray + * 9 = blue + * 10 = green + * 11 = aqua + * 12 = red + * 13 = light purple + * 14 = yellow + * 15 = white + */ + public static List xaeroColors = Stream.of(-16777216, -16777046, -16733696, -16733526, -5636096, -5635926, -22016, -5592406, -11184811, -11184641, -11141291, -11141121, -65536, -43521, -171, -1) + .map(Color::new) + .collect(Collectors.toList()); + + public static int nearestXaeroColorIndex(Color jmColor) { + int minIndex = 0; + double min = Double.MAX_VALUE; + for (int i = 0; i < xaeroColors.size(); i++) { + Color xaeroColor = xaeroColors.get(i); + double d = colorDistance(xaeroColor, jmColor); + if (d < min) { + minIndex = i; + min = d; + } + } + return minIndex; + } + + public static double colorDistance(Color c1, Color c2) { + int red1 = c1.getRed(); + int red2 = c2.getRed(); + int rmean = (red1 + red2) >> 1; + int r = red1 - red2; + int g = c1.getGreen() - c2.getGreen(); + int b = c1.getBlue() - c2.getBlue(); + return Math.sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8)); + } +} diff --git a/src/main/java/com/github/rfresh2/JourneyMapWaypointsToXaero.java b/src/main/java/com/github/rfresh2/JourneyMapWaypointsToXaero.java index b2e70ad..f23cc1a 100644 --- a/src/main/java/com/github/rfresh2/JourneyMapWaypointsToXaero.java +++ b/src/main/java/com/github/rfresh2/JourneyMapWaypointsToXaero.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.awt.*; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -22,7 +23,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.stream.Collectors; public class JourneyMapWaypointsToXaero { @@ -37,7 +37,6 @@ public class JourneyMapWaypointsToXaero { // we want to try modern, and if it fails, try legacy objectMapper.coercionConfigFor(LogicalType.Textual).setCoercion(CoercionInputShape.Integer, CoercionAction.Fail); } - static final Random random = new Random(); static final Logger LOG = LoggerFactory.getLogger("JMWaypointsToXaero"); public static void main(final String[] args) { @@ -47,7 +46,6 @@ public static void main(final String[] args) { } String input = args[0]; String output = args[1]; - Path folderIn = new File(String.format("%s/waypoints/", input)).toPath(); LOG.info("Reading JM waypoints from path: {}", folderIn.toAbsolutePath()); List xaeroWaypoints = convertWaypoints(folderIn); @@ -128,7 +126,7 @@ public static XaeroWaypoint convertWaypoint(final IJMWaypoint jmWaypoint) { (dimension == -1 ? jmWaypoint.getX() / 8 : jmWaypoint.getX()), // jm stores all wp in ow coords jmWaypoint.getY(), (dimension == -1 ? jmWaypoint.getZ() / 8 : jmWaypoint.getZ()), - random.nextInt(16), // todo: convert JM rgb to some equivalent Xaero color + ColorHelper.nearestXaeroColorIndex(new Color(jmWaypoint.getR(), jmWaypoint.getG(), jmWaypoint.getB())), !jmWaypoint.isEnabled(), 0, "gui.xaero_default", diff --git a/src/main/java/com/github/rfresh2/model/XaeroWaypoint.java b/src/main/java/com/github/rfresh2/model/XaeroWaypoint.java index 0ace894..80bb4aa 100644 --- a/src/main/java/com/github/rfresh2/model/XaeroWaypoint.java +++ b/src/main/java/com/github/rfresh2/model/XaeroWaypoint.java @@ -10,9 +10,6 @@ public class XaeroWaypoint { public int x; public int y; public int z; - /** - * Color index - */ public int color; public boolean disabled; public int type;