Skip to content

Commit 3fd3330

Browse files
committed
use byte for colorquad
1 parent 4677f15 commit 3fd3330

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

src/main/java/bwapi/Color.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESER
115115
};
116116

117117
private static boolean rgbInitialized;
118-
private static final int[][][] closestColor = new int[64][64][64];
118+
private static final byte[][][] closestColor = new byte[64][64][64];
119119

120120
public final int id;
121121

@@ -151,9 +151,9 @@ private static int getBestIdFor(final int red, final int green, final int blue)
151151
if (p.rgbReserved != 0) {
152152
continue;
153153
}
154-
final int r = red - p.rgbRed;
155-
final int g = green - p.rgbGreen;
156-
final int b = blue - p.rgbBlue;
154+
final int r = red - (p.rgbRed & 0xFF);
155+
final int g = green - (p.rgbGreen & 0xFF);
156+
final int b = blue - (p.rgbBlue & 0xFF);
157157

158158
final int distance = r * r + g * g + b * b;
159159
if (distance < min_dist) {
@@ -173,24 +173,24 @@ private static int getRGBIndex(final int red, final int green, final int blue) {
173173
for (int r = 0; r < 64; ++r) {
174174
for (int g = 0; g < 64; ++g) {
175175
for (int b = 0; b < 64; ++b) {
176-
closestColor[r][g][b] = getBestIdFor(r << 2, g << 2, b << 2);
176+
closestColor[r][g][b] = (byte) getBestIdFor(r << 2, g << 2, b << 2);
177177
}
178178
}
179179
}
180180
}
181-
return closestColor[red >> 2][green >> 2][blue >> 2];
181+
return closestColor[red >> 2][green >> 2][blue >> 2] & 0xFF;
182182
}
183183

184184
public int red() {
185-
return id < 256 ? defaultPalette[id].rgbRed : 0;
185+
return id < 256 ? defaultPalette[id].rgbRed & 0xFF : 0;
186186
}
187187

188188
public int green() {
189-
return id < 256 ? defaultPalette[id].rgbGreen : 0;
189+
return id < 256 ? defaultPalette[id].rgbGreen & 0xFF : 0;
190190
}
191191

192192
public int blue() {
193-
return id < 256 ? defaultPalette[id].rgbBlue : 0;
193+
return id < 256 ? defaultPalette[id].rgbBlue & 0xFF : 0;
194194
}
195195

196196
@Override
@@ -219,20 +219,20 @@ public String toString() {
219219

220220
/// BROODWAR COLOR IMPLEMENTATION
221221
private static class RGBQUAD {
222-
final int rgbRed;
223-
final int rgbGreen;
224-
final int rgbBlue;
225-
final int rgbReserved;
222+
final byte rgbRed;
223+
final byte rgbGreen;
224+
final byte rgbBlue;
225+
final byte rgbReserved;
226226

227227
RGBQUAD(int rgbRed, int rgbGreen, int rgbBlue) {
228228
this(rgbRed, rgbGreen, rgbBlue, 0);
229229
}
230230

231231
RGBQUAD(int rgbRed, int rgbGreen, int rgbBlue, int rgbReserved) {
232-
this.rgbRed = rgbRed;
233-
this.rgbGreen = rgbGreen;
234-
this.rgbBlue = rgbBlue;
235-
this.rgbReserved = rgbReserved;
232+
this.rgbRed = (byte)rgbRed;
233+
this.rgbGreen = (byte) rgbGreen;
234+
this.rgbBlue = (byte)rgbBlue;
235+
this.rgbReserved = (byte)rgbReserved;
236236
}
237237
}
238238
}

src/test/java/bwapi/ColorTest.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import org.junit.Test;
44

55
import static org.junit.Assert.assertEquals;
6-
6+
import static org.junit.Assert.assertArrayEquals;
77
public class ColorTest {
88

9-
private static int[][] colors = {
9+
private static final int[][] COLORS_ID_TO_RGB = {
1010
{0, 0, 0},
1111
{0, 0, 0},
1212
{0, 0, 0},
@@ -265,14 +265,58 @@ public class ColorTest {
265265
{255, 255, 255}
266266
};
267267

268+
// Amount of colors expected per ID for all colors generated by
269+
// R=0..256, G=0..256, B=0..256 . If the Color::getBestIdFor is not
270+
// correctly implemented, this will probably mismatch.
271+
static final int[] COLORS_RGB_TO_ID_BUCKETS = {
272+
1152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28608, 13184,
273+
14592, 30464, 17408, 33344, 13056, 57344, 27200, 16512, 47808,
274+
65152, 42176, 38080, 54784, 75584, 60352, 70016, 59200, 89792,
275+
36992, 42880, 50944, 72320, 2688, 16960, 27968, 24000, 21952,
276+
55104, 36992, 35392, 51712, 118016, 64320, 330624, 391424, 279040,
277+
70912, 133056, 128000, 134144, 77376, 18368, 31104, 56960, 53184,
278+
32000, 3584, 5312, 8448, 11712, 9728, 12416, 15168, 12608, 22720,
279+
20736, 26240, 47040, 31168, 54080, 41536, 60288, 58496, 97792,
280+
104896, 93312, 124352, 81152, 6976, 10048, 9920, 14592, 44224,
281+
8640, 24256, 6016, 13696, 38592, 22592, 73408, 41728, 15360,
282+
117696, 88704, 75776, 98752, 65216, 35840, 114304, 38912, 206464,
283+
179008, 80640, 25984, 94464, 268160, 255552, 58048, 80448, 381376,
284+
9856, 16576, 42304, 48640, 100736, 247808, 183040, 79616, 165120,
285+
47296, 227712, 85312, 55424, 67136, 108864, 135808, 194304, 35328,
286+
31680, 21632, 3072, 5120, 7040, 7936, 11392, 18048, 9984, 25536,
287+
19328, 38208, 31104, 36992, 65728, 81408, 108800, 139456, 304512,
288+
387648, 75264, 127360, 244608, 508160, 57344, 142464, 71936,
289+
294848, 1439616, 181632, 392832, 230848, 1664, 3264, 2304, 7552,
290+
24448, 38912, 128384, 281920, 53248, 84736, 42304, 65664, 42560,
291+
3072, 12672, 31744, 26624, 80000, 308480, 243328, 305792, 218752,
292+
220032, 253568, 11840, 31424, 45888, 62016, 82368, 70912, 100352,
293+
130560, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
294+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
295+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
296+
};
297+
268298
@Test
269-
public void checkAllColors() {
299+
public void checkAllColorsIdToRGB() {
270300
for (int id=0; id < 256; id++) {
271301
Color c = new Color(id);
272-
assertEquals(colors[id][0], c.red());
273-
assertEquals(colors[id][1], c.green());
274-
assertEquals(colors[id][2], c.blue());
302+
assertEquals(COLORS_ID_TO_RGB[id][0], c.red());
303+
assertEquals(COLORS_ID_TO_RGB[id][1], c.green());
304+
assertEquals(COLORS_ID_TO_RGB[id][2], c.blue());
305+
}
306+
}
307+
308+
@Test
309+
public void checkAllColorsRGBToId() {
310+
final int[] buckets = new int[256];
311+
for (int r=0; r < 256; r++) {
312+
for (int g=0; g < 256; g++) {
313+
for (int b=0; b < 256; b++) {
314+
Color c = new Color(r, g, b);
315+
buckets[c.id] += 1;
316+
}
317+
}
275318
}
319+
assertArrayEquals(COLORS_RGB_TO_ID_BUCKETS, buckets);
276320
}
277321

278322
@Test

0 commit comments

Comments
 (0)