ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Random change to specific color

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bram Rusman
Random change to specific color
on Sep 26, 2011 at 2:54:22 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.


Return to posts index

Dan Ebberts
Re: Random change to specific color
on Sep 26, 2011 at 3:45:23 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



Return to posts index

Bram Rusman
Re: Random change to specific color
on Sep 26, 2011 at 7:28:47 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!


Return to posts index


Dan Ebberts
Re: Random change to specific color
on Sep 26, 2011 at 7:46:08 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



Return to posts index

Kevin Camp
Re: Random change to specific color
on Sep 26, 2011 at 10:25:26 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


Return to posts index

mauro carlieri
Re: Random change to specific color
on Apr 8, 2014 at 12:24:26 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



Return to posts index


Shawn Sprockett
Re: Random change to specific color
on Aug 5, 2014 at 1:41:49 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?


Return to posts index

Dan Ebberts
Re: Random change to specific color
on Aug 5, 2014 at 4:05:51 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



Return to posts index

Ohad Sitton
Re: Random change to specific color
on Sep 28, 2016 at 2:38:42 pm

hi dan thaks for a great tip. i was trying to use it but could not get a linear color transition whit out easing between colors. i copy paste the last line and replace that to linear as you suggeted but steel get the same issue with as the colors are easing from one to another.
any suggestions?
thanks.


Return to posts index


Jane Nechayevsky
Re: Random change to specific color
on Mar 21, 2015 at 3:34:30 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!!


Return to posts index

max arch
Re: Random change to specific color
on Oct 19, 2015 at 11:54:01 pm

It sound stupid but where should i put this expression.


Return to posts index

Dan Ebberts
Re: Random change to specific color
on Oct 20, 2015 at 1:07:45 am

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

Dan



Return to posts index


Moniek van Adrichem
Re: Random change to specific color
on Nov 12, 2015 at 10:39:37 pm

Hi Dan,

I was wondering if it would be possible to make it so that the squares all randomly get a color and duration assigned (which is different for each square). But that you can set the colors and range for the duration in a Control null, so that you can easily adjust that.

Thanks a lot!

Moniek

--
http://www.megamoniek.nl


Return to posts index

Dan Ebberts
Re: Random change to specific color
on Nov 13, 2015 at 12:40:26 am

Something like this maybe:

ctrl = thisComp.layer("Control");
c1 = ctrl.effect("Color Control")("Color");
c2 = ctrl.effect("Color Control 2")("Color");
c3 = ctrl.effect("Color Control 3")("Color");
c4 = ctrl.effect("Color Control 4")("Color");
colors = [c1,c2,c3,c4];
minDur = ctrl.effect("Min Duration")("Slider");
maxDur = ctrl.effect("Max Duration")("Slider");
seedRandom(index,true)
segDur = random(minDur,maxDur);
easeTime = .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



Return to posts index

Moniek van Adrichem
Re: Random change to specific color
on Nov 15, 2015 at 10:57:17 pm

Thanks so much, that works! So now I wanted to put the color values in the source text of a textlayer, so I could later decide to add or delete a color. Problem is that I cannot get my head around how to do this...

--
http://www.megamoniek.nl


Return to posts index


Dan Ebberts
Re: Random change to specific color
on Nov 16, 2015 at 2:07:34 am

It depends a lot on how you want to represent the colors.

Dan



Return to posts index

Moniek van Adrichem
Re: Random change to specific color
on Nov 16, 2015 at 7:24:50 am

I thought the most convenient way would be to use an array of arrays like this:

[[141,203,235,255]/255,
[164,213,239,255]/255,
[205,219,143,255]/255]

But if its easier to use the 0-1 values that's fine too :)


Return to posts index

Dan Ebberts
Re: Random change to specific color
on Nov 16, 2015 at 5:19:42 pm

If your text layer text is like this:

[[141,203,235,255]/255,[164,213,239,255]/255,[205,219,143,255]/255]


then something like this should give you your color array:

colors = eval(thisComp.layer("your text layer").text.sourceText.value);

Dan



Return to posts index


Moniek van Adrichem
Re: Random change to specific color
on Nov 16, 2015 at 6:54:20 pm

That's what I thought as well, but somehow that gives this error (which refers to the line of idx1=Math.floor(random(colors.length))):

property or method named "length" in Class "Number" is missing or does not exist. It may have been renamed, moved, deleted, or the name may have been mistyped.

The weird thing is that I don't get this error when putting the numbers directly in the expression.

ctrl = thisComp.layer("colours");

colors=eval(ctrl.text.sourceText.value);
minDur = ctrl.effect("minDuration")("Slider")
maxDur = ctrl.effect("maxDuration")("Slider");
seedRandom(index,true)
segDur = random(minDur,maxDur);
easeTime = ctrl.effect("easeDuration")("Slider");

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])


Return to posts index

Moniek van Adrichem
Re: Random change to specific color
on Nov 16, 2015 at 7:29:08 pm

When evaluating the expression in another Source Text I found that somehow the square brackets which define the color-arrays are removed when using

ctrl = thisComp.layer("colours");
colors=ctrl.text.sourceText.value;


So that might be the problem


Return to posts index

Moniek van Adrichem
Re: Random change to specific color
on Nov 16, 2015 at 8:08:40 pm

Woohoo! I figured it out, the problem was that you get a string from the Source Text, so you first need to convert that to an Array. Also I had to make a workaround on the fact that the square brackets in the middle get removed. I guess there's a more elegant solution, but this works :D

ctrl = thisComp.layer("colours");

colorstring=ctrl.text.sourceText;
colors=colorstring.split(",");
minDur = ctrl.effect("minDuration")("Slider")
maxDur = ctrl.effect("maxDuration")("Slider");
seedRandom(index,true)
segDur = random(minDur,maxDur);
easeTime = ctrl.effect("easeDuration")("Slider");

curSeg = Math.floor(time/segDur);
t = time%segDur;
seedRandom(curSeg,true);
idx1 = (Math.floor((random(colors.length))/4))*4;
seedRandom(curSeg-1,true);
idx0 = (Math.floor((random(colors.length))/4))*4;
color0 = colors.slice(idx0,(idx0+4));
color1 = colors.slice(idx1,(idx1+4));
ease(t,0,easeTime,color0,color1)


Return to posts index


Jason Jantzen
Re: Random change to specific color
on May 24, 2016 at 4:07:47 am

Hey Dan, this helped solve a huge headache I was dealing with tonight, but now I'm wondering if it's possible to randomize the segment time for each square? Thank you for all your amazing help!

Jason Jantzen
vimeo.com/jasonj


Return to posts index

Dan Ebberts
Re: Random change to specific color
on May 24, 2016 at 4:26:59 am

If I understand what you're asking, I think you could just replace this:

segDur = 2;

with this:

minDur = 1.5;
maxDur = 2.5;
seedRandom(index,true);
segDur = random(minDur,maxDur);



Dan



Return to posts index

Jason Jantzen
Re: Random change to specific color
on May 24, 2016 at 1:43:34 pm

I thought it would be something like that based on your Random Motion examples on your site. I just can't speak expressions well enough to make it work. You are the best, Dan! Thanks!

Jason Jantzen
vimeo.com/jasonj


Return to posts index


Emil Stefanow
Re: Random change to specific color
on Aug 28, 2018 at 7:30:40 am

Wow, Dan!
This expression is amazing! Thanks so much! For my purposes I have even defined over 15 colors!

I have inserted it onto the Fill Effect (Color) of a Solid in a Comp named "Comp A".
I also have Comp B, Comp C, Comp D, Comp E and Comp F with the same setup (Solid with the same expression mentioned). The expression works like a charm.

Is it howerer possible to tweak the expression(s) so that the random colors are NEVER the same at any given frame? The use the same pre-defined colors but are never the same. With seedRandom it probably doesn't work, because it's still randomness.

Thank you!
Emil

C1=[255,255,255,255];
C2=[252,249,245,255];
C3=[208,209,203,255];
C4=[225,241,236,255];
C5=[234,204,182,255];
C6=[251,243,234,255];
C7=[48,39,37,255];
C8=[226,236,176,255];
C9=[98,150,54,255];
C10=[253,210,121,255];
C11=[111,33,73,255];
C12=[144,26,126,255];
C13=[118,72,152,255];
C14=[242,134,0,255];
C15=[235,90,55,255];
C16=[174,35,43,255];
C17=[60,28,20,255];
colors = [C1/255,C2/255,C3/255,C4/255,C5/255,C6/255,C7/255,C8/255,C9/255,C10/255,C11/255,C12/255,C13/255,C14/255,C15/255,C16/255,C17/255];
easeTime =comp("Comp 1").layer("CTRL").effect("easeTime")("Slider");
segDur = comp("Comp 1").layer("CTRL").effect("segDur")("Slider");

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])



Return to posts index

Dan Ebberts
Re: Random change to specific color
on Aug 28, 2018 at 4:10:19 pm

I think this will work:


C1=[255,255,255,255];
C2=[252,249,245,255];
C3=[208,209,203,255];
C4=[225,241,236,255];
C5=[234,204,182,255];
C6=[251,243,234,255];
C7=[48,39,37,255];
C8=[226,236,176,255];
C9=[98,150,54,255];
C10=[253,210,121,255];
C11=[111,33,73,255];
C12=[144,26,126,255];
C13=[118,72,152,255];
C14=[242,134,0,255];
C15=[235,90,55,255];
C16=[174,35,43,255];
C17=[60,28,20,255];
colors = [C1/255,C2/255,C3/255,C4/255,C5/255,C6/255,C7/255,C8/255,C9/255,C10/255,C11/255,C12/255,C13/255,C14/255,C15/255,C16/255,C17/255];
easeTime =comp("Comp 1").layer("CTRL").effect("easeTime")("Slider");
segDur = comp("Comp 1").layer("CTRL").effect("segDur")("Slider");

t = 0;
seedRandom(index,true);
idx0 = Math.floor(random(colors.length));
while (t <= time){
idx1 = (idx0 + (Math.floor(random(colors.length-1))+1))%colors.length;
t += segDur;
if (t > time) break;
idx0 = idx1;
}
ease(t,0,easeTime,colors[idx0],colors[idx1])



Dan



Return to posts index

Dan Ebberts
Re: Random change to specific color
on Aug 28, 2018 at 8:52:12 pm

I forgot about the ease. The last line should probably be like this:

ease(time,t-segDur,t-segDur+easeTime,colors[idx0],colors[idx1])


Dan



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]