package bdv.tools.brightness;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.Icon;

/**
 * Adapted from http://stackoverflow.com/a/3072979/230513
 */
public class ColorIcon implements Icon
{
	private final int width;

	private final int height;

	private final boolean drawAsCircle;

	private final int arcWidth;

	private final int arcHeight;

	private final Color color;

	private final int size; // == min(width, height)

	private int ox;

	private int oy;

	public ColorIcon( final Color color )
	{
		this( color, 16, 16, true );
	}

	public ColorIcon( final Color color, final int width, final int height, final boolean drawAsCircle )
	{
		this( color, width, height, drawAsCircle, 3, 3 );
	}

	public ColorIcon( final Color color, final int width, final int height, final int arcWidth, final int arcHeight )
	{
		this( color, width, height, false, arcWidth, arcHeight );
	}

	private ColorIcon( final Color color, final int width, final int height, final boolean drawAsCircle, final int arcWidth, final int arcHeight )
	{
		this.color = color;
		this.width = width;
		this.height = height;
		this.drawAsCircle = drawAsCircle;
		this.arcWidth = arcWidth;
		this.arcHeight = arcHeight;

		size = Math.min( width, height );
		ox = ( width - size ) / 2;
		oy = ( height - size ) / 2;
	}

	@Override
	public void paintIcon( final Component c, final Graphics g, final int x, final int y )
	{
		final Graphics2D g2d = ( Graphics2D ) g;
		g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
		final int x0 = x + ox;
		final int y0 = y + oy;
		if ( color == null )
		{
			g2d.setColor( new Color( 0xffbcbc ) );
			g2d.fillArc( x0, y0, size, size, 0, 120 );
			g2d.setColor( new Color( 0xbcffbc ) );
			g2d.fillArc( x0, y0, size, size, 120, 120 );
			g2d.setColor( new Color( 0xbcbcff ) );
			g2d.fillArc( x0, y0, size, size, 240, 120 );
		}
		else
		{
			g2d.setColor( color );
			if ( drawAsCircle )
				g2d.fillOval( x0, y0, size, size );
			else
				g2d.fillRoundRect( x, y, width, height, arcWidth, arcHeight );
		}
	}

	@Override
	public int getIconWidth()
	{
		return width;
	}

	@Override
	public int getIconHeight()
	{
		return height;
	}
}

@@ -439,32 +439,69 @@
 int arcHeight;%0A%0A
+%09private final boolean drawOutline;%0A%0A
 %09private final C
@@ -569,24 +569,30 @@
 )%0A%0A%09private 
+final 
 int ox;%0A%0A%09pr
@@ -597,16 +597,22 @@
 private 
+final 
 int oy;%0A
@@ -842,16 +842,23 @@
 le, 3, 3
+, false
  );%0A%09%7D%0A%0A
@@ -1027,24 +1027,253 @@
 h, arcHeight
+, false );%0A%09%7D%0A%0A%09public ColorIcon( final Color color, final int width, final int height, final int arcWidth, final int arcHeight, final boolean drawOutline )%0A%09%7B%0A%09%09this( color, width, height, false, arcWidth, arcHeight, drawOutline
  );%0A%09%7D%0A%0A%09pri
@@ -1401,32 +1401,59 @@
 al int arcHeight
+, final boolean drawOutline
  )%0A%09%7B%0A%09%09this.col
@@ -1603,16 +1603,50 @@
 cHeight;
+%0A%09%09this.drawOutline = drawOutline;
 %0A%0A%09%09size
@@ -2500,16 +2500,266 @@
 eight );
+%0A%0A%09%09%09if ( drawOutline )%0A%09%09%09%7B%0A%09%09%09%09g2d.setColor( c.isFocusOwner() ? new Color( 0x8FC4F9 ) : Color.gray );%0A%09%09%09%09if ( drawAsCircle )%0A%09%09%09%09%09g2d.drawOval( x0, y0, size, size );%0A%09%09%09%09else%0A%09%09%09%09%09g2d.drawRoundRect( x, y, width, height, arcWidth, arcHeight );%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%7D%0A

Add (optional) focus outline to ColorIcon