Skip to content

Commit 790ae83

Browse files
committed
Make color constructor set to 255 if invalid id is given
1 parent 3fd3330 commit 790ae83

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/main/java/bwapi/Color.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ public final class Color {
114114
RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, RGBRESERVE, new RGBQUAD(255, 255, 255)
115115
};
116116

117-
private static boolean rgbInitialized;
118-
private static final byte[][][] closestColor = new byte[64][64][64];
117+
private static byte[][][] closestColor = null;
119118

120119
public final int id;
121120

@@ -139,7 +138,10 @@ public Color(final int red, final int green, final int blue) {
139138
* @param id The index of the color in the 256-color palette.
140139
*/
141140
public Color(final int id) {
142-
this.id = id;
141+
// The id is set to 255 if the id is invalid, as in the official BWAPI sources:
142+
// https://github.com/bwapi/bwapi/blob/3438abd8e0222f37934ba62b2130c3933b067678/bwapi/include/BWAPI/Color.h#L13
143+
// https://github.com/bwapi/bwapi/blob/3438abd8e0222f37934ba62b2130c3933b067678/bwapi/include/BWAPI/Type.h#L66
144+
this.id = id < 0 || id > 255 ? 255 : id;
143145
}
144146

145147
private static int getBestIdFor(final int red, final int green, final int blue) {
@@ -168,8 +170,8 @@ private static int getBestIdFor(final int red, final int green, final int blue)
168170
}
169171

170172
private static int getRGBIndex(final int red, final int green, final int blue) {
171-
if (!rgbInitialized) {
172-
rgbInitialized = true;
173+
if (closestColor == null) {
174+
closestColor = new byte[64][64][64];
173175
for (int r = 0; r < 64; ++r) {
174176
for (int g = 0; g < 64; ++g) {
175177
for (int b = 0; b < 64; ++b) {
@@ -182,15 +184,15 @@ private static int getRGBIndex(final int red, final int green, final int blue) {
182184
}
183185

184186
public int red() {
185-
return id < 256 ? defaultPalette[id].rgbRed & 0xFF : 0;
187+
return defaultPalette[id].rgbRed & 0xFF;
186188
}
187189

188190
public int green() {
189-
return id < 256 ? defaultPalette[id].rgbGreen & 0xFF : 0;
191+
return defaultPalette[id].rgbGreen & 0xFF;
190192
}
191193

192194
public int blue() {
193-
return id < 256 ? defaultPalette[id].rgbBlue & 0xFF : 0;
195+
return defaultPalette[id].rgbBlue & 0xFF;
194196
}
195197

196198
@Override

src/main/java/bwapi/Player.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Player implements Comparable<Player> {
2525
private final String name;
2626
private final PlayerType playerType;
2727
private final TilePosition startLocation;
28-
28+
private final Color color;
2929
private PlayerSelf self = null;
3030
PlayerSelf self() {
3131
if (self == null) {
@@ -41,6 +41,7 @@ PlayerSelf self() {
4141
this.name = playerData.getName();
4242
this.playerType = PlayerType.idToEnum[playerData.getType()];
4343
this.startLocation = new TilePosition(playerData.getStartLocationX(), playerData.getStartLocationY());
44+
this.color = new Color(playerData.getColor());
4445
}
4546

4647
/**
@@ -533,7 +534,7 @@ public boolean isUpgrading(final UpgradeType upgrade) {
533534
* @return {@link Color} object that represents the color of the current player.
534535
*/
535536
public Color getColor() {
536-
return new Color(playerData.getColor());
537+
return color;
537538
}
538539

539540
/**
@@ -543,7 +544,7 @@ public Color getColor() {
543544
* @return character code to use for text in Broodwar.
544545
*/
545546
public Text getTextColor() {
546-
switch (playerData.getColor()) {
547+
switch (color.id) {
547548
case 111: // red
548549
return Text.BrightRed;
549550
case 165: // blue

src/test/java/bwapi/ColorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ public void checkAllColorsRGBToId() {
319319
assertArrayEquals(COLORS_RGB_TO_ID_BUCKETS, buckets);
320320
}
321321

322+
@Test
323+
public void checkColorWithInvalidId() {
324+
assertEquals(255, new Color(-1).id);
325+
assertEquals(255, new Color(256).id);
326+
}
327+
322328
@Test
323329
public void toStringTest() {
324330
assertEquals("Color.Black", Color.Black.toString());

src/test/java/game/DrawTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void onFrame() {
2929
game.drawBoxScreen(200, 110, 350, 210, Color.Purple, true);
3030
game.drawTextScreen(50, 50, "%cHello %cWorld!", Text.Red, Text.Green);
3131

32+
Player self = game.self();
33+
Player enemy = game.enemy();
34+
String out = "%c"+ self.getName() + " (" + self.getRace() + ")%c vs %c" + enemy.getName() + " (" + enemy.getRace() + ")";
35+
game.drawTextScreen(20, 20, out, self.getTextColor(), Text.Default, enemy.getTextColor());
3236
}
3337

3438
public static void main(String[] args) {

0 commit comments

Comments
 (0)