Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Change colour of certain characters in a text layer

  • Change colour of certain characters in a text layer

    Posted by Stefan Zerdzinski on October 26, 2016 at 11:06 am

    I’ve searched through the forum but haven’t found anything that quite works the way I would like it to.
    Basically I have a few hundred text fields and for each one the client wants the text to be grey but all T, G, I and F characters to be red.
    Is there a way to apply a quick expression to achieve this effect?

    For example one of the text fields reads:

    Rum, lime, sugar and fresh mint topped
    with soda, served over crushed ice.

    So most of the text would be grey with a few red t, g, i and f’s dotted about. I’ve been manually going through and selecting each letter and changing the colour before I figured there must be a quicker way to do this.

    Many thanks.

    Dan Ebberts replied 9 years, 6 months ago 2 Members · 7 Replies
  • 7 Replies
  • Stefan Zerdzinski

    October 26, 2016 at 2:36 pm

    I managed to achieve something although it’s not quite right.
    I added a red fill color and an expression selector to the text and added the following expression to the “amount” property:

    txt = text.sourceText;
    c = txt[textIndex];
    if (c == “t”)
    100
    else if (c == “g”)
    100
    else if (c == “i”)
    100
    else if (c == “f”)
    100
    else if (c == “T”)
    100
    else if (c == “G”)
    100
    else if (c == “I”)
    100
    else if (c == “F”)
    100
    else
    0;

    I’m sure there is an easier way to say if the text is either t,g,i or f then do this but I don’t know what it is.
    Anyway using this method it seemed to work but then I noticed there were a couple of random r’s that were red and some of the i’s weren’t.

  • Dan Ebberts

    October 26, 2016 at 4:17 pm

    I’d probably do it like this:

    c = text.sourceText[textIndex-1].toLowerCase();
    if (c == “t” || c == “g” || c == “i” || c == “f”) 100 else 0

    Dan

  • Stefan Zerdzinski

    October 26, 2016 at 4:41 pm

    Hi Dan,

    Thanks for getting back to me.
    That’s certainly a lot neater.

    Using that exact code, I’m still getting errors. Has it got anything to do with the text being on 2 lines messing up the index? If I make it so that it’s all on one line then the correct letters are highlighted.

    As you can see in the example below when it’s on 2 lines, some of the wrong letters are highlighted.

  • Dan Ebberts

    October 26, 2016 at 5:12 pm

    Ah. Try this one:

    c = text.sourceText.replace(/\r?\n|\r/g,””)[textIndex-1].toLowerCase();
    if (c == “t” || c == “g” || c == “i” || c == “f”) 100 else 0

    Dan

  • Stefan Zerdzinski

    October 27, 2016 at 8:52 am

    Weirdly it still isn’t working.
    I’ve purged the cache and restarted my computer to make sure that’s not affecting the result.

    This is the result from using your new code above. It’s the same result.

    I’ve noticed that if I change [textIndex-1] to just [textIndex] then I get the following result.

    Which makes things correct on the second line of text but wrong on the first line.
    Incidentally it works flawlessly for text that’s only on a single line.

  • Stefan Zerdzinski

    October 27, 2016 at 10:29 am

    Aha! I’ve figured out why it was messing up.
    In order to split the two lines of text up I pressed Enter. This seems to be changing something to do with the text index.

    I replaced the text with a text box and then made the text box smaller in order to force the text across 2 lines. This seems to work absolutely fine now.

    Do you know why having a… I don’t know what you’d call it… Line Break? would mess things up?

  • Dan Ebberts

    October 27, 2016 at 4:27 pm

    That is strange. It works for me for both point text with line breaks and box text.

    The issue is that line breaks count as characters when indexing into the text.sourceText array, but they get skipped for textIndex within an expression selector expression.

    Dan

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