FORUMS: list search recent posts

Changing colours of a few circles at random

FAQ   •   VIEW ALL
 Changing colours of a few circles at random on Mar 25, 2014 at 12:58:20 pm

Hello All,

I would like to change a few colour circles in the below picture at random. I'm fairly new to After effects but understand Expressions - which I gather I would have to use to do this.
I need to do this over a few seconds and they need to be a straight change in colour - Not a fade.
I'm not sure how tricky this is.
Any help would be greatly appreciated.

Thanks,

Bronwyn

 Re: Changing colours of a few circles at randomon Mar 25, 2014 at 3:05:47 pm

Hey Bronwyn,

You can make your image grayscale and overlay solids with fill effects.
Then apply this expression to the color property of that fill effect to make it change color over time.
You can change the var cycle to say how many time per second you want to change to another random color.

You can create sliders for all the vars so you can change the values for multiple fills.
thisComp.layer(" control layer ").effect("Slider Control")("Slider");

S.

var cycle = 5; // how many times to change per second;
var randomSeed = 2; //random seed

seed = Math.floor(time*cycle)*randomSeed;
seedRandom( seed , true );

var myColor = Math.floor( random(0,255))/255;
var mySaturation = .5
var myLlightness = .5
var myAlpha = 1;

hslToRgb( [ myColor, mySaturation, myLlightness , myAlpha ] )

```var cycle = 5; // how many times to change per second; var randomSeed = 2; //random seed seed = Math.floor(time*cycle)*randomSeed; seedRandom( seed , true ); var myColor = Math.floor( random(0,255))/255; var mySaturation = .5 var myLlightness = .5 var myAlpha = 1; hslToRgb( [ myColor, mySaturation, myLlightness , myAlpha ] )```

 Re: Changing colours of a few circles at randomon Mar 25, 2014 at 3:36:06 pm

Awesome - works great. Thank you :D

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 11:54:38 am

Hello again.

Solution working great thanks -
Is there also a way to limit the colours to 5 specific colours?
So if I am given a brand guideline I can colour pick those brand colours and go with them...

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 4:17:02 pm

Hello Bronwyn,

1. Create a layer with the name control in your comp.
2. Apply two sliders to the control layer, one called seed the other called cycle
3. Then drag 5 ( or more ) color control effects on the control layer.
4. Then copy this new code into the fill effect color

The Fill effect will now pick a random color from the color controls available. You can also turn the color effect off to disable that particular color.

S.

try{
var controlLayer = thisComp.layer("control");
var mySeed = controlLayer.effect("seed")("Slider");
var myCycle = controlLayer.effect("cycle")("Slider");
var numEffects = controlLayer.effect(1).propertyGroup(1).numProperties;

var activeColors = [];
var ListValue = 0;
var numColorsActive = 0;

for ( var n=1; n <= numEffects; n++){
if ( controlLayer.effect(n)(1).name == "Color" && controlLayer.effect(n).active ){
activeColors [ ListValue ] = n;
ListValue++;
numColorsActive +=1;
};
}

var seed = Math.floor( (time-inPoint)*myCycle )+mySeed;
seedRandom( seed , true );

generateRandom = Math.floor( random( 0,numColorsActive ) );
var pickColor = activeColors[ generateRandom % numColorsActive ];
controlLayer.effect( pickColor )("Color");

}catch(err){
value;
};

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 5:33:11 pm

Brill thanks for this.

When I apply the expression to the fill effect - It only seems to stay on one colour.
The second "color change" choice in the control layer.

So it's reading the control layer but it seems to be stuck on number 2.

Any Ideas?

Thanks

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 6:19:11 pm

Not sure why, make sure the names of the control layer and its effects are lowercase. exactly as the image shows.
Can you share a screenshot of your setup?

try this:

var controlLayer = thisComp.layer("control");
var mySeed = controlLayer.effect("seed")("Slider");
var myCycle = controlLayer.effect("cycle")("Slider");
var numEffects = controlLayer.effect(1).propertyGroup(1).numProperties;

var activeColors = [];
var ListValue = 0;
var numColorsActive = 0;

for ( var n=1; n <= numEffects; n++){
if ( controlLayer.effect(n)(1).name == "Color" && controlLayer.effect(n).active ){
activeColors [ ListValue ] = n;
ListValue++;
numColorsActive +=1;
};
}

var seed = Math.floor( (time-inPoint)*myCycle )+mySeed;
seedRandom( seed*index , true );

generateRandom = Math.floor( random( 0,numColorsActive ) );
var pickColor = activeColors[ generateRandom % numColorsActive ];
controlLayer.effect( pickColor )("Color");

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 6:44:29 pm

All seems to look the same but could be overlooking something simple as I'm fairly new

 Re: Changing colours of a few circles at randomon Mar 27, 2014 at 6:52:07 pm

I might know what is going on. Set the cycle slider to for example 5 to make it pick a new random color 5 times per second.
if you don't like the picked colors you can increase the Seed slider.

Hope that works!

 Re: Changing colours of a few circles at randomon Mar 28, 2014 at 2:28:58 pm

Thanks!!

 Re: Changing colours of a few circles at randomon Oct 5, 2014 at 9:40:15 am

HI

Thanks for putting this script up, i have been trying to make something similar but am struggling with exactly how expressions work.

is there any chance you would be able to add in a fade element to this script?

 Re: Changing colours of a few circles at randomon Oct 5, 2014 at 5:03:40 pm

Maybe like this:

dur = 1; //duration of each color (seconds)
seed = Math.floor((time-inPoint)/dur)+1113;
t = (time-inPoint)%dur;
mySaturation = .5
myLlightness = .5
var myAlpha = 1;
seedRandom(seed,true);
curColor = hslToRgb([random(1), mySaturation, myLlightness , myAlpha ]);
seedRandom(seed+1,true);
nextColor = hslToRgb([random(1), mySaturation, myLlightness , myAlpha ]);

Dan

 Re: Changing colours of a few circles at randomon Oct 8, 2014 at 4:45:42 am

Thanks Dan

that worked great.

i have another effect that im trying to create that i would be keen to get your advice on.

basically what i have is a background comp similar to the example at the start of this topic (round circles, but i have 60 of them) i want to start with all circles black (off) and then randomly select them and turn them "on" (to a random colour) one at a time, they then stay at their random colour until all 60 have turned on, then they do the reverse (turn off to black one at a time)

any ideas how i can achieve this with an expression?, or at least an idea of where i should start?