Last active
December 17, 2015 14:09
-
-
Save ramayac/5622734 to your computer and use it in GitHub Desktop.
Un simple sketch que obtiene una paleta de colores de una manera *nada* cientifica.
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
/** | |
@description Un sketch de processing para obtener una paleta de colores de una imagen. | |
Ciertamente no es la mejor manera de hacerlo, ni la más cientifica, pero si es la más rápida. | |
@date 21/May/2013 | |
@author Ramayac | |
- Cambios en el grid. | |
*/ | |
PImage img, anlz; | |
int UMBRAL = 10000; | |
void setup() { | |
size(800, 600); | |
noLoop(); | |
String archivo = "akira1.jpg"; | |
img = loadImage(archivo); | |
anlz = loadImage(archivo); | |
anlz.resize(200, 200); | |
} | |
void draw() { | |
background(51); | |
anlz.pixels = sort(anlz.pixels); | |
int arrlargo = anlz.pixels.length; | |
ColGrup[] colgrup = new ColGrup[arrlargo]; | |
int ultimo = 0; | |
for (int i = 0; i < arrlargo ; i++) { | |
color c = anlz.pixels[i]; | |
if (i == 0) { | |
colgrup[ultimo] = new ColGrup(c, 1); | |
} | |
else { | |
ColGrup aux = colgrup[ultimo]; | |
if (aux.dist(c)) { | |
aux.contador++; | |
colgrup[ultimo] = aux; | |
} | |
else { | |
ultimo++; | |
colgrup[ultimo] = new ColGrup(c, 1); | |
} | |
} | |
} | |
List<ColGrup> lista = Arrays.asList(colgrup); | |
List<ColGrup> lista2 = new ArrayList<ColGrup>(10000); | |
for (ColGrup cg: lista) { | |
if (cg != null) { | |
lista2.add(cg); | |
} | |
} | |
lista = null; | |
println("Elementos " + lista2.size()); | |
//Primer cuadrante | |
image(img, 0, 0, 400, 200); | |
//Segundo cuadrante | |
image(anlz, 400, 0, width, 200); | |
//Tercer cuadrante | |
int idx = 0; | |
//Ordenamos por color | |
Collections.sort(lista2, new ColorComparator()); | |
for (int x = 0; x < width; x++) { | |
if (idx >= lista2.size()) { | |
break; | |
} | |
ColGrup cg = lista2.get(idx); | |
stroke(cg.c); | |
line(x, height, x, 200+cg.contador); | |
idx++; | |
} | |
//Y ahora por cantidad | |
Collections.sort(lista2, new ContadorComparator()); | |
//println(lista2); | |
stroke(0); | |
//Cuarto cuadrante | |
//int idx = 0; | |
idx = 0; | |
int step = 10; | |
for (int x = 400; x < width; x+=step) { | |
for (int y = 200; y < height; y+=step) { | |
if (idx >= lista2.size()) { | |
break; | |
} | |
ColGrup cg = lista2.get(idx); | |
fill(cg.c); | |
rect(x, y, step, step); | |
idx++; | |
} | |
} | |
} | |
void mousePressed() { | |
//redraw(); | |
//println("redibujando " + frameCount); | |
} | |
class ColGrup implements Comparable<ColGrup> { | |
color c, umbral = UMBRAL, contador = 0; | |
ColGrup(color c_, int contador_) { | |
c = c_; | |
contador = contador_; | |
} | |
Float getColor() { | |
//return brightness(c);//hue(c); | |
return hue(c); | |
//return brightness(c); | |
} | |
Integer getContador() { | |
return contador; | |
} | |
boolean dist(color c_) { | |
int c1 = c_, c2 = c; | |
int diff = c2 - c1; | |
if (abs(diff) >= umbral) { | |
return false; | |
} else { | |
c = c1; // the HACK that makes things work :) | |
return true; | |
} | |
} | |
String toString() { | |
return "color: " + c + ", cantidad: " + contador; | |
} | |
int compareTo(ColGrup o) { | |
if (this.contador == o.contador) return 0; | |
if (this.contador < o.contador) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
} | |
static class ContadorComparator implements Comparator<ColGrup> { | |
public int compare(ColGrup c1, ColGrup c2) { | |
return c1.getContador().compareTo(c2.getContador()); | |
} | |
} | |
static class ColorComparator implements Comparator<ColGrup> { | |
public int compare(ColGrup c1, ColGrup c2) { | |
return c1.getColor().compareTo(c2.getColor()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment