Skip to content

Instantly share code, notes, and snippets.

@jnsdbr
Created January 19, 2018 08:03
Show Gist options
  • Save jnsdbr/f4ebbaae7273bd17cd6aaf4ba3d8a583 to your computer and use it in GitHub Desktop.
Save jnsdbr/f4ebbaae7273bd17cd6aaf4ba3d8a583 to your computer and use it in GitHub Desktop.
PFont font;
void setup()
{
size(500, 600);
background(200);
font = createFont("Courier", 20);
textFont(font);
ArrayList<Palette> palettes = getTopPalettes();
int y = 20;
for (Palette p: palettes)
{
for (int i = 0; i < p.colorCount; i++)
{
fill(p.colors[i]);
ellipse((i + 1) * 20, y, 20, 20);
}
text(p.title, 120, y + 7);
y += 25;
}
}
ArrayList<Palette> getTopPalettes()
{
ArrayList<Palette> palettes = new ArrayList<Palette>();
JSONArray jsonPalettes = loadJSONArray("http://www.colourlovers.com/api/palettes/top?format=json");
for (int i = 0; i < jsonPalettes.size(); i++)
{
JSONObject p = jsonPalettes.getJSONObject(i);
JSONArray extractedColors = p.getJSONArray("colors");
int[] colors = new int[extractedColors.size()];
for (int j = 0; j < extractedColors.size(); j++)
{
colors[j] = unhex("FF" + extractedColors.getString(j));
}
Palette palette = new Palette(colors, p.getString("title"));
palettes.add(palette);
}
return palettes;
}
public class Palette
{
public int[] colors;
public int colorCount;
public String title;
public Palette(int[] colors, String title)
{
this.colors = colors;
this.colorCount = colors.length;
this.title = title;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment