Skip to content

Commit

Permalink
Update closest color conversion (#1057)
Browse files Browse the repository at this point in the history
ViaVersion altered their color conversion to fix an issue and this just copies those changes
  • Loading branch information
rtm516 authored Jul 30, 2020
1 parent 427cb69 commit 50346a9
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,16 @@ private static String getClosestColor(String color) {
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;
}
}

Expand Down

0 comments on commit 50346a9

Please sign in to comment.