Skip to content

Instantly share code, notes, and snippets.

@iandioch
Created October 2, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandioch/abc39f6e11f88ddca344 to your computer and use it in GitHub Desktop.
Save iandioch/abc39f6e11f88ddca344 to your computer and use it in GitHub Desktop.
Finds the average of a number of colours. Makes interesting palettes.
color a = color(255, 152, 145);
color b = color(52, 81, 255);
void setup(){
size(500,500);
}
void draw(){
fill(a);
noStroke();
rect(0,0, width/3f, height);
fill(b);
rect(width*2/3f, 0, width/3, height);
fill(mean(a, b));
rect(width/3f, 0, width/3f, height);
}
void mouseReleased(){
b = color(random(255), random(255), random(255));
}
void keyReleased(){
a = color(random(255), random(255), random(255));
}
int mean(color a, color... vals){
float rsum = red(a);
float gsum = green(a);
float bsum = blue(a);
for(int i = 0; i < vals.length; i ++){
rsum += red(vals[i]);
gsum += green(vals[i]);
bsum += blue(vals[i]);
}
int n = 1 + vals.length;
float rmean = rsum/n;
float gmean = gsum/n;
float bmean = bsum/n;
return color(rmean, gmean, bmean);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment