Skip to content

Instantly share code, notes, and snippets.

@federico-pepe
Last active August 28, 2017 22:29
Show Gist options
  • Save federico-pepe/a9098e61163dea99f198458d402c8824 to your computer and use it in GitHub Desktop.
Save federico-pepe/a9098e61163dea99f198458d402c8824 to your computer and use it in GitHub Desktop.
Una palette di colori da un'immagine
/*
* Una palette di colori da un'immagine
* Federico Pepe, 28.08.2017
* http://blog.federicopepe.com/processing
*/
PImage img;
// Numero di "punti" della nostra palette
int numPoints = 5;
// Valore x di riferimento
int pointX;
// Salviamo i valori HEX in una lista di stringhe
StringList palette = new StringList();
void setup() {
size(1, 1);
surface.setResizable(true);
img = loadImage("flowers.jpg");
surface.setSize(img.width, img.height+50);
}
void draw() {
image(img, 0, 0);
img.loadPixels();
pointX = img.width / numPoints;
palette.clear();
for (int i = 0; i <= numPoints-1; i++) {
int x = pointX/2+pointX*i;
int y = mouseY;
if (mouseY <= 0 || mouseY >= img.height) {
y = img.height/2;
}
stroke(255);
noFill();
ellipse(x, y, 25, 25);
color c = img.pixels[x + y * img.width];
fill(c);
rect(pointX*i, img.height, pointX, 50);
fill(255);
textAlign(CENTER);
text("#"+hex(c,6), x, img.height+30);
// Aggiungiamo il colore come stringa all'array
palette.append("#"+hex(c,6));
}
}
void mousePressed() {
saveStrings("palette.txt", palette.array());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment