From 0407795060b6f887d39ea2be4d562e32f52ff405 Mon Sep 17 00:00:00 2001 From: knokko Date: Mon, 28 Oct 2024 21:28:48 +0100 Subject: [PATCH] Add ColorPacker.toString --- .../github/knokko/boiler/utilities/ColorPacker.java | 11 +++++++++++ .../knokko/boiler/utilities/TestColorPacker.java | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/com/github/knokko/boiler/utilities/ColorPacker.java b/src/main/java/com/github/knokko/boiler/utilities/ColorPacker.java index b455789..fd9c2ca 100644 --- a/src/main/java/com/github/knokko/boiler/utilities/ColorPacker.java +++ b/src/main/java/com/github/knokko/boiler/utilities/ColorPacker.java @@ -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)) + ")"; + } } diff --git a/src/test/java/com/github/knokko/boiler/utilities/TestColorPacker.java b/src/test/java/com/github/knokko/boiler/utilities/TestColorPacker.java index 0ad8cbb..57d1959 100644 --- a/src/test/java/com/github/knokko/boiler/utilities/TestColorPacker.java +++ b/src/test/java/com/github/knokko/boiler/utilities/TestColorPacker.java @@ -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))); + } }