ColorConverter { public static Color colorFromRgbStr(String colorStr) { Color color = null; String str = colorStr.toLowerCase().replaceAll("\\s", ""); if (str.matches(RGB_DICTIONNARY) | str.matches(SHORT_RGB_DICTIONNARY)){ str = str.replaceAll("rgb\\(", ""); str = str.replaceAll("\\)", ""); String[] strList = str.split(","); int r = Integer.parseInt(strList[0]); int g = Integer.parseInt(strList[1]); int b = Integer.parseInt(strList[2]); if( r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX && r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN){ color = new Color(r, g, b); } } return color; } 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 testColorFromRgbStrBadFormat() { String colorStr = "#255,0,0"; System.out.println("colorFromRgbStrBadRedValue ["+ colorStr +"]"); Color expResult = null; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBadValue() { String colorStr = "-1, 0, 0"; System.out.println("colorFromRgbStrBadValue ["+ colorStr +"]"); Color expResult = null; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBadRedValue() { String colorStr = "256, 0, 0"; System.out.println("colorFromRgbStrBadRedValue ["+ colorStr +"]"); Color expResult = null; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBadGreenValue() { String colorStr = "0, 256, 0"; System.out.println("colorFromRgbStrBadGreenValue ["+ colorStr +"]"); Color expResult = null; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBadBlueValue() { String colorStr = "0, 0, 256"; System.out.println("colorFromRgbStrBadBlueValue ["+ colorStr +"]"); Color expResult = null; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStr() { String colorStr = " rgb(175, 175, 175) "; System.out.println("colorFromRgbStr ["+ colorStr +"]"); Color expResult = new Color(175,175,175); Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrShort() { String colorStr = "175, 175, 175"; System.out.println("colorFromRgbStrShort ["+ colorStr +"]"); Color expResult = new Color(175,175,175); Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBlack() { String colorStr = "rgb(0,0,0)"; System.out.println("colorFromRgbStrBlack ["+ colorStr +"]"); Color expResult = Color.BLACK; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrWhite() { String colorStr = "rgb(255,255,255)"; System.out.println("colorFromRgbStrWhite ["+ colorStr +"]"); Color expResult = Color.WHITE; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrPink() { String colorStr = "rgb(255,175,175)"; System.out.println("colorFromRgbStrPink ["+ colorStr +"]"); Color expResult = Color.PINK; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrBlackShort() { String colorStr = "0,0,0"; System.out.println("colorFromRgbStrBlackShort ["+ colorStr +"]"); Color expResult = Color.BLACK; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrWhiteShort() { String colorStr = "255,255,255"; System.out.println("colorFromRgbStrWhiteShort ["+ colorStr +"]"); Color expResult = Color.WHITE; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }
@Test public void testColorFromRgbStrPinkShort() { String colorStr = "255,175,175"; System.out.println("colorFromRgbStrPinkShort ["+ colorStr +"]"); Color expResult = Color.PINK; Color result = ColorConverter.colorFromRgbStr(colorStr); assertEquals(expResult, result); }