ColorConverter { public static String formatColorStr(String colorStr) { String str = colorStr.replaceAll("\\s", ""); if (str.toLowerCase().matches(RGB_DICTIONNARY)){ str = str.toLowerCase(); if(!str.endsWith(")")){ str = str + ")"; } } else if(str.matches(SHORT_RGB_DICTIONNARY)){ str = "rgb(" + str + ")"; } else if(str.matches(HEXADECIMAL_DICTIONNARY_V2)) { if (str.charAt(0) != '#') { str = "#" + str; } str = str.toUpperCase(); } else { ColorNameLookup c = new ColorNameLookup(); String name = c.getColorNameFromStr(str); String empty = ""; if(!name.equals(empty)){ str = name; } } return str; } 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 testFormatColorStrRgbLastParenthesisIsMissing() { String expResult = "rgb(255,255,255)"; String colorStr = "rgb(255,255,255"; System.out.println("formatColorStr - RgblastParenthesisIsMissing ["+ colorStr +"]"); String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); colorStr = " rgb( 255, 255, 255 "; System.out.println("formatColorStr - RgblastParenthesisIsMissing ["+ colorStr +"]"); result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrRgbToLowerCase() { System.out.println("fformatColorStr - RgbToLowerCase [RGB(255,255,255)]"); String colorStr = "RGB(255,255,255)"; String expResult = "rgb(255,255,255)"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrRgb() { System.out.println("formatColorStr - Rgb [ rgb( 255, 255, 255 ) ]"); String colorStr = " rgb( 255, 255, 255 ) "; String expResult = "rgb(255,255,255)"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrRgbShortWidthSpace() { System.out.println("formatColorStr - RgbShortWidthSpace [ 255, 255, 255 ]"); String colorStr = " 255, 255, 255 "; String expResult = "rgb(255,255,255)"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrRgbShort() { System.out.println("formatColorStr - RgbShort [255,255,255]"); String colorStr = "255,255,255"; String expResult = "rgb(255,255,255)"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNotValidHex() { System.out.println("formatColorStr - NotValidHex [Ff]"); String colorStr = "Ff"; String expResult = "Ff"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNotValidHex2() { System.out.println("formatColorStr - NotValidHex2 [FfFf]"); String colorStr = "FfFf"; String expResult = "FfFf"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNotValidHexLetter2() { System.out.println("formatColorStr - NotValidHexLetter2 [#ZzZ]"); String colorStr = "#ZzZ"; String expResult = "#ZzZ"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNotValidHexLetter() { System.out.println("formatColorStr - NotValidHexLetter [ZzZ]"); String colorStr = "ZzZ"; String expResult = "ZzZ"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrHexAddHash() { System.out.println("formatColorStr - HexAddHash [000000]"); String colorStr = "000000"; String expResult = "#000000"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrHexNotAddHash() { System.out.println("formatColorStr - HexNotAddHash [#000000]"); String colorStr = "#000000"; String expResult = "#000000"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrHexTrim() { System.out.println("formatColorStr - HexTrim [ #000000 ]"); String colorStr = " #000000 "; String expResult = "#000000"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrHexToUpperCase() { System.out.println("formatColorStr - HexToUpperCase [#ffffff]"); String colorStr = "#ffffff"; String expResult = "#FFFFFF"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrHex() { System.out.println("formatColorStr - Hex [ ffffff ]"); String colorStr = " ffffff "; String expResult = "#FFFFFF"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNameBadCase() { System.out.println("formatColorStr - Name [ wHite ]"); String colorStr = " wHite "; String expResult = "White"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrNameWithSpace() { System.out.println("formatColorStr - Name [ dark magenta ]"); String colorStr = " dark magenta "; String expResult = "DarkMagenta"; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }
@Test public void testFormatColorStrBadName() { System.out.println("formatColorStr - BadName [wHite_BadName]"); String colorStr = "wHite_BadName"; String expResult = colorStr; String result = ColorConverter.formatColorStr(colorStr); assertEquals(expResult, result); }