Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random change to specific color

  • Random change to specific color

    Posted by Bram Rusman on September 26, 2011 at 2:54 pm

    I have a grid of squares (layered). Now I want to change the color of the squares randomly. But it has to be a specific color. Like in the image. So it randomly changes to blue -light blue – green – blue etc. Is there a way to do this with a expression? It would be very nice if the colors are fading to the other color.

    Shannon Copfer Brace replied 5 years, 9 months ago 12 Members · 30 Replies
  • 30 Replies
  • Dan Ebberts

    September 26, 2011 at 3:45 pm

    You didn’t specify anything about timing, so this example is pretty generic. This will ease from one of your four colors to a random selection over a .2 second period every 2 seconds:


    colors = [[141,203,235,255]/255,
    [187,223,243,255]/255,
    [164,213,239,255]/255,
    [205,219,143,255]/255];
    easeTime = .2;
    segDur = 2;

    curSeg = Math.floor(time/segDur);
    t = time%segDur;
    seedRandom(curSeg,true);
    idx1 = Math.floor(random(colors.length));
    seedRandom(curSeg-1,true);
    idx0 = Math.floor(random(colors.length));
    ease(t,0,easeTime,colors[idx0],colors[idx1])

    Dan

  • Bram Rusman

    September 26, 2011 at 7:28 pm

    Thanks Dan!

    This works very good. I applied it to a color overlay. But maybe for understanding the expression better: How do I get the color values? Are the last parameters (255) based on the transparancy?

    Again, Thanks a lot!

  • Dan Ebberts

    September 26, 2011 at 7:46 pm

    Color values are 4-element RGBA arrays, with channel values between 0.0 and 1.0. I set it up so that you could specify the RGB values as 0-255 numbers, but it’s not necessary to do it that way if you already have the 0-1 values. The 4th (alpha) element doesn’t generally do anything, but it has to be there.

    Dan

  • Kevin Camp

    September 26, 2011 at 10:25 pm

    if you’d like use a color picker to set colors rather than specifying rgb values, you can key frame the color changes using an effect that has a color picker control (like the fill effect). then apply this modification of dan’s expression to the color property:
    easeTime = .2;
    segDur = 2;

    curSeg = Math.floor(time/segDur);
    t = time%segDur;
    seedRandom(curSeg,true);
    idx1 = Math.floor(random(numKeys)+1);
    seedRandom(curSeg-1,true);
    idx0 = Math.floor(random(numKeys)+1);
    ease(t,0,easeTime,key(idx0),key(idx1))

    essentially this way you are building the color array by using the color keyframes rather than typing in rgb values. this is especially helpful when somebody says, “let’s make the green a little less yellow, and maybe add an orange color in there too.”

    Kevin Camp
    Senior Designer
    KCPQ, KMYQ & KRCW

  • Mauro Carlieri

    April 8, 2014 at 12:24 pm

    HI Dan, Hi Kevin

    I just found your fantastic script!!! Very useful!!
    But it will be possible to add some randomness to the duration of the easetime and Seg Duration.

    I have to apply to many layers and it will be nice if there’s also some variation in the velocity of the transition from one color to another

    Thank you very much,
    Mauro

  • Shawn Sprockett

    August 5, 2014 at 1:41 pm

    Hey there,

    This expression worked great, but I wanted to make my transitions more gradual, so increased my easeTime and duration to 3 and 4 respectively. The problem is that now I get a jarring pop in the last instant to the color. Is there a way to ease in a more linear fashion from one color to the next?

  • Dan Ebberts

    August 5, 2014 at 4:05 pm

    I’m not sure I see exactly what you’re describing, but try changing the last line to this:

    linear(t,0,easeTime,colors[idx0],colors[idx1])

    Dan

  • Jane Nechayevsky

    March 21, 2015 at 3:34 pm

    Great script!

    Is there any way to randomize the segDur and clamp it, to change randomly but only between every .7 seconds and 1.5 seconds? Thanks!!

  • Max Arch

    October 19, 2015 at 11:54 pm

    It sound stupid but where should i put this expression.

  • Dan Ebberts

    October 20, 2015 at 1:07 am

    It should work on the Color parameter of a Fill Effect.

    Dan

Page 1 of 3

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