An Introduction to colorLib

Posted by Jan Vantomme on 05 November 2010.
Tags: , ,

Some time ago, Andreas Köberle and I made a library for Processing to fetch colors from web services like Adobe Kuler and Colourlovers so we can use them in our work. You can also create color palettes from images and .cs or .act files with this library. I’m going to cover the basics of using colorLib in this article.

If you don’t have colorLib yet, you can download it on Google Code and install it in the libraries folder in your Processing sketchbook. The next thing you need to do is import the library to your sketch so you can start using it.

import colorLib.calculation.*;
import colorLib.*;
import colorLib.webServices.*; 

Creating a Palette

The most basic method to create a palette is to declare a Palette variable before and initialize the object in the setup() method. Right after you’ve initialized the Palette object, you can start adding colors with the addColor() method.

Palette p;
void setup()
{
    size
(450300);
    
= new Palette(this);
    
p.addColor(color(2551060));

I usually move the task of creating a palette and filling it with colors to a separate function to keep my setup() method a little cleaner. This also makes the management of colors throughout my sketches a little easier. If you add this function to a separate tab in Processing, you’ll also be able to reuse this palette in other sketches.

void setupColors()
{
    p 
= new Palette(this);
    
p.addColor(color(255106,   0));
    
p.addColor(color71,  69,  80));
    
p.addColor(color(164223,  36));
    
p.addColor(color19,  34,  41));
    
p.addColor(color95133117));

Using the Palette in your Sketch

If you want to get a color from your new palette, you need to call the getColor() method. This method requires an integer to get a color at a certain position. If you want the first color of the palette, you’ll use p.getColor(0);. A handy method to loop through all colors of the palette is the totalSwatches() method.

for (int i 0p.totalSwatches(); i++) {
    fill
(p.getColor(i));
    
rect(i*90090height);

Running the sketch should give you the following result.

A simple color palette created with colorLib

Sorting Colors

colorLib also provides a few methods to sort the colors in your palette by hue, saturation, luminance or proximity. To sort colors by hue, you simply call the sortByHue() method. The image below shows you the original palette and the same palette sorted by hue.

A color palette created with colorLib and the same palette sorted by hue.

Download

Download the full code for this tutorial: colorlib_basics.zip.

Tweet this article

Oldskool Comments (6)

Gravatar for Mark Webster

From: Mark Webster
Date: 15.11.2010

Hi there Jan, thanks for writing up this tutorial on colorLib. I'm trying to find further documentation as I'd like to use the library with the Kuler API. I haven't found much thougth. Perhaps a short tutorial on getting colors and sorting them from Kuler would be an excellent follow up to this. regards Mark

Top · Permanent link to this comment

Gravatar for Jan Vantomme

From: Jan Vantomme
Date: 15.11.2010

Mark, I'm writing some other tutorials on colorLib. I'll probably publish the one with examples for Kuler tomorrow. I'm also planning an article for the ColourLovers API and the Gradient class in colorLib. Jan

Top · Permanent link to this comment

Gravatar for Mark

From: Mark
Date: 16.11.2010

Hi there Jan, thank you for the quick reply. That's great news! regards Mark

Top · Permanent link to this comment

Gravatar for Alex

From: Alex
Date: 09.12.2010

First of all thank you for your library. I've problems with the examples you provide in beta1: is it possible that there are missing constructors? i.e. in Palette the constructor Palette(Palette, String) is missing. A few examples work. Was there a syntax change? Tutorial is working by the way! Alex

Top · Permanent link to this comment

Gravatar for Alex

From: Alex
Date: 09.12.2010

I sent you the message and soon after understood that there is something strange here. I installed the library and went soon inside its example folder. Starting from there most examples don't work. Copying example's source in a new sketch works perfectly. It looks like there is a path missing somewhere. Sorry for my "complains" :-) Have a nice day Alex

Top · Permanent link to this comment

Gravatar for Jan Vantomme

From: Jan Vantomme
Date: 09.12.2010

I think something must have gone wrong when I collected all the files from the subversion server to release the library. I'll try to fix it when I'll release the next version of colorLib.

Top · Permanent link to this comment