Skip to content

Commit

Permalink
Add ColorPacker.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Oct 28, 2024
1 parent 8ed1f4e commit 0407795
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/github/knokko/boiler/utilities/ColorPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@ public static byte alpha(int rgba) {
public static int unsigned(byte value) {
return value & 0xFF;
}

/**
* Returns a nice string showing the R, G, B, and A component of the packed color, like RGB(1, 2, 3) or
* RGB(100, 200, 200, 200). The alpha is skipped when it is 255 (opaque).
*/
public static String toString(int packed) {
int alpha = unsigned(alpha(packed));
String rgb = unsigned(red(packed)) + ", " + unsigned(green(packed)) + ", " + unsigned(blue(packed));
if (alpha == 255) return "RGB(" + rgb + ")";
else return "RGBA(" + rgb + ", " + unsigned(alpha(packed)) + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public void testSimplePackingRGB() {
assertEquals(rgba(1, 2, 3, 255), rgb(1, 2, 3));
assertEquals(rgba(1, 2, 3, 255), rgb((byte) 1, (byte) 2, (byte) 3));
}

@Test
public void testToString() {
assertEquals("RGBA(0, 100, 255, 200)", ColorPacker.toString(rgba(0, 100, 255, 200)));
assertEquals("RGB(255, 100, 0)", ColorPacker.toString(rgb(255, 100, 0)));
}
}

0 comments on commit 0407795

Please sign in to comment.