Skip to main content
the_starship
Participant
October 19, 2022
Answered

Conditional Text Color (if/else expression)

  • October 19, 2022
  • 2 replies
  • 5070 views

Hi, I am uploading information into my after effects animation via CSV. Some of these contain numbers and I would like to change the color based on if they're negative or positive. 

my script so far is as follows

 

var textcolor = thisComp.layer("Testgame4.csv")("Data")("Outline")("Difference")("Difference 12")

if (textcolor < 0 ){
textcolor.fillColor = hslToRgb(C20621);

}else { textcolor.fillColor = hslToRgb(44C206)}

 

but it keeps throwing an error one way or the other. Sometimes it's expecting a semi-colon where there is one and other times it's expecting a parenthisies where there is one. 

 

Any help would be appreciated as this would save a ton of time while creating content. Cheers! 

 

Correct answer mrtdg

I just found a good way to do this:

Color = comp("Main_Comp").layer("Customization").effect("TeamColor")("Menu").value;
colorArr = ["FFFFFF", "EAAA00", "444F51"];
hexToRgb(colorArr[Color -1]);

2 replies

ShiveringCactus
Community Expert
Community Expert
October 19, 2022

I didn't bother creating a CSV to test this, but based on the value of the source text, I think this changes as you want:

 

var myTextNumber = text.sourceText
var ogcolour = text.animator("Animator 1").property.fillColor;
var fill = hexToRgb('#44C206');
if (myTextNumber > 0) {
	fill = hexToRgb('#44C206');
} else {
	fill =  hexToRgb('#C20621');
}
fill;

 I used the Fill animator and added this expression to it.  It gives me an extra colour which I could have substituted for one of your hex codes.

I think I've spelled everything out, it probably could be written more efficiently, but basically:

  • Take the value of the text
  • create variables for the original colour and a fill value
  • If the text is more than 0, use the #44c206 colour value
  • else use the other colour #c20621
  • Tell After Effects which variable to use

the_starship
Participant
October 19, 2022

Thank you so much. I was able to add a Fill option on the essentail graphics and use that. It now works as intended. Thanks Again! 

mrtdg
mrtdgCorrect answer
Inspiring
January 10, 2024

I just found a good way to do this:

Color = comp("Main_Comp").layer("Customization").effect("TeamColor")("Menu").value;
colorArr = ["FFFFFF", "EAAA00", "444F51"];
hexToRgb(colorArr[Color -1]);

Mylenium
Legend
October 19, 2022

You cannot manipulate the text color this way. You have to use a Fill text animator with expressions and calculate the position of the element to be colored. And your color codes make no sense at all. AE treats colors as float values between 0 and 1 and to boot, your color codes aren't even valid hex because the # is missing. Once they are fixed, it's hexToRgb(), not HSL. There's a ton of things wrong here. You may want to read the online help on some basics.

 

[Abuse removed by moderator]

the_starship
Participant
October 19, 2022

the last line was unecessary and rude.