Skip to content

Commit 50364db

Browse files
committed
color toString & simplify text formatting code
1 parent 6b1d24e commit 50364db

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

src/main/java/bwapi/Color.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bwapi;
22

33

4+
import java.util.HashMap;
5+
import java.util.Map;
46
import java.util.Objects;
57

68
/**
@@ -59,6 +61,22 @@ public class Color {
5961
*/
6062
public final static Color Grey = new Color(74);
6163

64+
private final static Map<Integer, String> defaultColors = new HashMap<>();
65+
static {
66+
defaultColors.put(Color.Red.id, "Red");
67+
defaultColors.put(Color.Blue.id, "Blue");
68+
defaultColors.put(Color.Teal.id, "Teal");
69+
defaultColors.put(Color.Purple.id, "Purple");
70+
defaultColors.put(Color.Orange.id, "Orange");
71+
defaultColors.put(Color.Brown.id, "Brown");
72+
defaultColors.put(Color.White.id, "White");
73+
defaultColors.put(Color.Yellow.id, "Yellow");
74+
defaultColors.put(Color.Green.id, "Green");
75+
defaultColors.put(Color.Cyan.id, "Cyan");
76+
defaultColors.put(Color.Black.id, "Black");
77+
defaultColors.put(Color.Grey.id, "Grey");
78+
}
79+
6280
private static final RGBQUAD RGBRESERVE = new RGBQUAD(0, 0, 0, 0xFF);
6381

6482
private static final RGBQUAD[] defaultPalette = {
@@ -188,6 +206,17 @@ public int hashCode() {
188206
return Objects.hash(id);
189207
}
190208

209+
@Override
210+
public String toString() {
211+
if (defaultColors.containsKey(id)) {
212+
return "Color." + defaultColors.get(id);
213+
}
214+
return "Color{" +
215+
"red=" + red() +
216+
", green=" + green() +
217+
", blue=" + blue() + "}";
218+
}
219+
191220
/// BROODWAR COLOR IMPLEMENTATION
192221
private static class RGBQUAD {
193222
final int rgbRed;

src/main/java/bwapi/Game.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ public List<TilePosition> getStartLocations() {
14991499
return startLocations;
15001500
}
15011501

1502-
private String formatString(final String string, final Text... colors) {
1502+
static String formatString(final String string, final Text... colors) {
15031503
return colors.length > 0 ? String.format(string, Arrays.stream(colors).map(c -> c.id).toArray()) : string;
15041504
}
15051505

src/main/java/bwapi/Text.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ public enum Text {
125125
* Format text with a textcolor to display on broodwar
126126
*/
127127
public static String formatText(final String text, final Text format) {
128-
final byte[] data = text.getBytes();
129-
final int len = text.length();
130-
final byte[] formatted = new byte[len + 1];
131-
formatted[0] = format.id;
132-
System.arraycopy(data, 0, formatted, 1, len);
133-
return new String(formatted);
128+
return Game.formatString("%c" + text, format);
134129
}
135130

136131
/**

src/test/java/bwapi/ColorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,12 @@ public void checkAllColors() {
274274
assertEquals(colors[id][2], c.blue());
275275
}
276276
}
277+
278+
@Test
279+
public void toStringTest() {
280+
assertEquals("Color.Black", Color.Black.toString());
281+
assertEquals("Color.Black", new Color(0, 0, 0).toString());
282+
assertEquals("Color.Black", new Color(Color.Black.id).toString());
283+
assertEquals("Color{red=16, green=16, blue=16}", new Color(16, 16, 16).toString());
284+
}
277285
}

src/test/java/bwapi/TextTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bwapi;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class TextTest {
8+
9+
@Test
10+
public void textFormattingTest() {
11+
assertEquals("\u0006Red", Text.formatText("Red", Text.Red));
12+
assertEquals("\u0006Red and \u000EBlue then \u0002Default", Game.formatString("%cRed and %cBlue then %cDefault", Text.Red, Text.Blue, Text.Default));
13+
}
14+
}

0 commit comments

Comments
 (0)