Color extraction

FluFlu
edited May 2016 in Share Your Work

I just made a simple sketch that extracts 4 random colors from a picture. Anyway, this is my own version of the program, but there are others, of course. Sincerely, I don't know exactly why I am posting it, but maybe someone could make something better out of it, cause I can't. I was going towards selecting not just random colors, but the dominant ones, didn't made it through there...

/*
Image - random color extraction
by Adrian Fluturel
8th of July 2014
v1.9
Made with Processing and PDE
*/

PImage inputImg;
int buttonFill;
color[] randomColors = new color[4];
String inputPath;
PFont font;

void setup() {
  size(500, 300);
  font = createFont("Arial",16,true);
  textFont(font);
}

void draw() {
  background(255);

  fill(buttonFill);
  stroke(190);
  ellipse(width - 40, 40, 20, 20);

  if (dist(width - 40, 40, mouseX, mouseY) < 20 && mousePressed) {
    delay(200);
    buttonFill = 0;
    selectInput("", "inputFile");
  } else {
    buttonFill = 255;
  }

  if (inputPath != null) {

    noStroke();
    fill(randomColors[0]);
    ellipse(50, height/5, 20, 20);
    fill(120);
    text(int(red(randomColors[0])) + ", "
    + int(green(randomColors[0])) + ", "
    + int(blue(randomColors[0])), 80, height/5 + 7);

    fill(randomColors[1]);
    ellipse(50, height/5 + 50, 20, 20);
    fill(120);
    text(int(red(randomColors[1])) + ", "
    + int(green(randomColors[1])) + ", "
    + int(blue(randomColors[1])), 80, height/5 + 50 + 7);

    fill(randomColors[2]);
    ellipse(50, height/5 + 100, 20, 20);
    fill(120);
    text(int(red(randomColors[2])) + ", "
    + int(green(randomColors[2])) + ", "
    + int(blue(randomColors[2])), 80, height/5 + 100 + 7);

    fill(randomColors[3]);
    ellipse(50, height/5 + 150, 20, 20);
    fill(120);
    text(int(red(randomColors[3])) + ", "
    + int(green(randomColors[3])) + ", "
    + int(blue(randomColors[3])), 80, height/5 + 150 + 7);
  }
}

void inputFile(File inputFile) {
  if (inputFile == null) {
    println("Oops, nothing happened!");
  } else {
    inputPath = inputFile.getAbsolutePath();
    inputImg = loadImage(inputPath);
    for (byte i = 0; i < 4; i++) {
      randomColors[i] = inputImg.get(int(random(0, inputImg.width)), int(random(0, inputImg.height)));
    }
  }
}
Tagged:
Sign In or Register to comment.