From 50346a95cdf50d50e197d81c53b73764093abaa7 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Thu, 30 Jul 2020 21:09:53 +0100 Subject: [PATCH] Update closest color conversion (#1057) ViaVersion altered their color conversion to fix an issue and this just copies those changes --- .../geysermc/connector/utils/MessageUtils.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java index 21a5fa51..0b495895 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -400,13 +400,16 @@ public class MessageUtils { int testB = testColor.getValue() & 0xFF; // Check by the greatest diff of the 3 values - int rDiff = Math.abs(testR - r); - int gDiff = Math.abs(testG - g); - int bDiff = Math.abs(testB - b); - int maxDiff = Math.max(Math.max(rDiff, gDiff), bDiff); - if (closest == null || maxDiff < smallestDiff) { + int rAverage = (testR + r) / 2; + int rDiff = testR - r; + int gDiff = testG - g; + int bDiff = testB - b; + int diff = ((2 + (rAverage >> 8)) * rDiff * rDiff) + + (4 * gDiff * gDiff) + + ((2 + ((255 - rAverage) >> 8)) * bDiff * bDiff); + if (closest == null || diff < smallestDiff) { closest = testColor.getKey(); - smallestDiff = maxDiff; + smallestDiff = diff; } }