Created
June 1, 2011 00:21
-
-
Save frederickk/1001544 to your computer and use it in GitHub Desktop.
A processing class for grabbing colors from http://kuler.adobe.com/
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
/** | |
* Kuler.pde | |
* processing.org | |
* | |
* Ken Frederick | |
* ken.frederick@gmx.de | |
* | |
* http://cargocollective.com/kenfrederick/ | |
* http://kenfrederick.blogspot.com/ | |
* | |
* A class for grabbing colors from http://kuler.adobe.com/ | |
* | |
*/ | |
public class Kuler { | |
//----------------------------------------------------------------------------- | |
//properties | |
//----------------------------------------------------------------------------- | |
private PApplet p; | |
private String serverPage = "http://kuler-api.adobe.com/rss/"; | |
private String typ; | |
private String pageTyp = ".cfm"; | |
private int maxItems = 1; | |
private int startIndex = 0; | |
private String query; | |
private String key = "5F5D21FE5CA6CBE00A40BD4457BAF3BA"; | |
private String[] KulerThemes; | |
private XMLElement[] themeItems; | |
private XMLElement themeItem; | |
private XMLElement[] themeSwatches; | |
private XMLElement[] themeTag; | |
//----------------------------------------------------------------------------- | |
//constructor | |
//----------------------------------------------------------------------------- | |
public Kuler(PApplet parent) { | |
p = parent; | |
} | |
//----------------------------------------------------------------------------- | |
//methods | |
//----------------------------------------------------------------------------- | |
public void init(String query, String typ) { | |
String url = serverPage + typ + pageTyp + "?itemsPerPage=" + maxItems + "&startIndex=" + startIndex + query + "&key=" + key; | |
String urlPrint = typ + pageTyp + "?itemsPerPage=" + maxItems + "&startIndex=" + startIndex + query; | |
ArrayList themes = new ArrayList(); | |
XMLElement xml; | |
try { | |
xml = new XMLElement( p, url.toString() ); | |
println("\n" + urlPrint.toString()); | |
println("-----------------------------------------------------------------------------"); | |
if (xml.getChild("success") != null && xml.getChild("success").getContent().equals("false")) { | |
println("The following error appears while calling kuler service:"); | |
println(xml.getChild("error/errorText").getContent()); | |
println("-----------------------------------------------------------------------------"); | |
} else { | |
themeItems = xml.getChildren("channel/item/kuler:themeItem"); | |
println(themeItems.length + " theme results were returned"); | |
/* | |
for(int i=0; i<themeItems.length; i++) { | |
themeSwatches = themeItems[i].getChildren("kuler:themeSwatches/kuler:swatch/kuler:swatchHexColor"); | |
int[] swatches = new int[themeSwatches.length]; | |
for (int j=0; j < themeSwatches.length; j++) { | |
swatches[j] = unhex("FF" + themeSwatches[j].getContent()); | |
println("swatch color " + j + " " + swatches[j]); | |
} | |
} | |
println("-----------------------------------------------------------------------------"); | |
*/ | |
} | |
} catch(Exception e) { | |
println("Kuler XML Error " + e); | |
println("-----------------------------------------------------------------------------"); | |
} | |
} | |
//----------------------------------------------------------------------------- | |
//sets | |
//----------------------------------------------------------------------------- | |
public void setMaxItems(int maxItems) { | |
this.maxItems = maxItems; | |
} | |
public void setStartIndex(int startIndex) { | |
this.startIndex = startIndex; | |
} | |
public void setKey(String key) { | |
this.key = key; | |
} | |
public void setTheme(int num) { | |
themeItem = themeItems[num]; | |
if(num > themeItems.length) { | |
println("there are only " + themeItems.length); | |
println("-----------------------------------------------------------------------------"); | |
} else { | |
themeSwatches = themeItem.getChildren("kuler:themeSwatches/kuler:swatch/kuler:swatchHexColor"); | |
//themeTag = themeItem.getChildren("kuler:themeTags"); //)[0].getContent()); | |
themeTag = themeItem.getChildren("kuler:themeTags");//[0].getContent(); | |
} | |
} | |
//----------------------------------------------------------------------------- | |
//gets | |
//----------------------------------------------------------------------------- | |
public void getSearchTag(String suche) { | |
query = "&searchQuery=tag:" + suche; | |
typ = "search"; | |
initialize(query, typ); | |
} | |
public void getRecent() { | |
query = "&listType=recent"; | |
typ = "get"; | |
initialize(query, typ); | |
} | |
public void getRecent(int num) { | |
setMaxItems(num); | |
getRecent(); | |
} | |
int getThemeCount() { | |
try { | |
return themeItems.length; | |
} catch (Exception e) { | |
println("getThemeCount() error " + e); | |
return 0; | |
} | |
} | |
color[] getSwatches() { | |
themeSwatches = themeItems[0].getChildren("kuler:themeSwatches/kuler:swatch/kuler:swatchHexColor"); | |
int[] swatches = new int[themeSwatches.length]; | |
for (int j=0; j < themeSwatches.length; j++) { | |
swatches[j] = unhex("FF" + themeSwatches[j].getContent()); | |
} | |
return swatches; | |
} | |
color[] getSwatches(int t) { | |
try { | |
themeSwatches = themeItems[t].getChildren("kuler:themeSwatches/kuler:swatch/kuler:swatchHexColor"); | |
int[] swatches = new int[themeSwatches.length]; | |
for (int j=0; j < themeSwatches.length; j++) { | |
swatches[j] = unhex("FF" + themeSwatches[j].getContent()); | |
} | |
return swatches; | |
} catch(Exception e) { | |
//println("getSwatches() error " + e); | |
int[] swatches = {}; | |
return swatches; | |
} | |
} | |
String[] getThemeTag() { | |
String[] themeTags = new String[themeTag.length]; | |
try { | |
String holder = themeTag[0].getContent().toString(); | |
String[] tagsList = split(holder, ", "); | |
return tagsList; | |
} catch(Exception e) { | |
//println("error reading tags: " + e); | |
themeTags[0] = ""; | |
} | |
return themeTags; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment