WebColors extends HashMap<String, int[]> { public static float[] getRGBAColor(String name) { float[] color = null; try { String colorName = name.toLowerCase(); boolean colorStrWithoutHash = missingHashColorFormat(colorName); if (colorName.startsWith("#") || colorStrWithoutHash) { if (!colorStrWithoutHash) { colorName = colorName.substring(1); } if (colorName.length() == 3) { String red = colorName.substring(0, 1); color = new float[]{0, 0, 0, 1}; color[0] = (float) (Integer.parseInt(red + red, 16) / RGB_MAX_VAL); String green = colorName.substring(1, 2); color[1] = (float) (Integer.parseInt(green + green, 16) / RGB_MAX_VAL); String blue = colorName.substring(2); color[2] = (float) (Integer.parseInt(blue + blue, 16) / RGB_MAX_VAL); } else if (colorName.length() == 6) { color = new float[]{0, 0, 0, 1}; color[0] = (float) (Integer.parseInt(colorName.substring(0, 2), 16) / RGB_MAX_VAL); color[1] = (float) (Integer.parseInt(colorName.substring(2, 4), 16) / RGB_MAX_VAL); color[2] = (float) (Integer.parseInt(colorName.substring(4), 16) / RGB_MAX_VAL); } else { Logger logger = LoggerFactory.getLogger(WebColors.class); logger.error(LogMessageConstant.UNKNOWN_COLOR_FORMAT_MUST_BE_RGB_OR_RRGGBB); } } else if (colorName.startsWith("rgb(")) { final String delim = "rgb(), \t\r\n\f"; StringTokenizer tok = new StringTokenizer(colorName, delim); color = new float[]{0, 0, 0, 1}; parseRGBColors(color, tok); } else if (colorName.startsWith("rgba(")) { final String delim = "rgba(), \t\r\n\f"; StringTokenizer tok = new StringTokenizer(colorName, delim); color = new float[]{0, 0, 0, 1}; parseRGBColors(color, tok); if (tok.hasMoreTokens()) { color[3] = getAlphaChannelValue(tok.nextToken()); } } else if (NAMES.containsKey(colorName)) { int[] intColor = NAMES.get(colorName); color = new float[]{0, 0, 0, 1}; color[0] = (float) (intColor[0] / RGB_MAX_VAL); color[1] = (float) (intColor[1] / RGB_MAX_VAL); color[2] = (float) (intColor[2] / RGB_MAX_VAL); } } catch (Exception exc) { color = null; } return color; }  static DeviceRgb getRGBColor(String name); static float[] getRGBAColor(String name); static final WebColors NAMES; }
@Test public void getRGBAColorByNameTest() { String colorName = "violet"; float[] cmpRgba = new float[]{(float)(238/RGB_MAX_VAL), (float)(130/RGB_MAX_VAL), (float)(238/RGB_MAX_VAL), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(colorName); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }
@Test public void getRGBAColorByCodeWithHashTest() { String hashHex = "#EE82EE"; float[] cmpRgba = new float[]{(float)(238/RGB_MAX_VAL), (float)(130/RGB_MAX_VAL), (float)(238/RGB_MAX_VAL), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(hashHex); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }
@Test public void getRGBAColorByCode6DigitsTest() { String hexString = "EE82EE"; float[] cmpRgba = new float[]{(float)(238/RGB_MAX_VAL), (float)(130/RGB_MAX_VAL), (float)(238/RGB_MAX_VAL), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(hexString); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }
@Test public void getRGBAColorByCode3DigitsTest() { String hexString = "900"; float[] cmpRgba = new float[]{(float)(153/RGB_MAX_VAL), (float)(0.0), (float)(0.0), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(hexString); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }
@Test public void getRGBAColorByCodeWrongDigitsNumberTest() { String hexString = "9000"; float[] resultRgba = WebColors.getRGBAColor(hexString); Assert.assertNull(resultRgba); }
@Test public void getRGBAColorByRgbObjectTest() { String rgbString = "rgb(238,130,238)"; float[] cmpRgba = new float[]{(float)(238/RGB_MAX_VAL), (float)(130/RGB_MAX_VAL), (float)(238/RGB_MAX_VAL), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(rgbString); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }
@Test public void getRGBAColorByRgbaObjectTest() { String rgbaString = "rgba(238,130,238,255)"; float[] cmpRgba = new float[]{(float)(238/RGB_MAX_VAL), (float)(130/RGB_MAX_VAL), (float)(238/RGB_MAX_VAL), (float)(1.0)}; float delta = (float)(0.0001); float[] resultRgba = WebColors.getRGBAColor(rgbaString); Assert.assertArrayEquals(cmpRgba, resultRgba, delta); }