diff --git a/src/main/java/appeng/util/helpers/P2PHelper.java b/src/main/java/appeng/util/helpers/P2PHelper.java index 57ce23dae..151f4d515 100644 --- a/src/main/java/appeng/util/helpers/P2PHelper.java +++ b/src/main/java/appeng/util/helpers/P2PHelper.java @@ -21,6 +21,8 @@ package appeng.util.helpers; import com.google.common.base.Preconditions; +import net.minecraft.util.text.TextFormatting; + import appeng.api.util.AEColor; @@ -57,4 +59,32 @@ public class P2PHelper return (short) ( t & 0xFFFF ); } + public String toHexDigit( AEColor color ) + { + return String.format( "%01X", color.ordinal() ); + } + + public String toHexString( short frequency ) + { + return String.format( "%04X", frequency ); + } + + public String toColorHexDigit( AEColor color ) + { + return TextFormatting.fromColorIndex( color.ordinal() ) + this.toHexDigit( color ); + } + + public String toColorHexString( short frequency ) + { + final AEColor[] colors = toColors( frequency ); + final StringBuilder builder = new StringBuilder(); + + for( AEColor aeColor : colors ) + { + builder.append( this.toColorHexDigit( aeColor ) ); + } + + return builder.toString(); + } + } diff --git a/src/test/java/appeng/util/helpers/P2PHelperTest.java b/src/test/java/appeng/util/helpers/P2PHelperTest.java index c96a459cd..497a6fb75 100644 --- a/src/test/java/appeng/util/helpers/P2PHelperTest.java +++ b/src/test/java/appeng/util/helpers/P2PHelperTest.java @@ -32,14 +32,26 @@ public class P2PHelperTest private P2PHelper unitUnderTest = new P2PHelper(); - private static short WHITE_FREQUENCY = 0; - private static AEColor[] WHITE_COLORS = new AEColor[] { AEColor.WHITE, AEColor.WHITE, AEColor.WHITE, AEColor.WHITE }; + private static final short WHITE_FREQUENCY = 0; + private static final AEColor[] WHITE_COLORS = new AEColor[] { AEColor.WHITE, AEColor.WHITE, AEColor.WHITE, AEColor.WHITE }; - private static short BLACK_FREQUENCY = (short) 0xFFFF; - private static AEColor[] BLACK_COLORS = new AEColor[] { AEColor.BLACK, AEColor.BLACK, AEColor.BLACK, AEColor.BLACK }; + private static final short BLACK_FREQUENCY = (short) 0xFFFF; + private static final AEColor[] BLACK_COLORS = new AEColor[] { AEColor.BLACK, AEColor.BLACK, AEColor.BLACK, AEColor.BLACK }; - private static short MULTI_FREQUENCY = (short) 0xE8D1; - private static AEColor[] MULTI_COLORS = new AEColor[] { AEColor.RED, AEColor.LIGHT_GRAY, AEColor.GREEN, AEColor.ORANGE }; + private static final short MULTI_FREQUENCY = (short) 0xE8D1; + private static final AEColor[] MULTI_COLORS = new AEColor[] { AEColor.RED, AEColor.LIGHT_GRAY, AEColor.GREEN, AEColor.ORANGE }; + + private static final String HEX_WHITE_FREQUENCY = "0000"; + private static final String HEX_BLACK_FREQUENCY = "FFFF"; + private static final String HEX_MULTI_FREQUENCY = "E8D1"; + private static final String HEX_MIN_FREQUENCY = "8000"; + private static final String HEX_MAX_FREQUENCY = "7FFF"; + + private static final String COLOR_STRING_WHITE_FREQUENCY = "§00§00§00§00"; + private static final String COLOR_STRING_BLACK_FREQUENCY = "§fF§fF§fF§fF"; + private static final String COLOR_STRING_MULTI_FREQUENCY = "§eE§88§dD§11"; + private static final String COLOR_STRING_MIN_FREQUENCY = "§88§00§00§00"; + private static final String COLOR_STRING_MAX_FREQUENCY = "§77§fF§fF§fF"; @Test public void testToColors() @@ -66,4 +78,46 @@ public class P2PHelperTest } } + @Test + public void testToHexDigit() + { + assertEquals( "0", unitUnderTest.toHexDigit( AEColor.WHITE ) ); + assertEquals( "1", unitUnderTest.toHexDigit( AEColor.ORANGE ) ); + assertEquals( "2", unitUnderTest.toHexDigit( AEColor.MAGENTA ) ); + assertEquals( "3", unitUnderTest.toHexDigit( AEColor.LIGHT_BLUE ) ); + assertEquals( "4", unitUnderTest.toHexDigit( AEColor.YELLOW ) ); + assertEquals( "5", unitUnderTest.toHexDigit( AEColor.LIME ) ); + assertEquals( "6", unitUnderTest.toHexDigit( AEColor.PINK ) ); + assertEquals( "7", unitUnderTest.toHexDigit( AEColor.GRAY ) ); + assertEquals( "8", unitUnderTest.toHexDigit( AEColor.LIGHT_GRAY ) ); + assertEquals( "9", unitUnderTest.toHexDigit( AEColor.CYAN ) ); + assertEquals( "A", unitUnderTest.toHexDigit( AEColor.PURPLE ) ); + assertEquals( "B", unitUnderTest.toHexDigit( AEColor.BLUE ) ); + assertEquals( "C", unitUnderTest.toHexDigit( AEColor.BROWN ) ); + assertEquals( "D", unitUnderTest.toHexDigit( AEColor.GREEN ) ); + assertEquals( "E", unitUnderTest.toHexDigit( AEColor.RED ) ); + assertEquals( "F", unitUnderTest.toHexDigit( AEColor.BLACK ) ); + } + + @Test + public void testToHexString() + { + assertEquals( HEX_WHITE_FREQUENCY, unitUnderTest.toHexString( WHITE_FREQUENCY ) ); + assertEquals( HEX_BLACK_FREQUENCY, unitUnderTest.toHexString( BLACK_FREQUENCY ) ); + assertEquals( HEX_MULTI_FREQUENCY, unitUnderTest.toHexString( MULTI_FREQUENCY ) ); + + assertEquals( HEX_MIN_FREQUENCY, unitUnderTest.toHexString( Short.MIN_VALUE ) ); + assertEquals( HEX_MAX_FREQUENCY, unitUnderTest.toHexString( Short.MAX_VALUE ) ); + } + + @Test + public void testToColorHexString() + { + assertEquals( COLOR_STRING_WHITE_FREQUENCY, unitUnderTest.toColorHexString( WHITE_FREQUENCY ) ); + assertEquals( COLOR_STRING_BLACK_FREQUENCY, unitUnderTest.toColorHexString( BLACK_FREQUENCY ) ); + assertEquals( COLOR_STRING_MULTI_FREQUENCY, unitUnderTest.toColorHexString( MULTI_FREQUENCY ) ); + assertEquals( COLOR_STRING_MIN_FREQUENCY, unitUnderTest.toColorHexString( Short.MIN_VALUE ) ); + assertEquals( COLOR_STRING_MAX_FREQUENCY, unitUnderTest.toColorHexString( Short.MAX_VALUE ) ); + } + }