/** * 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); }
/** * 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 red The red value * @param green The green value * @param blue The blue value * @return The color object */ public static Color ofRgb(int red, int green, int blue) { return new Color(red, green, blue); }
/** * 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 public DataContainer toContainer() { return DataContainer.createNew() .set(Queries.CONTENT_VERSION, getContentVersion()) .set(Queries.COLOR_RED, this.getRed()) .set(Queries.COLOR_GREEN, this.getGreen()) .set(Queries.COLOR_BLUE, this.getBlue()); }
/** * 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); }
@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 testGetRed() { assertTrue(0xFF == Color.RED.getRed()); }
@Test public void testGetGreen() { assertTrue(0x80 == Color.GREEN.getGreen()); }
@Test public void testGetBlue() { assertTrue(0xFF == Color.BLUE.getBlue()); }
/** * Creates a new color with the provided {@code Colors}. * * @param colors The provided colors to mix * @return The new color */ public Color mixWithColors(Color... colors) { Color[] newColorArray = new Color[colors.length + 1]; newColorArray[0] = this; System.arraycopy(colors, 0, newColorArray, 1, colors.length); return mixColors(newColorArray); }
@Override public DataContainer toContainer() { return new MemoryDataContainer() .set(Queries.CONTENT_VERSION, getContentVersion()) .set(Queries.COLOR_RED, this.getRed()) .set(Queries.COLOR_GREEN, this.getGreen()) .set(Queries.COLOR_BLUE, this.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); } } }