ColorConverter { public static Color hex2Rgb(String colorStr) { if(colorStr.length() > 0){ if (colorStr.charAt(0) == '#') { String str = colorStr.substring(1); if (str.matches(HEXADECIMAL_DICTIONNARY) && str.length() == RGB_HEXA_LENGTH) { return getNewColor(str); } else if (str.matches(HEXADECIMAL_DICTIONNARY) && str.length() == RGB_SHORT_HEXA_LENGTH) { return getNewColorShortHexa(str); } } else if (colorStr.matches(HEXADECIMAL_DICTIONNARY)) { if (colorStr.length() == RGB_HEXA_LENGTH) { return getNewColor(colorStr); } else if (colorStr.length() == RGB_SHORT_HEXA_LENGTH) { return getNewColorShortHexa(colorStr); } } } return null; } private  ColorConverter(); static Color offsetHsbColor(Color color, float offsetHue, float offsetSaturation, float offsetBrightness); static Float getBrightness(Color color); static Float getSaturation(Color color); static Float getHue(Color color); static Color offsetRgbColor(Color bgColor, int offsetRed, int offsetGreen, int offsetBlue); static String formatColorStr(String colorStr); static Color colorFromStr(String colorStr); static Color colorFromColorName(String colorStr); static Color colorFromRgbStr(String colorStr); static Color hex2Rgb(String colorStr); static String hex2Rgb(Color color); static String rgb2Hex(Color color); static String rgb2Hsl(Color color);  }
@Test public void testHex2RgbColor() { System.out.println("---- Hex2Rgb_color --------------------"); Color colorA; String expResult; String result; colorA = Color.WHITE; System.out.println("hex2Rgb [Color.WHITE]"); expResult = "rgb(255, 255, 255)"; result = ColorConverter.hex2Rgb(colorA); assertEquals(expResult, result); colorA = Color.BLACK; System.out.println("hex2Rgb [Color.BLACK]"); expResult = "rgb(0, 0, 0)"; result = ColorConverter.hex2Rgb(colorA); assertEquals(expResult, result); System.out.println("--------------------------"); }
@Test public void testHex2RgbStr() { System.out.println("---- Hex2RgbStr --------------------"); String colorStr = "FFFFFF"; System.out.println("hex2Rgb string ["+ colorStr +"]"); Color expResult = Color.WHITE; Color result = ColorConverter.hex2Rgb(colorStr); assertEquals(expResult, result); colorStr = "FFF"; System.out.println("hex2Rgb string ["+ colorStr +"]"); expResult = Color.WHITE; result = ColorConverter.hex2Rgb(colorStr); assertEquals(expResult, result); colorStr = "#FFFFFF"; System.out.println("hex2Rgb string ["+ colorStr +"]"); expResult = Color.WHITE; result = ColorConverter.hex2Rgb(colorStr); assertEquals(expResult, result); colorStr = "#FFF"; System.out.println("hex2Rgb string ["+ colorStr +"]"); expResult = Color.WHITE; result = ColorConverter.hex2Rgb(colorStr); assertEquals(expResult, result); System.out.println("--------------------------"); }