Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Automatically assign fill color to text characters

  • Automatically assign fill color to text characters

    Posted by Dolph Kawalec on August 26, 2025 at 10:15 am

    Hi!
    I’m struggling with an expression problem. I’m trying to simulate color coding of text like a password generator does (see attachment). I’ve tried to add expressions to text animators with code like this on the fill property of the animator (I’m not a coder, so blame AI if this is the issue 😊)

    // Define colors

    letterColor = [1,0,0,1]; // Red

    numberColor = [0,1,0,1]; // Green

    symbolColor = [0,0,1,1]; // Blue

    txt = text.sourceText.toString();

    c = (textIndex > 0 && textIndex <= txt.length) ? txt.charAt(textIndex-1) : “”;

    if (c.match(/[A-Za-z]/)) {

    letterColor;

    } else if (c.match(/[0-9]/)) {

    numberColor;

    } else {

    symbolColor;

    }

     

    I keep getting a “Reference Error: texIndex is not defined”

    I’ve also tried to apply an expression to the source text property with a style.setFill() with even less luck.

    Any suggestion to getting this to work? Any help would be appreciated!

     

    Brie Clayton
    replied 8 months, 3 weeks ago
    3 Members · 10 Replies
  • 10 Replies
  • Dan Ebberts

    August 26, 2025 at 4:46 pm

    Try this Source Text expression:

    letterColor = [1,0,0,1]; // Red
    numberColor = [0,1,0,1]; // Green
    symbolColor = [0,0,1,1]; // Blue
    myExpr = "style";
    for (i = 0; i < value.length; i++){
    c = value[i];
    if (c.match(/[A-Za-z]/)) {
    myExpr += ".setFillColor(letterColor," + i + ",1)";
    } else if (c.match(/[0-9]/)) {
    myExpr += ".setFillColor(numberColor," + i + ",1)";
    } else {
    myExpr += ".setFillColor(symbolColor," + i + ",1)";
    }
    }
    eval(myExpr)
  • Dolph Kawalec

    August 26, 2025 at 5:34 pm

    Thanks Dan! Worked like a charm. (Didn’t realize I uploaded the wrong screenshot – hope that wasn’t too confusing 😀)

  • Dolph Kawalec

    August 26, 2025 at 5:58 pm

    I have a follow up question, Dan.

    I’m trying to animate the characters to randomly offset, so doing this on the Source Text level will only apply the colors to the original text correctly. I tried to run it through AI again to apply it to a text animator and it came up with this:

    // Define colors

    letterColor = [.4784, .4784, .4784]; // letters

    numberColor = [.1333, .4275, .7804]; // numbers

    symbolColor = [.6196, .2235, .3686]; // symbols

    // Get the source text

    txt = text.sourceText;

    // Get this character

    c = txt[textIndex – 1]; // textIndex is 1-based

    // Default color

    col = symbolColor;

    // Test character type

    if (c.match(/[A-Za-z]/)) {

    col = letterColor;

    } else if (c.match(/[0-9]/)) {

    col = numberColor;

    }

    col

    But I’m getting that “textIndex is not defined” again. Do you know how to make it work?

  • Dan Ebberts

    August 26, 2025 at 6:28 pm

    textIndex can only be used in an expression for the Amount property of an Expression Selector. What are you doing with character offset exactly? You might be able to incorporate it into the Source Text expression.

  • Dolph Kawalec

    August 26, 2025 at 6:35 pm

    The idea is to have a block of “password text” that is cycling over and over. I was hoping I could add a character offset to the text layer, but the offset is applied after the source code is affected by the expression. So the the color coding stays attached to the original text. If a letter changes to a number, it will still have the original color of the letter still assigned to it.

    I’m thinking if I can apply the fill color to the text through a text animator I can run the offset first, followed by the expression for the fill colors and it should work, right?

  • Dan Ebberts

    August 26, 2025 at 6:43 pm

    Yes, but a Fill Color animator only gives you access to one additional color. I suppose you could have multiple animators.

  • Dolph Kawalec

    August 26, 2025 at 7:07 pm

    That’s why I’m trying to put an expression on the fill, something similar to the one that works on the source text. I wouldn’t need to animate it, I would just need it to come after the animator that offsets the text so the characters are colored appropriately.

  • Dan Ebberts

    August 26, 2025 at 7:26 pm

    I don’t think an expression on a subsequent animator is going to be able to see the text created by the offset animator. I think it will get the original text. That’s why I was wondering about creating the offset text within the source text expression.

  • Dolph Kawalec

    August 26, 2025 at 8:01 pm

    I see. That explains the error then. Thanks so much for your help. If you have any other suggestions, feel free to send them my way.

    Cheers!

  • Brie Clayton

    August 26, 2025 at 8:27 pm

    Thanks for solving this, Dan!

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