Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
color similarColor(PImage img) {
color similar = #FF0000;
float dist = red_distance(img);
if(dist > green_distance(img)) {
similar = #00FF00;
dist = green_distance(img);
}
if(dist > blue_distance(img)) {
similar = #0000FF;
dist = blue_distance(img);
}
if(dist > pink_distance(img)) {
similar = #FF8080;
dist = pink_distance(img);
}
if(dist > yellow_distance(img)) {
similar = #FFFF00;
dist = yellow_distance(img);
}
return similar;
}
float red_distance(PImage img) {
float sum = 0;
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
sum += sq(255-red(img.get(i, j))) + sq(green(img.get(i, j))) + sq(blue(img.get(i, j)));
}
}
return sum / (img.width * img.height);
}
float green_distance(PImage img) {
float sum = 0;
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
sum += sq(red(img.get(i, j))) + sq(255-green(img.get(i, j))) + sq(blue(img.get(i, j)));
}
}
return sum / (img.width * img.height);
}
float blue_distance(PImage img) {
float sum = 0;
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
sum += sq(red(img.get(i, j))) + sq(green(img.get(i, j))) + sq(255-blue(img.get(i, j)));
}
}
return sum / (img.width * img.height);
}
float pink_distance(PImage img) {
float sum = 0;
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
sum += sq(255-red(img.get(i, j))) + sq(128-green(img.get(i, j))) + sq(128-blue(img.get(i, j)));
}
}
return sum / (img.width * img.height);
}
float yellow_distance(PImage img) {
float sum = 0;
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
sum += sq(255-red(img.get(i, j))) + sq(green(255-img.get(i, j))) + sq(blue(img.get(i, j)));
}
}
return sum / (img.width * img.height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment