-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelator.pde
87 lines (75 loc) · 1.81 KB
/
pixelator.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Set Colors
color[] colors = new color[24];
// Get the Image
PImage photo;
float photoWidth;
float photoHeight;
void setup() {
// Defaults
background(255);
noStroke();
// Load Image
photo = loadImage("image.jpg");
photoWidth = photo.width;
photoHeight = photo.height;
// Automatically Detect Photo Size
size(photo.width, photo.height);
// Select Color Palette
selectColors();
}
void selectColors(){
for(int i = 0; i < colors.length; i++){
colors[i] = color(random(255),random(255),random(255));
}
}
void randomColor() {
int nextColor = int(random(colors.length));
fill(colors[nextColor]);
}
color pixelCompare(color pixel){
float colorDistance[][] = new float[colors.length][2];
for(int i = 0; i < colors.length; i++){
float red1 = red(pixel);
float red2 = red(colors[i]);
float green1 = green(pixel);
float green2 = green(colors[i]);
float blue1 = blue(pixel);
float blue2 = blue(colors[i]);
colorDistance[i][0] = colors[i];
colorDistance[i][1] = deltaE(red1, red2, green1, green2, blue1, blue2);
}
float lowestDistance = 99999999.99;
color colorChoice = #FFFFFF;
for(int i = 0; i < colors.length; i++){
if(colorDistance[i][1] < lowestDistance){
lowestDistance = colorDistance[i][1];
colorChoice = colors[i];
}
}
return colorChoice;
}
void draw() {
// No Looping
noLoop();
// Draw Image
background(255);
int dimension = photo.width*photo.height;
for(int i = 0; i < dimension; i++){
photo.pixels[i] = pixelCompare(photo.pixels[i]);
}
photo.updatePixels();
image(photo,0,0);
}
// Exit on Mouse Click or Key Press
void mousePressed() {
// Save as PNG
save("desktop.png");
println("Finished.");
exit();
}
void keyPressed() {
// Save as PNG
save("desktop.png");
println("Finished.");
exit();
}