Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions change colour fill based on opacity

  • change colour fill based on opacity

    Posted by Rosie Skinner on March 28, 2018 at 9:31 am

    Hi everybody!

    I’m a relative expressions newbie so please excuse my ignorance with this…

    I want to have the colour of a layer change based on the opacity of another layer. I want there to be 4 colours so I used an if else expression… can someone please tell me where I’m going wrong… I’d really appreciate some help!

    color=effect("Fill")("Color")
    num=thisComp.layer("Colour Control Icing").effect("Icing 4")("Opacity");
    varWhite=thisComp.layer("Colour Control Icing").effect("Icing 1")("Color");
    varBrown=thisComp.layer("Colour Control Icing").effect("Icing 2")("Color");
    varPink=thisComp.layer("Colour Control Icing").effect("Icing 3")("Color");
    varBlue=thisComp.layer("Colour Control Icing").effect("Icing 4")("Color");

    if (num<25){varWhite};
    if (num>25 && num<50){varBrown};
    if (num<75 && num> 50){varPink};
    else (num>75}{varBlue};
    color;

    Rosie Skinner replied 8 years, 1 month ago 2 Members · 6 Replies
  • 6 Replies
  • Jamie Bradshaw

    March 28, 2018 at 11:17 am

    Hi.

    I think there might have been a few errors in what you were trying to achieve. If you want I can try and take you through why as I’m not all that busy at the moment.

    Try using this code in the Colour property…

    controlLayer = thisComp.layer("Colour Control Icing");
    controlOpacity = controlLayer.opacity;

    varWhite = controlLayer.effect("Icing 1")("Color");
    varBrown = controlLayer.effect("Icing 2")("Color");
    varPink = controlLayer.effect("Icing 3")("Color");
    varBlue = controlLayer.effect("Icing 4")("Color");

    if (controlOpacity < 25) {
    varWhite;
    } else if (controlOpacity < 50) {
    varBrown;
    } else if (controlOpacity < 75) {
    varPink;
    } else {
    varBlue;
    }

  • Rosie Skinner

    March 28, 2018 at 11:23 am

    Thanks so much for the help!

    Did you mean post it into the effects – fill – color property? Because I did that and it didn’t work… The error is saying Error at line 9 : Expected…?

    But also if you could explain it that’d be wonderful!

  • Jamie Bradshaw

    March 28, 2018 at 11:24 am

    hmm I forgot about that bug… I wonder if this pastes better…

    controlLayer = thisComp.layer("Colour Control Icing");
    controlOpacity = controlLayer.opacity;

    varWhite = controlLayer.effect("Icing 1")("Color");
    varBrown = controlLayer.effect("Icing 2")("Color");
    varPink = controlLayer.effect("Icing 3")("Color");
    varBlue = controlLayer.effect("Icing 4")("Color");

    if (controlOpacity < 25) {
    varWhite;
    } else if (controlOpacity < 50) {
    varBrown;
    } else if (controlOpacity < 75) {
    varPink;
    } else {
    varBlue;
    }

    JimJam•Graphics
    https://www.jimjamgraphics.com/

  • Rosie Skinner

    March 28, 2018 at 11:36 am

    Boom! That works! Thanks so much!

    Why do you not need to set a lower parameter? I would have thought that if you are telling the computer that a value under 50 should make it pink and a value under 75 should make it white then for a value of say 40 it wouldn’t know whether to choose pink or white?

    Also how would it work if I wanted to base it on a slider instead of opacity? I’ve read the slider doesn’t necessarily output a number so it confuses things?

  • Jamie Bradshaw

    March 28, 2018 at 11:54 am

    In this case you don’t need to set a lower parameter because of the ‘else’ aspect of the ‘if’ statements.

    So… lets say that the opacity is 40… It would get to this line…


    if (controlOpacity < 25) {

    …and then as 40 is not less than 25 it would jump straight to the next ‘else if’ bit of code…


    } else if (controlOpacity < 50) {

    …and as 40 is less than 50 it will execute the code…

    varBrown;

    …however, it when it gets to the next bit, there is another ‘else if’. Which is the same as saying ‘otherwise do this’. As it already met the correct conditions, it will ignore that and move on.

    Does that make sense?

    It’s pretty easy to change it to a Slider to control it, but first you’d need to know how you want that slider to behave? Do you still want it to be a value between 0-100 and it acts just the same as when you had it linked to opacity?

  • Rosie Skinner

    March 28, 2018 at 4:07 pm

    That makes sense…

    Re: the slider control… I’ve just replaced the opacity control with the slider control and it works perfectly!

    Thanks for all your help!

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