Skip to content
Jan Vantomme edited this page Aug 29, 2014 · 2 revisions

A Swatch is the most basic block in colorLib. It is basically an object that stores a color. Let's take a look at how swatches work. The following line of code creates a swatch. Since we didn't add a color as a parameter in the constructor, the swatch will be red.

Swatch s1 = new Swatch( this );

If you want to create a swatch with a specific color, you just need to add a color variable as the second parameter in the constructor.

Swatch s2 = new Swatch( this, color( 255, 125, 0 ) );

If you want to use the color stored in a Swatch object you can get it by using the getColor()method. The following code draws two rectangles, filled with the colours from the swatches we have created.

background( 255 );
fill( s1.getColor() );
rect( 10, 10, 85, 180 );
fill( s2.getColor() );
rect( 105, 10, 85, 180 );

This is the full code for the sketch, you can also find it in the examples folder that comes with the colorLib download.

import colorlib.webservices.*;
import colorlib.tools.*;
import colorlib.*;

Swatch s1;
Swatch s2;

void setup()
{
	size( 200, 200 );
	smooth();
	noStroke();
	
	s1 = new Swatch( this );
	s2 = new Swatch( this, color( 255, 125, 0 ) );
}

void draw()
{
	background( 255 );
	
	fill( s1.getColor() );
	rect( 10, 10, 85, 180 );
	
	fill( s2.getColor() );
	rect( 105, 10, 85, 180 );
}

If you run the code, you should get to see a red and an orange rectangle on a white background. Just like in the image below.

Screenshot of the Swatch example

Additionally, there are some methods to manipulate the color.

// TODO: Write more about this...

Clone this wiki locally