Compare And Contrast Hue.java
package ui;
import java.io.File;
import color.ColorHelper;
import model.HSBColor;
import processing.core.PApplet;
import processing.core.PImage;
@SuppressWarnings("serial")
public class CompareAndContrastHue extends PApplet {
int fileIndex = 0;
private String[] fileNames;
private static String filePath = "../data/images/";
static final int hueRange = 320;
static final int hueTolerance = 40;
private static final int imageWidth = 384;
private static final int imageHeight = 268;
public void setup() {
size(3*imageWidth, imageHeight);
background(0);
noLoop();
colorMode(HSB, hueRange - 1);
// Read in images.
File dir = new File(filePath);
fileNames = dir.list();
nextFileIndex();
}
public void draw() {
PImage img = loadImage(filePath + fileNames[fileIndex]);
img.loadPixels();
img.resize(imageWidth, imageHeight);
HSBColor color = ColorHelper.hsbColorFromImage(img, this, hueRange);
image(img, imageWidth, 0, imageWidth, imageHeight);
// Display image only showing dominant hue.
drawImage(createImage(imageWidth, imageHeight, HSB), img.pixels, color,
0, 0, imageWidth, imageHeight, false);
// Display image excluding dominant hue.
drawImage(createImage(imageWidth, imageHeight, HSB), img.pixels, color,
2 * imageWidth, 0, imageWidth, imageHeight, true);
}
public void mousePressed() {
nextFileIndex();
redraw();
}
private void drawImage(PImage img, int[] pixels, HSBColor color,
int x, int y, int width, int height, boolean showDominantHue) {
img.loadPixels();
// Manipulate photo, grayscale any pixel that isn't close to that hue.
for (int i = 0; i < img.pixels.length; i++) {
int pixel = pixels[i];
float hue = hue(pixel);
if (hueInRange(hue, color.h, hueTolerance) == showDominantHue) {
float brightness = brightness(pixel);
img.pixels[i] = color(brightness);
} else {
img.pixels[i] = pixel;
}
}
image(img, x, y, width, height);
}
private void nextFileIndex() {
while (true) {
fileIndex++;
if (fileIndex < fileNames.length) {
if (fileNames[fileIndex].toLowerCase().contains(".jpg")) {
break;
}
} else {
fileIndex = 0;
}
}
}
private static boolean hueInRange(float hueA, float hueB, int tolerance) {
return Math.abs(hueA - hueB) < tolerance;
}
}