Skip to content

Instantly share code, notes, and snippets.

@need12648430
Created August 29, 2014 19:42
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save need12648430/67c719b8597a837d59fd to your computer and use it in GitHub Desktop.
Save need12648430/67c719b8597a837d59fd to your computer and use it in GitHub Desktop.
PokéPieChart
import java.util.Map;
import java.util.Comparator;
import java.util.Collections;
int index = 0;
PImage pokemon, current;
PGraphics pie;
HashMap<Integer,Integer> colorMap;
ArrayList<Integer> colorByCount;
ArrayList<Float> knownHues;
int totalColors = 0;
PGraphics outputImage;
int outputIndex = 0;
int outputCount = 1;
void setup(){
size(500, 500);
frameRate(30);
pokemon = loadImage("allpokemon.png");
pokemon.loadPixels();
current = createImage(80, 80, ARGB);
pie = createGraphics(100, 100);
outputImage = createGraphics(500, 500);
updateCurrent();
analyzePixels();
drawFullPie();
}
boolean end = false;
void draw(){
outputImage.beginDraw();
outputImage.endDraw();
background(0xFF);
//image(pie, width/2-pie.width/2, height/2-pie.height/2);
image(outputImage, 0, 0);
if(outputIndex < 25) {
if(end) return;
updateCurrent();
analyzePixels();
drawFullPie();
outputImage.beginDraw();
int x = (outputIndex % 5) * 100;
int y = floor(outputIndex / 5) * 100;
outputImage.image(pie, x, y);
outputImage.endDraw();
if(index < 492) {
index ++;
outputIndex ++;
} else {
outputImage.save("out/" + nf(outputCount, 2) + ".png");
outputCount ++;
end = true;
}
} else {
outputImage.save("out/" + nf(outputCount, 2) + ".png");
outputCount ++;
outputImage.clear();
outputIndex = 0;
}
}
void keyPressed(){
switch(keyCode){
case 37:
index = index > 0 ? index - 1 : 492;
break;
case 39:
index = index < 492 ? index + 1 : 0;
break;
}
updateCurrent();
analyzePixels();
drawFullPie();
}
void mousePressed() {
color c = get(mouseX, mouseY);
println("#" + hex(c, 6) + " (" +hue(c)+ ")");
}
void drawFullPie() {
pie.clear();
pie.beginDraw();
pie.noStroke();
drawChart(pie, 90);
pie.image(current,
pie.width / 2 - current.width / 2 / 2,
pie.height / 2 - current.height / 2 / 2,
current.width / 2,
current.height / 2);
pie.endDraw();
}
void drawChart(PGraphics g, float diameter) {
float currentAngle = 0;
g.fill(0x10);
g.arc(g.width/2, g.height/2, diameter + 2, diameter + 2, radians(0), radians(360));
for(int i = 0; i < colorByCount.size(); i ++) {
color c = colorByCount.get(i);
float a = 360 * (float(colorMap.get(c))/float(totalColors));
g.fill(c);
g.arc(g.width/2, g.height/2, diameter, diameter, radians(currentAngle), radians(currentAngle + a + 1));
currentAngle += a;
}
}
void analyzePixels(){
totalColors = 0;
knownHues = new ArrayList<Float>();
colorMap = new HashMap<Integer,Integer>();
current.loadPixels();
for(int i = 0; i < 80; i ++) {
for(int j = 0; j < 80; j ++) {
color c = current.pixels[j * 80 + i];
// ignore BS colors
if(alpha(c) == 0) continue;
if((c & 0xFFFFFF) == 0x000000) continue;
if((c & 0xFFFFFF) == 0x101010) continue;
if((c & 0xFFFFFF) == 0x181818) continue;
// if((c & 0xFFFFFF) == 0xFFFFFF) continue;
// add to color list
if(!colorMap.containsKey(c))
colorMap.put(c, 0);
colorMap.put(c, colorMap.get(c) + 1);
totalColors ++;
}
}
colorByCount = new ArrayList<Integer>(colorMap.keySet());
Collections.sort(colorByCount, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return colorMap.get(b) - colorMap.get(a);
}
});
}
void updateCurrent(){
int k = 80 * (index % 25), l = 80 * floor(index / 25);
current.loadPixels();
for(int i = 0; i < 80; i ++)
for(int j = 0; j < 80; j ++)
current.pixels[j * 80 + i] = pokemon.pixels[(l + j) * pokemon.width + (k + i)];
current.updatePixels();
}
@Kanandro
Copy link

151 lines of absolute magic. Thank you based PokéPieChart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment