ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Random Color Array Selection

FAQ   •   VIEW ALL
 Random Color Array Selection on Nov 30, 2015 at 10:14:14 am

Hi,

I have four layers that have to change randomly every few frames to one of four colors (all four are different in every cycle).
I found an old thread of how to do a random selection from a color array, but can't figure out how to make each layer pick a color that hasn't been picked by the other layers.
(Let's say I have blue, green, red and yellow, and I want that when one layer is randomly red, the second could be only blue, green or yellow, etc.)

 Re: Random Color Array Selectionon Nov 30, 2015 at 11:40:43 am

[assaf tshuva] "I found an old thread of how to do a random selection from a color array, but can't figure out how to make each layer pick a color that hasn't been picked by the other layers."

Shuffle the array of colors, then pull from them sequentially.

Try this sample expression code on a text layer to see what it does:
```colorArray = ["red","yellow","green","blue"]; seedRandom(time); colorArray = shuffleArray(colorArray); // return the new randomly-sorted array "" + colorArray[0] + " " + colorArray[1] + " " + colorArray[2] + " " + colorArray[3]; function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } ```

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Random Color Array Selectionon Nov 30, 2015 at 1:30:19 pmLast Edited By assaf tshuva on Nov 30, 2015 at 1:30:48 pm

I understand the shuffling concept, but that's randomizing values on the same layer. Couldn't find how to "export" the values to other layers - I'm probably thinking of a "global" values array so that each layer can grab a color, but that not existing in AE (a "general expressions" controller), for all I know... or is there another trick?

 Re: Random Color Array Selectionon Nov 30, 2015 at 2:12:44 pm

Sorry, I thought that using a constant seed on multiple layers would make this deterministic, and eliminate the need for storing a single value somewhere. It does not; I'm not sure why, but I'll have to look into that.

In the meantime, here's an ugly workaround.

Create a slider control to hold a sequence of indices. Give it this expression:
```indexArray = [1,2,3,4]; indexArray = shuffleArray(indexArray); "" + indexArray[0] + indexArray[1] + indexArray[2] + indexArray[3]; function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }```

Elsewhere, you can grab the value of the slider and use some string tricks to extract a single value from it:
```indices = "" + thisComp.layer("RandomIndices").effect("randomIndices")("Slider").value; index0 = indices.substr(0,1)-1; index1 = indices.substr(1,1)-1; index2 = indices.substr(2,1)-1; index3 = indices.substr(3,1)-1; ```

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Random Color Array Selectionon Nov 30, 2015 at 3:04:01 pm

Thanks, seems like it might work, but I'm blocked in the second part:
Wherever I copy the second code, "indices" is taken as 1-dimension variable, and the slider value (even though it's a number of four digits) as a 4-dimension variable, so it pops an error and so unusable.
I tried to play with the variable/link values and look for a conversion expression but so far no good.

 Re: Random Color Array Selectionon Nov 30, 2015 at 3:12:16 pm

indices is string. Each of the index values shows you how to extract a single-digit value from the string of indices to use as an array index later. You'll want to use each of these as the index into your array of colors.

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Random Color Array Selectionon Nov 30, 2015 at 3:46:08 pm

I must have been doing something wrong...

0_error.jpg

("Colors" layer contains "fill" effect to hold different colors, the effects are renamed 1-4 to be accessed from the index variables' values)

 Re: Random Color Array Selectionon Nov 30, 2015 at 4:09:24 pm

Effects on a layer are 1-origin-indexed, not 0-origin-indexed like JavaScript arrays. Remove the -1 operation from the "index0 = ..." expressions.

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Random Color Array Selectionon Nov 30, 2015 at 4:51:54 pm

That's it. Thanks a lot!

 Re: andom Color Array Selectionon Nov 30, 2015 at 3:51:31 pm

You know, there are only 16 possible color combinations. You could make all 16, change them yourself and they would appear random...and you would not have to bother with a single line of coding.

Dave LaRonde
Promotion Producer
KGAN (CBS) & KFXA (Fox) Cedar Rapids, IA