Skip to content

Commit c710b66

Browse files
authored
Add MapPalette.getNearestColor (#13104)
1 parent c33a9ce commit c710b66

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

paper-api/src/main/java/org/bukkit/map/MapPalette.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Represents the palette that map items use.
1313
* <p>
14-
* These fields are hee base color ranges. Each entry corresponds to four
14+
* These fields are the base color ranges. Each entry corresponds to four
1515
* colors of varying shades with values entry to entry + 3.
1616
*/
1717
public final class MapPalette {
@@ -27,7 +27,7 @@ private static double getDistance(@NotNull Color c1, @NotNull Color c2) {
2727
// All weights are 512x their original to avoid floating point division
2828
int weightR = 1024 + rsum;
2929
int weightG = 2048;
30-
int weightB = 1024 + (255*2 - rsum);
30+
int weightB = 1024 + (255 * 2 - rsum);
3131

3232
// Division by 256 here is unnecessary as this won't change the result of the sort
3333
return weightR * r * r + weightG * g * g + weightB * b * b;
@@ -460,6 +460,18 @@ public static Color getColor(byte index) {
460460
return colors[index >= 0 ? index : index + 256];
461461
}
462462

463+
/**
464+
* Gets the closest {@link Color} in the map palette.
465+
*
466+
* @param color to match against the map palette
467+
* @return the nearest {@link Color} from the map palette
468+
*/
469+
@NotNull
470+
public static Color getNearestColor(@NotNull Color color) {
471+
byte b = matchColor(color);
472+
return getColor(b);
473+
}
474+
463475
private static MapColorCache mapColorCache;
464476

465477
/**

0 commit comments

Comments
 (0)