Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Change font color of text that is between brackets (Inline color)

  • Change font color of text that is between brackets (Inline color)

  • Maeve Tan

    January 9, 2023 at 12:35 pm

    I have some dynamic elements I’m trying to get to work. I would like to change the font color of a word in a sentence that is between brackets. Also, the word between brackets should be the only one that changes color by a hex code. The hex code is provided by the user. So the text and text color would all be dynamic.

    e.g input text is: Hello [Friend]

    The word “friend” should then be in another color, which would be a color provided in a hex code.

    In my project, I have 3 text layers: 2 input layers which are for the text and the hex code, and 1 output layer, which will display the final result.

    At the moment I could only figure out, grabbing the input text to display in the Output layer, and the coloring.

    var inputText = thisComp.layer("Input Text").text.sourceText;
    inputText;

    This is on the Output layer, to which I added a Fill effect. The expression is on the Color property from the Fill:

    var myColor = thisComp.layer("Input Color:").text.sourceText;
    var rgb = hexToRgb(myColor);
    rgb;

    So right now the text would be displayed as: Hello [Friend]
    But what I would like is to have it display as: Hello Friend
    And the word “friend” would be in the color of the hex code that the user will input

    I hope my question is clear enough, and thanks in advance for your help!


  • Dan Ebberts

    January 9, 2023 at 6:37 pm

    <div>On your output layer, add this expression for Source Text:</div>

    txt = thisComp.layer("Input Text").text.sourceText;
    if (time < 0)
    txt
    else
    txt.replace(/[\[\]']+/g,'');

    Add a Fill Color Animator, add an Expression Selector, delete the Range Selector, set the Expression Selector’s “Based On” parameter to “Words”, and apply this expression for Amount:

    txt = text.sourceText.valueAtTime(-1);
    idx = textIndex - 1;
    txt.split(" ")[idx].indexOf("[") > -1 ? 100 : 0

    Add this expression to the Fill Color:

    hexToRgb(thisComp.layer("Input Color:").text.sourceText.value)

    Note this this will remove all square brackets from the text, but only requires that a word start with [ to get the fill color applied. You could require both [ and ] brackets, but that would be a little more complex.

  • Maeve Tan

    January 10, 2023 at 9:06 am

    Hi Dan, thank you so much! This works 🙂
    Can you explain a little bit more about what each expression does? (except for the hexToRgb, this one I understood what it does)

    TIA! 🙂

  • Dan Ebberts

    January 10, 2023 at 1:39 pm

    <div>In getting ready to write up a little explanation for the expressions, I realized that I made them way more complicated than they needed to be. The first one could be simplified to this:</div>

    txt = thisComp.layer("Input Text").text.sourceText;
    txt.replace(/[\[\]']+/g,'');

    which just grabs the input text and applies a Regular Expression to remove the bracket characters.

    The second one could be simplified to this:

    txt = thisComp.layer("Input Text").text.sourceText;
    idx = textIndex - 1;
    txt.split(" ")[idx].indexOf("[") == 0 ? 100 : 0

    which just checks each word of the input text to see if it starts with “[” and if so, applies the fill color.

  • Maeve Tan

    January 10, 2023 at 2:05 pm

    Thank you, Dan! I did notice the first one could be simplified to what you have now. Just didn’t know what

    if (time < 0)

    does.

    Thanks for the explanation and help!

  • Dan Ebberts

    January 10, 2023 at 2:35 pm

    Normally you would use the negative time trick to have a property make its pre-expression value available to other properties where they could use valueAtTime() with a negative number. However, in this case that wasn’t necessary because the pre-expression value is still available from the Input Text layer, so it was an unnecessary extra level of complexity.

  • TIneke Van Schalkwyk

    February 3, 2023 at 3:33 am

    Very helpful!

    It works great on my test text layers but when I apply it to one that has key frames (for captions) it has an error.
    Any ideas?

Viewing 1 - 7 of 7 posts

Log in to reply.

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