ColorConverter { public static String rgb2Hsl(Color color) { float[] hsvTab = new float[MAX_COMPONENT]; Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsvTab); float h = hsvTab[HUE] * MAX_ANGLE; float l = (CONSTANT_SL_COMPONENTS_TWO - (hsvTab[SATURATION] * CONSTANT_SL_COMPONENTS_HUNDRED) / CONSTANT_SL_COMPONENTS_HUNDRED) * (hsvTab[BRIGHTNESS] * CONSTANT_SL_COMPONENTS_HUNDRED) / CONSTANT_SL_COMPONENTS_TWO; float s = (hsvTab[SATURATION] * CONSTANT_SL_COMPONENTS_HUNDRED) * (hsvTab[BRIGHTNESS] * CONSTANT_SL_COMPONENTS_HUNDRED) / (l < CONSTANT_S_COMPONENTS_FIFTY ? l * CONSTANT_SL_COMPONENTS_TWO : CONSTANT_S_COMPONENTS_TWO_HUNDRED - l * CONSTANT_SL_COMPONENTS_TWO); return ("hsl(" + Float.valueOf(h).intValue() + ", " + Float.valueOf(s).intValue() + "%" + ", " + Float.valueOf(l).intValue() + "%" + ")"); } 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 testRgb2Hsl() { System.out.println("------------ rgb2Hsl ----------------"); Color colorA; String result; String expResult; colorA = Color.BLACK; result = ColorConverter.rgb2Hsl(colorA); expResult = "hsl(0, 0%, 0%)"; assertEquals(expResult, result); System.out.println("rgb2Hsl BLACK to ["+ expResult +"]"); colorA = Color.WHITE; result = ColorConverter.rgb2Hsl(colorA); expResult = "hsl(0, 0%, 100%)"; System.out.println("rgb2Hsl WHITE to ["+ expResult +"]"); assertEquals(expResult, result); colorA = new Color(70, 136, 71); result = ColorConverter.rgb2Hsl(colorA); expResult = "hsl(120, 32%, 40%)"; System.out.println("rgb2Hsl #468847 to ["+ expResult +"]"); assertEquals(expResult, result); System.out.println("--------------------------"); }