Skip to content

Instantly share code, notes, and snippets.

@coleww
Created March 7, 2014 02:55
Show Gist options
  • Save coleww/9404486 to your computer and use it in GitHub Desktop.
Save coleww/9404486 to your computer and use it in GitHub Desktop.
Color picker for Processing
//TIRED OF INT(RANDOM(0, 255)) ALL OVER THE PLACE?
//get an array of colors from an image
//takes path to image, num of colors to grab, and whether to pick an even distribution or at random
color[] pickColors(String path, int numColors, boolean ordered) {
color[] colors = new color[numColors];
PImage img = loadImage(path);
img.loadPixels();
int numPixels = img.width * img.height;
int iterator = numPixels / numColors;
for (int i = 0; i < numColors; i ++) {
if (ordered) {
colors[i] = img.pixels[i * iterator];
}
else {
colors[i] = img.pixels[int(random(0, numPixels))];
}
}
return colors;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment