Skip to content

Instantly share code, notes, and snippets.

@frederickk
Created May 14, 2011 01:37
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 frederickk/971583 to your computer and use it in GitHub Desktop.
Save frederickk/971583 to your computer and use it in GitHub Desktop.
A class for grabbing color values and creating palettes from http://prettycolors.tumblr.com/
/**
* Prettycolors.pde
* processing.org
*
* Ken Frederick
* ken.frederick@gmx.de
*
* http://cargocollective.com/kenfrederick/
* http://kenfrederick.blogspot.com/
*
* A class for grabbing color values and
* creating palettes from http://prettycolors.tumblr.com/
*
*/
//-----------------------------------------------------------------------------
//libraries
//-----------------------------------------------------------------------------
//import java.util.Arrays;
//import java.util.Comparator;
//import java.util.Collections;
public class Prettycolors {
//-----------------------------------------------------------------------------
//properties
//-----------------------------------------------------------------------------
private PApplet papplet;
private int total;
private int returned;
private int numToReturn = 50;
private int colorsReturn[];
//palette types
private PrettycolorsPalette paletteOrder[];
private PrettycolorsPalette paletteBrightness[];
private PrettycolorsPalette paletteComplement[];
private PrettycolorsPalette paletteRandom;
private PrettycolorsPalette paletteRandomAll;
//pallete statics
static final int ORDER = 0;
static final int BRIGHTNESS = 1;
static final int COMPLEMENT = 2;
static final int RANDOM = 3;
static final int RANDOM_ALL = 4;
//-----------------------------------------------------------------------------
//constructor
//-----------------------------------------------------------------------------
public Prettycolors(PApplet _papplet) {
papplet = _papplet;
try {
XMLElement xml = new XMLElement(papplet, "http://prettycolors.tumblr.com/api/read?num=" + numToReturn + "&filter=text");
XMLElement posts = xml.getChild(1);
total = posts.getIntAttribute("total");
returned = posts.getChildCount();
//collect all returned colors into array
colorsReturn = new int[returned];
for (int i=0; i<colorsReturn.length; i++) {
String hexs = posts.getChild(i).getChild(0).getContent();
colorsReturn[i] = getRGB(hexs);
}
//sort the collected colors into palettes
paletteOrder = new PrettycolorsPalette[ (int) returned/5 ];
paletteBrightness = new PrettycolorsPalette[ (int) returned/5 ];
paletteComplement = new PrettycolorsPalette[ (int) returned ];
paletteRandom = new PrettycolorsPalette();
paletteRandom.type = "random";
paletteRandomAll = new PrettycolorsPalette();
paletteRandomAll.type = "randomAll";
createPaletteOrder();
createPaletteBrightness();
createPaletteComplement();
createPaletteRandomAll();
} catch(Exception e) {
println("error: " + e);
println("probably because the tumblr servers are down. shocking, i know");
}
}
//-----------------------------------------------------------------------------
//methods
//-----------------------------------------------------------------------------
private int parseElement(XMLElement xml, int num) {
XMLElement post = xml.getChild(1);
int count = post.getChildCount();
if (num > count) num = count;
String hexs = post.getChild(num).getChild(0).getContent();
return getRGB(hexs);
}
//-----------------------------------------------------------------------------
/**
* create palettes (groups of 5), based on order posted
*/
private void createPaletteOrder() {
int index = 0;
for(int i=0; i<paletteOrder.length; i++) {
paletteOrder[i] = new PrettycolorsPalette();
paletteOrder[i].type = "order_" + str(i);
for(int j=0; j<paletteOrder[i].AMMOUNT; j++) {
paletteOrder[i].set(j, colorsReturn[index]);
index++;
}
}
}
/**
* create palettes (groups of 5), based on brightness
*/
private void createPaletteBrightness() {
ArrayList<Integer> colorsTemp = new ArrayList();
for(int i=0; i<colorsReturn.length; i++) {
colorsTemp.add( new Integer(colorsReturn[i]) );
}
Collections.sort(colorsTemp, new BrightnessComparator());
int index = 0;
for(int i=0; i<paletteBrightness.length; i++) {
paletteBrightness[i] = new PrettycolorsPalette();
paletteBrightness[i].type = "brightness_" + str(i);
for(int j=0; j<paletteBrightness[i].AMMOUNT; j++) {
paletteBrightness[i].set(j, colorsTemp.get(index).intValue());
index++;
}
}
}
/**
* create palettes (groups of 5), based on complements
* very very very rudimentary, if not downright wrong
*/
private void createPaletteComplement() {
findContrast(getRandom(), colorsReturn);
for(int i=0; i<paletteComplement.length; i++) {
int con = findContrast(colorsReturn[i], colorsReturn);
int sat = findSaturation(con, colorsReturn);
paletteComplement[i] = new PrettycolorsPalette();
paletteComplement[i].type = "complement_" + str(i);
paletteComplement[i].set(0, colorsReturn[i]);
paletteComplement[i].set(1, con);
paletteComplement[i].set(2, findContrast(con, colorsReturn));
paletteComplement[i].set(3, sat);
paletteComplement[i].set(4, findSaturation(sat, colorsReturn));
}
}
/**
* create palettes (groups of 5), based on color rules -- analagous, complementary, etc.
*/
//-----------------------------------------------------------------------------
/**
* create palettes (groups of 5), random returned
*/
private void createPaletteRandom() {
for(int j=0; j<paletteRandom.AMMOUNT; j++) {
paletteRandom.set(j, getRandom());
}
}
/**
* create palettes (groups of 5), random all
*/
private void createPaletteRandomAll() {
for(int j=0; j<paletteRandomAll.AMMOUNT; j++) {
paletteRandomAll.set(j, getRandom());
}
}
//-----------------------------------------------------------------------------
public void drawPalette(float x, float y, float w, float h, int type) {
drawPalette(x,y, w,h, type,0);
}
public void drawPalette(float x, float y, float w, float h, int type, int num) {
int amt = getPalette(type,num).AMMOUNT;
for(int i=0; i<amt; i++) {
fill( getPalette(type,num).get(i) );
rect(i*(w/amt),y, w/amt,h);
}
}
//-----------------------------------------------------------------------------
private int findBrightness(int colOrig, int target) {
int val = -1;
for(int i=0; i<colorsReturn.length; i++) {
int range = 5;
do {
if( brightness(colorsReturn[i]) > target-range && brightness(colorsReturn[i]) < target+range ) {
val = colorsReturn[i];
//println(target + "\t" + range + "\t" + brightness(val) + "\t" + val);
if(val != -1) break;
} else {
range+=5;
}
} while(val == -1);
}
return val;
}
private int findSaturation(int colOrig, int target) {
int val = -1;
for(int i=0; i<colorsReturn.length; i++) {
int range = 5;
do {
if( saturation(colorsReturn[i]) > target-range && saturation(colorsReturn[i]) < target+range ) {
val = colorsReturn[i];
//println(target + "\t" + range + "\t" + saturation(val) + "\t" + val);
if(val != -1) break;
} else {
range+=5;
}
} while(val == -1);
}
return val;
}
//-----------------------------------------------------------------------------
private int findContrast(int colOrig, int[] colAll) {
int target = 0;
if(brightness(colOrig) > 255*0.4) { //0.3) {
target = int(255*0.1f + brightness(colOrig) * 0.25f);
return findBrightness(colOrig, target);
} else {
target = int(255*1.0f - brightness(colOrig) * 0.25f);
return findBrightness(colOrig, target);
}
}
private int findSaturation(int colOrig, int[] colAll) {
int target = 0;
target = int(255*0.1f + saturation(colOrig) * 0.25f);
return findSaturation(colOrig, target);
}
//-----------------------------------------------------------------------------
//gets
//-----------------------------------------------------------------------------
private int getRGB(String hexs) {
//http://processing.org/bugs/bugzilla/attachments/191
int opacityMask = 0xFF000000;
return opacityMask | (Integer.parseInt(hexs.substring(1), 16)) & 0xFFFFFF;
}
//-----------------------------------------------------------------------------
public PrettycolorsPalette getPalette(int type) {
return getPalette(type,0);
}
public PrettycolorsPalette getPalette(int type, int num) {
if(type == ORDER) {
return paletteOrder[ constrain(num, 0,paletteOrder.length) ];
} else if(type == BRIGHTNESS) {
return paletteBrightness[ constrain(num, 0,paletteBrightness.length) ];
} else if(type == COMPLEMENT) {
return paletteComplement[ constrain(num, 0,paletteComplement.length) ];
} else if(type == RANDOM) {
createPaletteRandom();
return paletteRandom;
} else if(type == RANDOM_ALL) {
//put the reload into a thread?
createPaletteRandomAll();
return paletteRandomAll;
} else {
println("not a valid type");
return null;
}
}
//-----------------------------------------------------------------------------
public int getRandomAll() {
int rand = int(random(total));
try {
XMLElement xml = new XMLElement(papplet, "http://prettycolors.tumblr.com/api/read?num=1&filter=text&start=" + rand );
return parseElement(xml, 0);
} catch(Exception e) {
return 0;
}
}
public int getRandom() {
int rand = int(random(colorsReturn.length));
return colorsReturn[rand];
}
} //end Prettycolors
/**
* PrettycolorsPalette
* Palette holder class
*
*/
public class PrettycolorsPalette {
//-----------------------------------------------------------------------------
//properties
//-----------------------------------------------------------------------------
static final int AMMOUNT = 5;
private int palette[];
public String type = "";
//-----------------------------------------------------------------------------
//constructor
//-----------------------------------------------------------------------------
public PrettycolorsPalette() {
palette = new int[AMMOUNT];
}
//-----------------------------------------------------------------------------
//methods
//-----------------------------------------------------------------------------
public void save() {
String filename = type + "_" + year() + month() + day() + "_" + hour() + minute() + second() + ".png";
PImage img = createImage(AMMOUNT,1, ARGB);
img.loadPixels();
for(int j=0; j<img.pixels.length; j++) img.pixels[j] = color(palette[j]);
img.updatePixels();
img.save("palette/" + filename);
println(filename + "\tsaved!");
}
//-----------------------------------------------------------------------------
//sets
//-----------------------------------------------------------------------------
public void set(int num, int val) {
palette[num] = val;
}
//-----------------------------------------------------------------------------
//gets
//-----------------------------------------------------------------------------
public int[] get() {
return palette;
}
public int get(int num) {
return palette[ constrain(num, 0,palette.length) ];
}
} //end PrettycolorsPalette
/**
* Comparators
* color sorting methods/classes
*
* http://processing.org/discourse/yabb2/YaBB.pl?num=1272458371
*
*/
public class BrightnessComparator implements Comparator<Integer> {
public int compare(Integer _col1, Integer _col2) {
int col1 = _col1.intValue();
int col2 = _col2.intValue();
if( brightness(col1) != brightness(col2) ) {
return int(brightness(col1) - brightness(col2));
}
return int( hue(col1) - hue(col2));
}
}
Prettycolors pc;
int choose[];
PFont typeface;
void setup() {
size(500,500);
noLoop();
noStroke();
choose = new int[3];
choose[0] = (int) random(10);
choose[1] = (int) random(10);
choose[2] = (int) random(50);
pc = new Prettycolors(this);
typeface = createFont("LucidaGrande-Bold",9);
textFont(typeface);
}
void draw() {
background(0);
noStroke();
//palettes
pc.drawPalette(0,0, width,height/5, pc.ORDER, choose[0]);
pc.drawPalette(0,(height/5), width,height/5, pc.BRIGHTNESS, choose[1]);
pc.drawPalette(0,(height/5)*2, width,height/5, pc.COMPLEMENT, choose[2]);
pc.drawPalette(0,(height/5)*3, width,height/5, pc.RANDOM);
pc.drawPalette(0,(height/5)*4, width,height/5, pc.RANDOM_ALL);
//labels
fill(0);
text("Order", 15,15);
text("Brightness", 15,(height/5) +15);
text("Complement", 15,(height/5)*2 +15);
text("Random", 15,(height/5)*3 +15);
text("Random All", 15,(height/5)*4 +15);
}
/**
* pressing mouse will save the palettes as 5x1 pixel images for later use
*/
void mousePressed() {
pc.getPalette(pc.ORDER, choose[0]).save();
pc.getPalette(pc.BRIGHTNESS, choose[1]).save();
pc.getPalette(pc.COMPLEMENT, choose[2]).save();
pc.getPalette(pc.RANDOM).save();
pc.getPalette(pc.RANDOM_ALL).save();
}
/**
* pressing the spacebar will randomly create new palettes
*/
void keyPressed() {
if(key == ' ') {
choose[0] = (int) random(10);
choose[1] = (int) random(10);
choose[2] = (int) random(50);
redraw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment