Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Changing colours of a few circles at random

  • Changing colours of a few circles at random

    Posted by Bronwyn Harvey on March 25, 2014 at 12:58 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

    Emil Stefanow replied 7 years, 8 months ago 5 Members · 13 Replies
  • 13 Replies
  • Sander Van dijk

    March 25, 2014 at 3:05 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 ] )

  • Bronwyn Harvey

    March 25, 2014 at 3:36 pm

    Awesome – works great. Thank you 😀

  • Bronwyn Harvey

    March 27, 2014 at 11:54 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…

    Thanks in advance

  • Sander Van dijk

    March 27, 2014 at 4:17 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;
    };

  • Bronwyn Harvey

    March 27, 2014 at 5:33 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

  • Sander Van dijk

    March 27, 2014 at 6:19 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”);

  • Bronwyn Harvey

    March 27, 2014 at 6:44 pm

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

  • Sander Van dijk

    March 27, 2014 at 6:52 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!

  • Bronwyn Harvey

    March 28, 2014 at 2:28 pm

    Thanks!!

  • Adam Walker

    October 5, 2014 at 9:40 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?

    ie instead of snapping to the new color it fades?

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy