Activity › Forums › Adobe After Effects › Random Color Array Selection
-
Random Color Array Selection
Posted by Assaf Tshuva on November 30, 2015 at 10:14 amHi,
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.)Thanks in advance.
Assaf Tshuva replied 10 years, 9 months ago 2 Members · 9 Replies -
9 Replies
-
Walter Soyka
November 30, 2015 at 11:40 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
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn] -
Assaf Tshuva
November 30, 2015 at 1:30 pmI 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?
-
Walter Soyka
November 30, 2015 at 2:12 pmSorry, 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
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn] -
Assaf Tshuva
November 30, 2015 at 3:04 pmThanks, 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. -
Walter Soyka
November 30, 2015 at 3:12 pmindices 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
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn] -
Assaf Tshuva
November 30, 2015 at 3:46 pmI must have been doing something wrong…
(“Colors” layer contains “fill” effect to hold different colors, the effects are renamed 1-4 to be accessed from the index variables’ values)
-
Assaf Tshuva
November 30, 2015 at 3:58 pm[Dave LaRonde] “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.”
True, I know I’m going the (very) harder way, but I have a lot of upcoming projects that need this type of randomness, so I prefer having it written once correctly and use it later, instead of making pseudo-random every time.
-
Walter Soyka
November 30, 2015 at 4:09 pmEffects on a layer are 1-origin-indexed, not 0-origin-indexed like JavaScript arrays. Remove the -1 operation from the “index0 = …” expressions.
Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn]
Reply to this Discussion! Login or Sign Up