## Colour Control Key Frames

Moderators: Disciple, zlovatt

dlewyb
Posts: 7
Joined: January 17th, 2013, 10:41 pm
Hi,
I'm trying to link up several Expression Control / Color Controls to keyframes on the colour value of an object.

key(1).time = effect("Color 1")("Color");
key(2).time = effect("Color 2")("Color");
key(3).time = effect("Color 3")("Color");
key(4).time = effect("Color 4")("Color");

What's the next step to get this to work?
Any help would be great, thanks!

Cheers,
Daniel
beginUndoGroup
Posts: 80
Joined: November 27th, 2012, 6:41 am
The things on left hand side are defined by the keyframes and cannot be overwritten, at least not explicitely as you wrote it.
Beside, the left hand side is a time (number), while the right hand side is a color (array of 3+1 numbers), so it's also hard to understand what you want to achieve!

Xavier.
dlewyb
Posts: 7
Joined: January 17th, 2013, 10:41 pm
Hi Xavier,
I'd like to define the colour value array of each key frame of an animated color property with color control (expression controller effect).
Thanks
beginUndoGroup
Posts: 80
Joined: November 27th, 2012, 6:41 am
Ah ok. This should work:

Code: Select all

``````if (numKeys<1){
value;
}
else{
idx1 = nearestKey(time).index;
if (key(idx1).time>time) idx1--;
idx2 = idx1+1;
if (idx1<1) idx1=1 else if (numKeys<idx2) idx2=numKeys;
val1 =  effect("Color " + idx1)("Color").value;
val2 =  effect("Color " + idx2)("Color").value;

linear(time, key(idx1).time, key(idx2).time, val1, val2);
};``````
dlewyb
Posts: 7
Joined: January 17th, 2013, 10:41 pm
OOOH YEAHHH that is very cool!!!! I love how those few lines of very smart code can do so much!
Thanks heaps BUG!!! If you ever come to Melbourne I'll buy you a drink / food / AFL game.
Could I substitute another term for "linear" if I wanted the colours to change immediately rather than fading or interpolating between the values.
I did try "step" with no success.
Thanks so much for your help!
Cheers!
beginUndoGroup
Posts: 80
Joined: November 27th, 2012, 6:41 am
No there is no such function. In the notations above to get a step behaviour that would be just : val1
That is, replace the line with the linear thing with:

Code: Select all

``````if (myConditionForStepBehaviour){
val1;
}
else{
linear(time, key(idx1).time, key(idx2).time, val1, val2);
};``````
where myConditionForStepBehaviour (boolean) could be the value of a checkbox control for instance.

Edit, : if you dont need linear at all, this should be simpler (faster)

Code: Select all

``````if (numKeys<1){
value;
}
else{
k = nearestKey(time);
idx = (k.index>1 && k.time>time) ? k.index-1 : k.index;
try{effect("Color " + idx)("Color").value}catch(e){value;};
};``````
dlewyb
Posts: 7
Joined: January 17th, 2013, 10:41 pm
Once again BUG sensational work! I don't know how to hook up the the check box control for linear / step option, but I'll go try to find out how as that would handy.
Is there an easy way to add a loopOut() function to this is expression?
Many thanks again!
beginUndoGroup
Posts: 80
Joined: November 27th, 2012, 6:41 am
HI,

this is another way using markers.
If you have the possibility to use markers (ie they are not busy for something else) i think it's better, it allows to queue colors in arbitrary order,
like Color 1 - Color 2 - Color 1 - Color 6 - Color 4, etc
Markers comment must be 1, or 2, or 3, etc (ie number) or, for the last marker only : *, to specify to loop out.
Any other comment will be interpreted as "1" (ie: the color will be Color 1, if the effect "Color 1" is found).

Xavier

Code: Select all

``````// Requires : layer markers to carry comments (numbers, same as the numbers of the color effects : Color 1, Color 2 etc)
// If the last marker comment is exactly "*", the color animation will loop out continuously.
// If there is a checkbox control called "Linear Color Transitions": possibility to do linear transitions between colors

function getVal(j){try{j = parseInt(m.key(j).comment); if (isNaN(j) || j<1) j=1; val = effect("Color "+j)("Color").value;}catch(e){val=value;}; return val;};

m = marker;
M = m.numKeys;
LOOP_OUT = M>0 && m.key(M).comment==="*";
numValues = LOOP_OUT ? M-1 : M;

if (numValues<2 || time<=m.key(1).time){
getVal(1);
}
else{
t = time;
if (LOOP_OUT){
t1 = m.key(1).time;
t2 = m.key(M).time;
if (t>=t2){t = t1+ ((t-t1)%(t2 - t1));};
};

idx1 = m.nearestKey(t).index;
if (t<m.key(idx1).time) idx1--;
if (LOOP_OUT && idx1===M) idx1 = 1;
val1 =  getVal(idx1);

try{doLinear = effect("Linear Color Transitions")(1).value;} catch(e){doLinear=false};

if (doLinear){
idx2 = idx1===M ? idx1 : idx1+1;
val2 =  getVal(idx2);
ease(t, m.key(idx1).time, m.key(idx2).time, val1, val2);
}
else{
val1;
};
};``````
dlewyb
Posts: 7
Joined: January 17th, 2013, 10:41 pm
Sensational work BUG. Thanks again. I'm going to message you.
Cheers
Daniel