Skip to content

Commit

Permalink
use byte for colorquad
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperGeurtz committed Oct 13, 2022
1 parent 4677f15 commit 3fd3330
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
34 changes: 17 additions & 17 deletions src/main/java/bwapi/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESER
};

private static boolean rgbInitialized;
private static final int[][][] closestColor = new int[64][64][64];
private static final byte[][][] closestColor = new byte[64][64][64];

public final int id;

Expand Down Expand Up @@ -151,9 +151,9 @@ private static int getBestIdFor(final int red, final int green, final int blue)
if (p.rgbReserved != 0) {
continue;
}
final int r = red - p.rgbRed;
final int g = green - p.rgbGreen;
final int b = blue - p.rgbBlue;
final int r = red - (p.rgbRed & 0xFF);
final int g = green - (p.rgbGreen & 0xFF);
final int b = blue - (p.rgbBlue & 0xFF);

final int distance = r * r + g * g + b * b;
if (distance < min_dist) {
Expand All @@ -173,24 +173,24 @@ private static int getRGBIndex(final int red, final int green, final int blue) {
for (int r = 0; r < 64; ++r) {
for (int g = 0; g < 64; ++g) {
for (int b = 0; b < 64; ++b) {
closestColor[r][g][b] = getBestIdFor(r << 2, g << 2, b << 2);
closestColor[r][g][b] = (byte) getBestIdFor(r << 2, g << 2, b << 2);
}
}
}
}
return closestColor[red >> 2][green >> 2][blue >> 2];
return closestColor[red >> 2][green >> 2][blue >> 2] & 0xFF;
}

public int red() {
return id < 256 ? defaultPalette[id].rgbRed : 0;
return id < 256 ? defaultPalette[id].rgbRed & 0xFF : 0;
}

public int green() {
return id < 256 ? defaultPalette[id].rgbGreen : 0;
return id < 256 ? defaultPalette[id].rgbGreen & 0xFF : 0;
}

public int blue() {
return id < 256 ? defaultPalette[id].rgbBlue : 0;
return id < 256 ? defaultPalette[id].rgbBlue & 0xFF : 0;
}

@Override
Expand Down Expand Up @@ -219,20 +219,20 @@ public String toString() {

/// BROODWAR COLOR IMPLEMENTATION
private static class RGBQUAD {
final int rgbRed;
final int rgbGreen;
final int rgbBlue;
final int rgbReserved;
final byte rgbRed;
final byte rgbGreen;
final byte rgbBlue;
final byte rgbReserved;

RGBQUAD(int rgbRed, int rgbGreen, int rgbBlue) {
this(rgbRed, rgbGreen, rgbBlue, 0);
}

RGBQUAD(int rgbRed, int rgbGreen, int rgbBlue, int rgbReserved) {
this.rgbRed = rgbRed;
this.rgbGreen = rgbGreen;
this.rgbBlue = rgbBlue;
this.rgbReserved = rgbReserved;
this.rgbRed = (byte)rgbRed;
this.rgbGreen = (byte) rgbGreen;
this.rgbBlue = (byte)rgbBlue;
this.rgbReserved = (byte)rgbReserved;
}
}
}
56 changes: 50 additions & 6 deletions src/test/java/bwapi/ColorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertArrayEquals;
public class ColorTest {

private static int[][] colors = {
private static final int[][] COLORS_ID_TO_RGB = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
Expand Down Expand Up @@ -265,14 +265,58 @@ public class ColorTest {
{255, 255, 255}
};

// Amount of colors expected per ID for all colors generated by
// R=0..256, G=0..256, B=0..256 . If the Color::getBestIdFor is not
// correctly implemented, this will probably mismatch.
static final int[] COLORS_RGB_TO_ID_BUCKETS = {
1152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28608, 13184,
14592, 30464, 17408, 33344, 13056, 57344, 27200, 16512, 47808,
65152, 42176, 38080, 54784, 75584, 60352, 70016, 59200, 89792,
36992, 42880, 50944, 72320, 2688, 16960, 27968, 24000, 21952,
55104, 36992, 35392, 51712, 118016, 64320, 330624, 391424, 279040,
70912, 133056, 128000, 134144, 77376, 18368, 31104, 56960, 53184,
32000, 3584, 5312, 8448, 11712, 9728, 12416, 15168, 12608, 22720,
20736, 26240, 47040, 31168, 54080, 41536, 60288, 58496, 97792,
104896, 93312, 124352, 81152, 6976, 10048, 9920, 14592, 44224,
8640, 24256, 6016, 13696, 38592, 22592, 73408, 41728, 15360,
117696, 88704, 75776, 98752, 65216, 35840, 114304, 38912, 206464,
179008, 80640, 25984, 94464, 268160, 255552, 58048, 80448, 381376,
9856, 16576, 42304, 48640, 100736, 247808, 183040, 79616, 165120,
47296, 227712, 85312, 55424, 67136, 108864, 135808, 194304, 35328,
31680, 21632, 3072, 5120, 7040, 7936, 11392, 18048, 9984, 25536,
19328, 38208, 31104, 36992, 65728, 81408, 108800, 139456, 304512,
387648, 75264, 127360, 244608, 508160, 57344, 142464, 71936,
294848, 1439616, 181632, 392832, 230848, 1664, 3264, 2304, 7552,
24448, 38912, 128384, 281920, 53248, 84736, 42304, 65664, 42560,
3072, 12672, 31744, 26624, 80000, 308480, 243328, 305792, 218752,
220032, 253568, 11840, 31424, 45888, 62016, 82368, 70912, 100352,
130560, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

@Test
public void checkAllColors() {
public void checkAllColorsIdToRGB() {
for (int id=0; id < 256; id++) {
Color c = new Color(id);
assertEquals(colors[id][0], c.red());
assertEquals(colors[id][1], c.green());
assertEquals(colors[id][2], c.blue());
assertEquals(COLORS_ID_TO_RGB[id][0], c.red());
assertEquals(COLORS_ID_TO_RGB[id][1], c.green());
assertEquals(COLORS_ID_TO_RGB[id][2], c.blue());
}
}

@Test
public void checkAllColorsRGBToId() {
final int[] buckets = new int[256];
for (int r=0; r < 256; r++) {
for (int g=0; g < 256; g++) {
for (int b=0; b < 256; b++) {
Color c = new Color(r, g, b);
buckets[c.id] += 1;
}
}
}
assertArrayEquals(COLORS_RGB_TO_ID_BUCKETS, buckets);
}

@Test
Expand Down

0 comments on commit 3fd3330

Please sign in to comment.