/** * Gets a new {@link Color} based on the hexadecimal value * for a combined {@code red}, {@code green}, and {@code blue} * color. Note that colors do not utilize an alpha modifier * * @param hex The hexadecimal value of the color * @return The color object */ public static Color ofRgb(int hex) { return ofRgb((hex >> 0x10) & MASK, (hex >> 0x8) & MASK, hex & MASK); }
/** * Creates a new {@link Color} by using the provided * {@code red} color, while retaining the current {@link #getGreen()} * and {@link #getBlue()} values. * * @param red The red value to use * @return The new color object */ public Color withRed(int red) { return ofRgb(red, getGreen(), getBlue()); }
/** * Creates a new {@link Color} by using the provided * {@code green} color, while retaining the current {@link #getRed()} * and {@link #getBlue()} values. * * @param green The green value to use * @return The new color object */ public Color withGreen(int green) { return ofRgb(getRed(), green, getBlue()); }
/** * Creates a new {@link Color} by using the provided * {@code blue} color, while retaining the current {@link #getGreen()} * and {@link #getRed()} ()} values. * * @param blue The blue value to use * @return The new color object */ public Color withBlue(int blue) { return ofRgb(getRed(), getGreen(), blue); }
@Override protected Optional<Color> buildContent(DataView container) throws InvalidDataException { if (!container.contains(Queries.COLOR_RED, Queries.COLOR_GREEN, Queries.COLOR_BLUE)) { return Optional.empty(); } try { final int red = container.getInt(Queries.COLOR_RED).get(); final int green = container.getInt(Queries.COLOR_GREEN).get(); final int blue = container.getInt(Queries.COLOR_BLUE).get(); return Optional.of(Color.ofRgb(red, green, blue)); } catch (Exception e) { throw new InvalidDataException("Could not parse some data.", e); } } }
/** * Creates a new {@link Color} combining the provided {@link Color} * objects, their summation and average is taken into effect * to properly mix the colors together. * * @param colors The colors to mix * @return The final output mixed color */ public static Color mixColors(Color... colors) { Validate.noNullElements(colors, "No null colors allowed!"); checkArgument(colors.length > 0, "Cannot have an empty color array!"); if (colors.length == 1) { return colors[0]; } int red = colors[0].getRed(); int green = colors[0].getGreen(); int blue = colors[0].getBlue(); for (int i = 1; i < colors.length; i++) { red += colors[i].getRed(); green += colors[i].getGreen(); blue += colors[i].getBlue(); } int averageRed = Math.round((float) red / colors.length); int averageGreen = Math.round((float) green / colors.length); int averageBlue = Math.round((float) blue / colors.length); return ofRgb(averageRed, averageGreen, averageBlue); }
@Test public void testAsJavaColor() { final int color = 0xFF00FF; final Color apiColor = Color.ofRgb(color); final java.awt.Color javaColor = apiColor.asJavaColor(); final java.awt.Color testColor = new java.awt.Color(color); assertTrue(javaColor.equals(testColor)); }
@Test public void testOfRgbForRedGreenBlue() { final Color colorGray = Color.ofRgb(128, 128, 128); assertTrue(Color.GRAY.equals(colorGray)); }
@Test public void testOfRgbHex() { final Color colorGray = Color.ofRgb(0x808080); assertTrue(Color.GRAY.equals(colorGray)); }
@Test public void testGetRgb() { final int color = 0x808080; final Color color1 = Color.ofRgb(color); final int rgb = color1.getRgb(); assertTrue(color == rgb); }
@Test public void testEquals() { assertTrue(Color.GREEN.equals(Color.ofRgb(0x008000))); assertTrue(!Color.GREEN.equals(Color.GRAY)); }
public static Color ofRgb(int hex) { return ofRgb((hex >> 0x10) & MASK, (hex >> 0x8) & MASK, hex & MASK); }
public Color withRed(int red) { return ofRgb(red, getGreen(), getBlue()); }
public Color withBlue(int blue) { return ofRgb(getRed(), getGreen(), blue); }
public Color withGreen(int green) { return ofRgb(getRed(), green, getBlue()); }
@Override protected Optional<Color> buildContent(DataView container) throws InvalidDataException { if (!container.contains(Queries.COLOR_RED, Queries.COLOR_GREEN, Queries.COLOR_BLUE)) { return Optional.empty(); } try { final int red = container.getInt(Queries.COLOR_RED).get(); final int green = container.getInt(Queries.COLOR_GREEN).get(); final int blue = container.getInt(Queries.COLOR_BLUE).get(); return Optional.of(Color.ofRgb(red, green, blue)); } catch (Exception e) { throw new InvalidDataException("Could not parse some data.", e); } } }
public static Color mixColors(Color... colors) { Validate.noNullElements(colors, "No null colors allowed!"); checkArgument(colors.length > 0, "Cannot have an empty color array!"); if (colors.length == 1) { return colors[0]; } int red = colors[0].getRed(); int green = colors[0].getGreen(); int blue = colors[0].getBlue(); for (int i = 1; i < colors.length; i++) { red += colors[i].getRed(); green += colors[i].getGreen(); blue += colors[i].getBlue(); } int averageRed = Math.round((float) red / colors.length); int averageGreen = Math.round((float) green / colors.length); int averageBlue = Math.round((float) blue / colors.length); return ofRgb(averageRed, averageGreen, averageBlue); }
builder.color(Color.ofRgb(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]))); } else if (args[0].equalsIgnoreCase("fade")) { String[] rgb = RegexUtil.COMMA_PATTERN.split(args[1]); builder.fade(Color.ofRgb(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]))); } else if (args[0].equalsIgnoreCase("flicker") || args[0].equalsIgnoreCase("twinkle")) { builder.flicker(true);