Java 8 Color - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Color class.

This code example shows how to use the following methods:getBlue, getGreen, getRed
 */ 
public class ColorUtil { 
 
	public static Color blend(Color color1, Color color2, double ratio) {
		int[] rgb1 = {color1.getRed(), color1.getGreen(), color1.getBlue()};
		int[] rgb2 = {color2.getRed(), color2.getGreen(), color2.getBlue()};
 
		return blend(rgb1, rgb2, ratio);
	} 
 
	public static Color blend(int[] color1, int[] color2, double ratio) {
		Color blended = new Color(
			(int)(((color2[0]-color1[0]) * ratio) + color1[0]),
			(int)(((color2[1]-color1[1]) * ratio) + color1[1]),
			(int)(((color2[2]-color1[2]) * ratio) + color1[2]));
 
		return blended;
	} 
 
	public static Color darker(int[] color, double ratio) {
7
CodeRank
This code example shows how to use the following methods:brighter, darker, getBlue, getGreen, getRed
        if( c == null ) c2=Color.GRAY;
        else            c2=c;
  
        final Color color=c2;
  
        int saturation = color.getRed() + color.getGreen() + color.getBlue();
        final Color contrast = saturation < 384 ? c.brighter() : c.darker();
  
        gc.setColor( color );
        gc.fillRoundRect( 0, 0, 14, 14, 2, 2);
  
        gc.setColor( contrast );
        gc.drawRoundRect( 0, 0, 14, 14, 2, 2 );
  
        return bi;
    } 
  
    /** 
     * Icon for grid data, small grid made up of provided colors. 
     * Layout:<pre><code> 
7
CodeRank
This code example shows how to use the following methods:getBlue, getGreen, getRed
This code example shows how to use the following methods:getBlue, getGreen, getRed
         
        assertPixel(image, 20, 20, Color.RED);
        assertPixel(image, 20, 60, Color.GREEN);
        assertPixel(image, 20, 100, Color.BLUE);
        Color linePixel = getPixelColor(image, 20, 140);
        assertTrue(linePixel.getRed() < 10);
        assertTrue(linePixel.getGreen() < 10);
        assertTrue(linePixel.getBlue() < 10);
    } 
     
    /** 
     * Tests a dpi rescaled legend with specific rule name 
     */ 
    @Test
    public void testStatesLegendDpiRescaledSingleRule() throws Exception {
        String base = "wms?service=WMS&version=1.1.1&request=GetLegendGraphic" +
                        "&layer=sf:states&style=Population" + 
                        "&format=image/png&width=20&height=20&legend_options=dpi:180&rule=2-4M"; 
        BufferedImage image = getAsImage(base, "image/png");
         

7
CodeRank
This code example shows how to use the following methods:getAlpha, getBlue, getGreen, getRed
		} 
		return arr;
	} 
	 
	public static String toCSS(Color color) {
		return color.getAlpha() < 255 ? getRGBAString(color) : getHexString(color);
	} 
	 
	public static String toCSS(int[] color) {
		if (color.length > 3)
			return toCSS(new Color(color[0], color[1], color[2], color[3]));
		return toCSS(new Color(color[0], color[1], color[2], 255));
	} 
	 
	public static String getRGBAString(Color c) {
		return "rgba(" + c.getRed() + ", " + c.getGreen() + ", " + 
				c.getBlue() + ", " + c.getAlpha() + ")";
	} 
	 
	public static String getRGBAString(int[] color) {

7
CodeRank
This code example shows how to use the following methods:getBlue, getGreen, getRed
    public ScoreAnimation(Position tilePosition, String points, ImmutablePoint point, Color color, Integer duration) {
        this.tilePosition = tilePosition;
        this.points = points;
        this.offset = point;
        this.color = color;
        if (duration == null) {
            nextFrame = Long.MAX_VALUE >> 20; //divisior greater then 1000000
        } else { 
            nextFrame = System.currentTimeMillis() + duration * 1000;
        } 
    } 
 
 
    @Override 
    public void paint(AnimationLayer l, Graphics2D g) {
        Color bgColor;
        if (UiUtils.isBrightColor(color)) {
            bgColor = POINTS_BLACK_BACKGROUND_COLOR;
        } else { 
            bgColor = POINTS_BACKGROUND_COLOR;

7
CodeRank
This code example shows how to use the following methods:getBlue, getGreen, getRed
     */ 
    protected void startParsing(final Attributes attrs) throws SAXException {
        final String encodedValue = attrs.getValue("value");
        this.color = Color.decode(encodedValue);
        if (attrs.getValue("alpha") != null) {
            this.color = new Color(this.color.getRed(), this.color.getGreen(),
                                   this.color.getBlue(), 
                                   Integer.parseInt(attrs.getValue("alpha")));
        } 
    } 
 
    /** 
     * Returns the color under construction. 
     *  
     * @return the color. 
     */ 
    public Object getObject() { 
        return this.color;
    } 
     
7
CodeRank
This code example shows how to use the following methods:getAlpha, getBlue, getGreen, getRed
        final AttributeList attribs = new AttributeList(); 
        if (mPlexAttribute != null) {
            attribs.setAttribute(mPlexAttribute, mPlexValue);
        } 
        attribs.setAttribute("value", encodeColor(color));
        if (color.getAlpha() != 255) {
            attribs.setAttribute("alpha", String.valueOf(color.getAlpha()));
        } 
        writer.writeTag(tagName, attribs, true);
    } 
 
    private String encodeColor(final Color color) {
        return "#" + encodeInt(color.getRed()) 
            + encodeInt(color.getGreen()) + encodeInt(color.getBlue());
    } 
 
    private String encodeInt(final int i) {
        final String retVal = Integer.toHexString(i);
        if (retVal.length() == 1) {
            return "0" + retVal;
7
CodeRank
This code example shows how to use the following methods:getAlpha, getBlue, getGreen, getRed
        assertEquals(Color.RED, c);
        assertEquals(255, c.getAlpha());
 
        buildDocument("<color>00ff0000</color>"); 
        c = (Color) parse();
        assertEquals(255, c.getRed());
        assertEquals(0, c.getBlue());
        assertEquals(0, c.getGreen());
        assertEquals(0, c.getAlpha());
 
        buildDocument("<color>ff0000</color>"); 
        c = (Color) parse();
        assertEquals(Color.RED, c);
        assertEquals(255, c.getAlpha());
    } 
} 
7
CodeRank
This code example shows how to use the following methods:getAlpha, getBlue, getGreen, getRed

6
CodeRank
See Code Examples for Java 8 Color Methods: