Function to extract colors from

Hi everyone.
I was trying to come up with a function to extract colours from the URL from to easily change colour pallets. This is an idea I’ve seen done for P5 projects, but I’d like it for Processing.

For some reason, I am able to get the colours and apply them to the background, but not to any object.
Anyone know why that might be?

String colors = "";

color[] pal;
color pallete;

void setup() {
  size(800, 800);
  pal = createPallete(colors);
void draw() {
  pallete = pal[int(random(pal.length))];
  pallete = pal[int(random(pal.length))];
  ellipse(width/2, height/2, 400, 400);

color[] createPallete(String colors) {
  String[] clrs = splitTokens(colors, "/");
  String[] c = splitTokens(clrs[2], "-");
  //String[] a = new String[c.length];
  color[] col = new color[c.length];

  for (int i = 0; i < c.length; i++) {
    //a[i] = "#"+c[i];
    col[i] = unhex(c[i]);
  return col;
void mousePressed() {

1 Like

Processing usually expects integer representations of RGB colour to be negative; colours are effectively created and treated like this:
col = -16777216 + (red << 16 | green << 8 | blue)

println(color(0)); // -16777216

However, background() seems to accept positive colour integers whereas fill() (like most other methods) doesn’t.

In fact, fill(n) with n > 255 doesn’t produce any fill!

Solution is to convert the pallete colour int into Processing-compatible int.



Thank you! I’ve managed to solve it based on your explanation. This is great


This will do the trick in your code:
col[i] = unhex("FF" + c[i]);

See references:
color \ Language (API) \ Processing 3+
Color \
Color Gradients in Processing (v 2.0) | by Jeremy Behreandt | Medium
And there are others…

You want the alpha to be 0xFF for 100% opaque after conversion instead of 0% opaque.

@micycle ,

That should be:

Which is the same as:


1 Like