Color Shuffle with hex color codes

Behind the scenes, color isn’t really a type. Processing automatically converts color values into int values. That’s why the color type doesn’t work with Java code: Java doesn’t know about this color to int magic.

To get your example working, you can use Integer values instead:

import java.util.*;

Integer[] array;
List<Integer> l;

void setup() {
  size(600,600);
  array = new Integer[]{#FEF0D5, #D81E5B, #F35B68, #00BEB2, #1A5D63};
  // Shuffle the elements in the array
  l = Arrays.asList(array);
  printArray("original order: " + l);
}

void draw() {
  color c = array[0];
  background(c);
}

void keyPressed() {
  Collections.shuffle(l);
  printArray("new order:      " + l);
}

Also notice the code inside the draw() function which treats the Integer value like a color. Processing knows that color values are really int values, so this will work.

By the way, the Color type (with an upper-case C) is a Java-specific class that you should (almost) never use in Processing.

2 Likes