Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.map;
2
3import java.awt.*;
4import java.awt.image.BufferedImage;
5
6/**
7 * Represents the palette that map items use.
8 */
9public final class MapPalette {
10
11 // Internal mechanisms
12
13 private MapPalette() {
14 }
15
16 private static Color c(int r, int g, int b) {
17 return new Color(r, g, b);
18 }
19
20 private static double getDistance(Color c1, Color c2) {
21 double rmean = (c1.getRed() + c2.getRed()) / 2.0;
22 double r = c1.getRed() - c2.getRed();
23 double g = c1.getGreen() - c2.getGreen();
24 int b = c1.getBlue() - c2.getBlue();
25 double weightR = 2 + rmean / 256.0;
26 double weightG = 4.0;
27 double weightB = 2 + (255 - rmean) / 256.0;
28 return weightR * r * r + weightG * g * g + weightB * b * b;
29 }
30
31 private static final Color[] colors = { new Color(0, 0, 0, 0), new Color(0, 0, 0, 0), new Color(0, 0, 0, 0), new Color(0, 0, 0, 0), c(89, 125, 39), c(109, 153, 48), c(27, 178, 56), c(109, 153, 48), c(174, 164, 115), c(213, 201, 140), c(247, 233, 163), c(213, 201, 140), c(117, 117, 117), c(144, 144, 144), c(167, 167, 167), c(144, 144, 144), c(180, 0, 0), c(220, 0, 0), c(255, 0, 0), c(220, 0, 0), c(112, 112, 180), c(138, 138, 220), c(160, 160, 255), c(138, 138, 220), c(117, 117, 117), c(144, 144, 144), c(167, 167, 167), c(144, 144, 144), c(0, 87, 0), c(0, 106, 0), c(0, 124, 0), c(0, 106, 0), c(180, 180, 180), c(220, 220, 220), c(255, 255, 255), c(220, 220, 220), c(115, 118, 129), c(141, 144, 158), c(164, 168, 184), c(141, 144, 158), c(129, 74, 33), c(157, 91, 40), c(183, 106, 47), c(157, 91, 40), c(79, 79, 79), c(96, 96, 96), c(112, 112, 112), c(96, 96, 96), c(45, 45, 180), c(55, 55, 220), c(64, 64, 255), c(55, 55, 220), c(73, 58, 35), c(89, 71, 43), c(104, 83, 50), c(89, 71, 43) };
32
33 // Interface
34
35 /**
36 * The base color ranges. Each entry corresponds to four colors of varying
37 * shades with values entry to entry + 3.
38 */
39 public static final byte TRANSPARENT = 0;
40 public static final byte LIGHT_GREEN = 4;
41 public static final byte LIGHT_BROWN = 8;
42 public static final byte GRAY_1 = 12;
43 public static final byte RED = 16;
44 public static final byte PALE_BLUE = 20;
45 public static final byte GRAY_2 = 24;
46 public static final byte DARK_GREEN = 28;
47 public static final byte WHITE = 32;
48 public static final byte LIGHT_GRAY = 36;
49 public static final byte BROWN = 40;
50 public static final byte DARK_GRAY = 44;
51 public static final byte BLUE = 48;
52 public static final byte DARK_BROWN = 52;
53
54 /**
55 * Resize an image to 128x128.
56 *
57 * @param image The image to resize.
58 * @return The resized image.
59 */
60 public static BufferedImage resizeImage(Image image) {
61 BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
62 Graphics2D graphics = result.createGraphics();
63 graphics.drawImage(image, 0, 0, 128, 128, null);
64 graphics.dispose();
65 return result;
66 }
67
68 /**
69 * Convert an Image to a byte[] using the palette.
70 *
71 * @param image The image to convert.
72 * @return A byte[] containing the pixels of the image.
73 */
74 public static byte[] imageToBytes(Image image) {
75 BufferedImage temp = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
76 Graphics2D graphics = temp.createGraphics();
77 graphics.drawImage(image, 0, 0, null);
78 graphics.dispose();
79
80 int[] pixels = new int[temp.getWidth() * temp.getHeight()];
81 temp.getRGB(0, 0, temp.getWidth(), temp.getHeight(), pixels, 0, temp.getWidth());
82
83 byte[] result = new byte[temp.getWidth() * temp.getHeight()];
84 for (int i = 0; i < pixels.length; i++) {
85 result[i] = matchColor(new Color(pixels[i]));
86 }
87 return result;
88 }
89
90 /**
91 * Get the index of the closest matching color in the palette to the given color.
92 *
93 * @param r The red component of the color.
94 * @param b The blue component of the color.
95 * @param g The green component of the color.
96 * @return The index in the palette.
97 */
98 public static byte matchColor(int r, int g, int b) {
99 return matchColor(new Color(r, g, b));
100 }
101
102 /**
103 * Get the index of the closest matching color in the palette to the given color.
104 *
105 * @param color The Color to match.
106 * @return The index in the palette.
107 */
108 public static byte matchColor(Color color) {
109 if (color.getAlpha() < 128) return 0;
110
111 int index = 0;
112 double best = -1;
113
114 for (int i = 4; i < colors.length; i++) {
115 double distance = getDistance(color, colors[i]);
116 if (distance < best || best == -1) {
117 best = distance;
118 index = i;
119 }
120 }
121
122 return (byte) index;
123 }
124
125 /**
126 * Get the value of the given color in the palette.
127 *
128 * @param index The index in the palette.
129 * @return The Color of the palette entry.
130 */
131 public static Color getColor(byte index) {
132 if (index < 0 || index >= colors.length) {
133 throw new IndexOutOfBoundsException();
134 } else {
135 return colors[index];
136 }
137 }
138
139}