Random Color Palettes with colorLib

Posted by Jan Vantomme on 13 April 2011.
Tags: ,

I’ve just released colorLib v1.0 today. The new version has some bug fixes, better documentation and a new RandomPalette class. In this short tutorial, I’m going to show you how to use this class.

The first thing you need to do is to declare a RandomPalette object, right before the setup() function.

RandomPalette p

Inside the setup() function, you need to call the RandomPalette() constructor to actually create the object. There are two constructors available. The standard one creates a Palette with 5 random swatches, the second one lets you choose how many swatches you want.

// generate a Palette with 5 random colors
= new RandomPalette(this);
// generate a Palette with 7 random colors
= new RandomPalette(this7); 

Regenerate the Palette

If you are not happy with the random colors, there is a method to regenerate the Palette. You can call the randomSwatches() method and a new Palette will be generated.


Adding Colors

There are two methods to add extra random colors to the Palette: addRandomColor() adds one color to the end of the Palette, addRandomColors(int numSwatches) lets you decide how many colors you want to add.

// add one color to the end of the Palette
// add two colors to the end of the Palette


You can download the example for the RandomPalette right here from the blog. colorLib v1.0 is available from Google Code.

Tweet this article