Hue Mirror
in
Share your Work
•
2 years ago
Very simple application. Asks you to open an image, lets you scroll around with arrow keys. Click on two pixels in the picture to switch the hues of those pixels (and mirror every other hue along with it).
This is different from inverting the hue of an image in Photoshop! (which I why I thought of making it in the first place). Spacebar to switch between original/result view, 'O' to open another picture, 'S' to save the result (make sure you've clicked twice first!)
- PImage sourceImage, invertedImage;
- boolean displaySource, displayInverted, mouseClicks;
- int sourcePixelOne, sourcePixelTwo, x0, offsetX, y0, offsetY;
- void setup() {
- size(screen.width, screen.height, P2D);
- loadPixels();
- colorMode(HSB, 1.0);
- strokeWeight(1);
- stroke(1.0);
- if (!openImg()) {
- exit();
- }
- initKeysAndInput();
- }
- void draw() {
- checkKeyboardInput();
- if (displaySource) {
- imgToPixels(sourceImage);
- }
- else if (displayInverted) {
- imgToPixels(invertedImage);
- }
- updatePixels();
- }
- boolean openImg() {
- String inputImg = selectInput();
- if (inputImg == null) {
- println("No file selected!");
- return false;
- }
- else {
- background(0);
- sourceImage = loadImage(inputImg);
- sourceImage.loadPixels();
- imgToPixels(sourceImage);
- invertedImage = createImage(sourceImage.width, sourceImage.height, RGB);
- invertedImage.loadPixels();
- displaySource = mouseClicks = true;
- displayInverted = false;
- return true;
- }
- }
- void imgToPixels(PImage img) {
- if (width > img.width) {
- offsetX = (width - img.width) >> 1;
- x0 = img.width;
- }
- else {
- offsetX = 0;
- x0 = width;
- }
- if (height > img.height) {
- offsetY = (height - img.height) >> 1;
- y0 = img.height;
- }
- else {
- offsetY = 0;
- y0 = height;
- }
- for (int i = 0; i < x0; ++i) {
- for (int j = 0; j < y0; ++j) {
- pixels[i + offsetX + (j + offsetY)*width] = img.pixels[i + x + (j + y)*img.width];
- }
- }
- }
- void mirrorHue() {
- float hueOne = hue(sourcePixelOne);
- float hueTwo = hue(sourcePixelTwo);
- println("hueOne: " + hueOne + " hueTwo: " + hueTwo);
- float avgHue = (hueOne + hueTwo) * 0.5;
- for (int i = 0; i < sourceImage.pixels.length; ++i) {
- hueOne = hue(sourceImage.pixels[i]);
- hueTwo = (1.0 + 2*avgHue - hueOne)%1.0;//mirrors the hue around avgHue.
- invertedImage.pixels[i] = color(hueTwo, saturation(sourceImage.pixels[i]), brightness(sourceImage.pixels[i]));
- }
- invertedImage.updatePixels();
- }
- int x, y, delta;
- boolean[] keys;
- void initKeysAndInput() {
- keys = new boolean[526];
- for (int i = 0; i < keys.length; ++i) {
- keys[i] = false;
- }
- x = y = 0;
- delta = 5;
- }
- void keyPressed() {
- keys[keyCode] = true;
- }
- void keyReleased() {
- keys[keyCode] = false;
- if (!(checkKey(LEFT) || checkKey(RIGHT) || checkKey(UP) || checkKey(DOWN))) {
- delta = 5;
- }
- }
- boolean checkKey(int k) {
- if (keys.length >= k) {
- return keys[k];
- }
- return false;
- }
- void checkKeyboardInput() {
- if (checkKey(LEFT) && x > 0) {
- x = max(x-delta, 0);
- delta = min(delta+1, 40);
- }
- if (checkKey(RIGHT) && sourceImage.width > width) {
- x = min(x + delta, sourceImage.width - width);
- delta = min(delta+1, 40);
- }
- if (checkKey(UP) && y > 0) {
- y = max(y-delta, 0);
- delta = min(delta+1, 40);
- }
- if (checkKey(DOWN) && sourceImage.height > height) {
- y = min(y + delta, sourceImage.height - height);
- delta = min(delta+1, 40);
- }
- if (checkKey('o') || checkKey('O')) {
- openImg();
- }
- if (checkKey('s') || checkKey('S')) {
- PGraphics saveGraphics = createGraphics(invertedImage.width, invertedImage.height, P2D);
- saveGraphics.beginDraw();
- saveGraphics.loadPixels();
- for (int i = 0; i < saveGraphics.pixels.length; ++i) {
- saveGraphics.pixels[i] = invertedImage.pixels[i];
- }
- saveGraphics.updatePixels();
- saveGraphics.save("output.png");
- saveGraphics.endDraw();
- }
- if (checkKey(' ')) {
- displaySource = !displaySource;
- displayInverted = !displayInverted;
- keys[' '] = false; //one switch per press
- }
- }
- void mouseClicked(){
- if (mouseClicks){
- mouseClicks = !mouseClicks;
- sourcePixelOne = sourceImage.pixels[mouseX - offsetX + x + (mouseY - offsetY + y) * sourceImage.width];
- } else {
- mouseClicks = !mouseClicks;
- sourcePixelTwo = sourceImage.pixels[mouseX - offsetX + x + (mouseY - offsetY + y) * sourceImage.width];
- mirrorHue();
- }
- }