Created
March 28, 2012 22:50
-
-
Save tomgibara/2231258 to your computer and use it in GitHub Desktop.
Color sample plotter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BasicStroke; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.Graphics2D; | |
import java.awt.RenderingHints; | |
import java.awt.geom.Arc2D; | |
import java.awt.geom.Ellipse2D; | |
import java.awt.geom.Line2D; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import com.tomgibara.graphics.util.ImageUtil; | |
public class PlotColorSamples { | |
public static void main(String[] args) throws IOException { | |
String path = args[0]; | |
BufferedImage image = ImageIO.read(new File(path, "samples.png")); | |
int[] rgb = ImageUtil.toIntRGB(image); | |
ImageIO.write(plot(rgb, true), "PNG", new File(path, "brightness-plot.png")); | |
ImageIO.write(plot(rgb, false), "PNG", new File(path, "saturation-plot.png")); | |
} | |
private static BufferedImage plot(int[] rgb, boolean brightness) { | |
String name = brightness ? "Brightness" : "Saturation"; | |
int size = 300; | |
BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); | |
Graphics2D g = img.createGraphics(); | |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); | |
g.setBackground(Color.WHITE); | |
g.clearRect(0, 0, size, size); | |
g.setColor(Color.GRAY); | |
float[] hsb = new float[3]; | |
double radius = size *.5f; | |
g.translate(radius, radius); | |
g.scale(radius, radius); | |
int steps = 30; | |
double angleStep = 360.0 / steps * 0.8; | |
float strokeWidth = 0.05f; | |
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); | |
double outerRadius = 1 - strokeWidth; | |
for (int i = 0; i < steps; i++) { | |
double angle = i * 360.0 / steps; | |
Color c = Color.getHSBColor(i / (float) steps, 0.5f, 0.5f); | |
g.setColor(c); | |
g.draw(new Arc2D.Double(-outerRadius, -outerRadius, 2 * outerRadius, 2 * outerRadius, angle - angleStep/2, angleStep, Arc2D.OPEN)); | |
} | |
double innerRadius = 1 - 1.5 * strokeWidth; | |
g.setColor(Color.BLACK); | |
g.setStroke(new BasicStroke(0.01f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); | |
g.rotate(Math.PI/6); | |
g.draw(new Line2D.Double(0, 0, innerRadius, 0)); | |
g.draw(new Line2D.Double(innerRadius, 0, innerRadius - 0.04, -0.02)); | |
g.draw(new Line2D.Double(innerRadius, 0, innerRadius - 0.04, 0.02)); | |
g.setFont(new Font("Arial", Font.PLAIN, 1).deriveFont(0.11f)); | |
g.drawString(name, 0.20f, 0.12f); | |
g.rotate(-Math.PI/6); | |
for (int i = 0; i < rgb.length; i++) { | |
Color c = new Color(rgb[i]); | |
Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsb); | |
double a = -hsb[0] * 2.0 * Math.PI; | |
double r = hsb[brightness ? 2 : 1] * 0.9; | |
double x = Math.cos(a) * r; | |
double y = Math.sin(a) * r; | |
double ptr = 0.03; | |
g.setColor(c); | |
g.fill(new Ellipse2D.Double(x-ptr, y-ptr, ptr*2, ptr*2)); | |
} | |
ImageUtil.showImage(name + " Plot", img); | |
return img; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment