GenericColor extends AbstractDatatype { public static GenericColor valueOf(String color) { Objects.requireNonNull(color, "color"); Color namedColor = Color.fromName(color); if (namedColor != null) { return valueOf(namedColor); } int length = color.length(); Throwable cause = null; try { Color hexColor = Color.parseHexString(color); if (hexColor != null) { return valueOf(hexColor); } if (length >= 7) { String model = BasicHelper.toUpperCase(color.substring(0, 3)); ColorModel colorModel = ColorModel.valueOf(model); int index = 3; boolean hasAlpha = false; char c = Character.toLowerCase(color.charAt(index)); if (c == 'a') { hasAlpha = true; index++; c = color.charAt(index); } if (c == '(') { index++; int endIndex = color.indexOf(',', index); if (endIndex > 0) { String firstSegment = color.substring(index, endIndex).trim(); index = endIndex + 1; endIndex = color.indexOf(',', index); if (endIndex > 0) { String secondSegment = color.substring(index, endIndex).trim(); index = endIndex + 1; if (hasAlpha) { endIndex = color.indexOf(',', index); } else { endIndex = length - 1; } if (endIndex > 0) { String thirdSegment = color.substring(index, endIndex).trim(); Alpha alpha; if (hasAlpha) { alpha = new Alpha(color.substring(endIndex + 1, length - 1)); } else { alpha = Alpha.OPAQUE; } switch (colorModel) { case RGB: return valueOf(new Red(firstSegment), new Green(secondSegment), new Blue(thirdSegment), alpha); case HSL: return valueOf(new Hue(firstSegment), new Saturation(secondSegment), new Lightness(thirdSegment), alpha); case HSV: case HSB: return valueOf(new Hue(firstSegment), new Saturation(secondSegment), new Brightness(thirdSegment), alpha); default : throw new IllegalStateException("" + colorModel); } } } } } } } catch (RuntimeException e) { cause = e; } throw new IllegalArgumentException(color, cause); } protected  GenericColor(); static GenericColor valueOf(String color); static GenericColor valueOf(Color color); static GenericColor valueOf(Red red, Green green, Blue blue, Alpha alpha); static GenericColor valueOf(Hue hue, Saturation saturation, Brightness brightness, Alpha alpha); static GenericColor valueOf(Hue hue, Saturation saturation, Lightness lightness, Alpha alpha); Alpha getAlpha(); Hue getHue(); Saturation getSaturationHsb(); Saturation getSaturationHsl(); Brightness getBrightness(); Lightness getLightness(); Chroma getChroma(); Red getRed(); Blue getBlue(); Green getGreen(); AbstractDoubleSegment<?> getSegment(ColorSegmentType type); Color toColor(); GenericColor invert(ColorModel model); GenericColor lighten(ColorFactor factor); GenericColor darken(ColorFactor factor); @Override String toString(); String toString(ColorModel colorModel); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testExampleWhite() { ColorModel model = ColorModel.RGB; Red red = new Red(1.0); Green green = new Green(1.0); Blue blue = new Blue(1.0); Alpha alpha = new Alpha(1.0); GenericColor color = GenericColor.valueOf(red, green, blue, alpha); assertThat(GenericColor.valueOf("white")).isEqualTo(color); assertThat(GenericColor.valueOf("#FFFFFF")).isEqualTo(color); checkColor(color, red, green, blue, new Hue(0.0), new Saturation(0.0), new Saturation(0.0), new Brightness(1.0), new Lightness(1.0), new Chroma(0.0), alpha, true, model); }
@Test public void testExampleBlack() { ColorModel model = ColorModel.RGB; Red red = new Red(0.0); Green green = new Green(0.0); Blue blue = new Blue(0.0); Alpha alpha = new Alpha(1.0); GenericColor color = GenericColor.valueOf(red, green, blue, alpha); assertThat(GenericColor.valueOf("BLACK")).isEqualTo(color); assertThat(GenericColor.valueOf("#000000")).isEqualTo(color); assertThat(GenericColor.valueOf("rgb(0, 0, 0)")).isEqualTo(color); checkColor(color, red, green, blue, new Hue(0.0), new Saturation(0.0), new Saturation(0.0), new Brightness(0.0), new Lightness(0.0), new Chroma(0.0), alpha, true, model); }
@Test public void testExampleHsl7E7EB8() { ColorModel model = ColorModel.HSL; Hue hue = new Hue(240.5); Saturation saturationHsl = new Saturation(0.290); Lightness lightness = new Lightness(0.607); Alpha alpha = new Alpha(0.625); GenericColor color = GenericColor.valueOf(hue, saturationHsl, lightness, alpha); checkColor(color, new Red(0.4949295), new Green(0.4930299), new Blue(0.72097), hue, new Saturation(0.3161574), saturationHsl, new Brightness(0.72097), lightness, new Chroma(0.22794), alpha, false, model); }
@Test public void testExampleHslA0A424() { ColorModel model = ColorModel.HSL; Hue hue = new Hue(61.8); Saturation saturationHsl = new Saturation(0.638); Lightness lightness = new Lightness(0.393); Alpha alpha = new Alpha(0.125); GenericColor color = GenericColor.valueOf(hue, saturationHsl, lightness, alpha); checkColor(color, new Red(0.62869), new Green(0.643734), new Blue(0.142266), hue, new Saturation(0.778998779), saturationHsl, new Brightness(0.643734), lightness, new Chroma(0.501468), alpha, false, model); }
@Test public void testExampleHsl19CB97() { ColorModel model = ColorModel.HSL; Hue hue = new Hue(162.4); Saturation saturationHsl = new Saturation(0.779); Lightness lightness = new Lightness(0.447); Alpha alpha = new Alpha(0.3333); GenericColor color = GenericColor.valueOf(hue, saturationHsl, lightness, alpha); checkColor(color, new Red(0.098787), new Green(0.795213), new Blue(0.590928), hue, new Saturation(0.875773), saturationHsl, new Brightness(0.795213), lightness, new Chroma(0.696426), alpha, false, model); }