Created
March 7, 2014 02:55
-
-
Save coleww/9404486 to your computer and use it in GitHub Desktop.
Color picker for Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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