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)
TIneke Van Schalkwyk updated 1 month, 3 weeks ago 3 Members · 7 Posts
-
Maeve Tan
January 9, 2023 at 12:35 pmI 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 inputI 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 : 0Add 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 amHi 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 : 0which 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 pmThank 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 pmNormally 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
Reply to this Discussion! Login or Sign Up
Log in to reply.