Copy link to clipboard
Copied
Short version, I want the fill color of a shape to change based on the value of a number on another layer. There are a total of three colors I want it to be ready to take, but try as I might I can't get the expression part to work. I've got the colors set up in a master template, so that part's easy; now I need to figure out the IF/ELSE part to get it to actually evaluate the number.
temp1 = thisComp.layer("Temperature").effect("Temperature")("Slider");
temp2 = thisComp.layer("Temperature").effect("Warning Max")("Slider");
temp3 = thisComp.layer("Temperature").effect("Warning Min")("Slider");
temp4 = thisComp.layer("Temperature").effect("Average")("Slider");
if (temp1 <= temp2 || temp1 >= temp3) { 100 }
else if (temp1 > temp4) { 150 };
else if (temp1 < temp4) { 250 };
Basically, if the value of temp1 is outside the range specified by temp2 and temp3 (greater than or equal to or less than or equal to the appropriate value), I want the box to turn one color (represented here by the numeral "100" but I'll change that to point to the template's fill color).
If it isn't but it's above the value of temp4, I want the box to turn a different color (represented by "150"), and if it isn't but it's below the value of temp4, I want it to turn a third color (represented by "250").
The numerals here are because I've got this on a slider that I'm using to test this out. The problem is, I'm getting an error, "Unexpected token 'else' " so something about this syntax is wrong, but I can't figure out what I'm doing wrong.
Can anybody help me fix it?
Copy link to clipboard
Copied
One thing that might work, if I'm understanding your desire correctly, is to replace your "100," etc., with hexToRgb(000000) <<with the hex values for the color you want>>. This assumes that the expression is on a color control or color property. So the last part of your code would read:
// Replace the "color values" with actual hex colors!
if (temp1 < temp2 || temp1 >= temp3) hexToRgb(000001);
else if (temp1 > temp4) hexToRgb(000002);
else if (temp1 < temp4) hexToRgb(000003);
Copy link to clipboard
Copied
As I believe I explained--the problem here isn't the RGB values, which I can swap in later. This expression is sitting on a slider, which wouldn't know what to do with RGB values, which is why I used the numerals--if I can get the slider to return the correct numeral values, then I'll know I've gotten the IF/ELSE problem solved. Also as I explained, I have the RGB values in a template elsewhere in the project--I can swap those values in once we get the IF/ELSE problem solved.
So while your contribution is interesting, it doesn't help me solve the underlying problem, which is that the IF/ELSE syntax is kicking back an error.
Copy link to clipboard
Copied
Your approach is going to snap from one color to another. You have a slider for temperature, a to set a minimum value, a slider to set a maximum value, and a slider to set an average temperature. As the temperature slider approaches the minimum value, the color changes to one color; as the temperature approaches the maximum value, it changes to another color. I'm not sure I understand what the Average slider is supposed to do.
If I were designing this kind of graphic, I would set up three rectangles on a single shape layer, attach the sliders to that shape layer, then animate the Rectangle/Transform/Opacity of each layer using an expression on Transform Rectangle 1 (hot), and Rectangle 2 (Cold) while leaving Rectangle 3 (Normal) untouched. If the temperature slider is animated, you can establish a range of values to blend the colors as the temperature changes and use blend modes between the Rectangles to give you pleasing colors.
Hot Opacity Expression:
t = effect("Temperature")("Slider");
tMax = effect("Max")("Slider");
av = effect("Differende")("Slider");
tMin = tMax - av;
linear (t, tMin, tMax, 0, 100)
Cold Opacity Expression:
t = effect("Temperature")("Slider");
tMin = effect("Min")("Slider");
av = effect("Differende")("Slider");
tMax = tMin + av;
linear (t, tMin, tMax, 100, 0)
You could use the same approach to directly control the color of a fill by figuring out the RGB values you want for the color changes and replacing the 0 and 100 with the starting and ending RGB colors. That would require only one shape layer and an expression for color that looked something like this:
t = effect("Temperature")("Slider");
tMin = effect("Min")("Slider");
tMax = effect("Max")("Slider");
dif = effect("Difference")("Slider");
Nor = [.2, .9, .2, 1];// Green
Hot = [.9, .9, .2, 1]; // Yellow
Cold = [.2, .5, .9, 1]; // Blue
Av = tMax - tMin;
if (t < tMin + dif)
linear(t, tMin, tMin + dif, Cold, Nor);
else if (t > tMin + dif && t < tMax - dif)
Nor
else if (t > tMax - tMin)
linear(t, tMax - dif, tMax, Nor, Hot)
Color values are always 0 to 1 for R, G, B, and A unless you use a converter method in the expression. I usually just use the float values and have my Info panel always set to decimal (32-bit), so the color picker gives you the decimal values for RGB. You'll end up with something like this:
I included a project file for you to play with.
If you want to use Hex codes and a single shape layer fill or color effect like tint, this expression should do it. I cleaned it up a bit so it's easier to understand the structure.
Hot = hexToRgb("F6A70F");// Oraange
Cold = hexToRgb("3380E6"); // Blue
Nor = hexToRgb("26D071"); // Green
t = effect("Temperature")("Slider");
tMin = effect("Min")("Slider");
tMax = effect("Max")("Slider");
dif = effect("Difference")("Slider");
Av = tMax - tMin;
if (t > tMin + dif && t < tMax - dif)
Nor
else if (t < tMin + dif)
linear(t, tMin, tMin + dif, Cold, Nor);
else if (t > tMax - tMin)
linear(t, tMax - dif, tMax, Nor, Hot)
else
Nor
I hope this makes sense and helps a bit.
Copy link to clipboard
Copied
This is actually a very clever approach that I hadn't considered--it's not terribly applicable to the exact scenario I have (the reason I'm doing it this way is it's a demo graphic and the temperatures are all assigned to random expressions, and the expression is assigning a static color based on the result of the random seed).
Copy link to clipboard
Copied
Hi Brandon,
You are getting the error because you put semicolons in the wrong place (after the curly braces). Also you need to swap || with && otherwise the first "if" will always be true. Try this:
if (temp1 <= temp2 && temp1 >= temp3) {
100;
} else if (temp1 > temp4) {
150;
} else if (temp1 < temp4) {
250;
}
Copy link to clipboard
Copied
Another thing is that if you have a string of else if clauses, you want the last one to be just an else in case something gets all the way through your logic (like if temp1 == temp4):
if (){
}else if (){
}else if (){
}else{
}
Copy link to clipboard
Copied
Great stuff as usual Dan!
Copy link to clipboard
Copied
@Christian Lengger Incorrect about the first if/else--it has to be "or" not "and," because those two are outside values (10 degrees above and below a mean temperature), and if the actual temperature is higher than temp3, the high value, OR lower than temp2, I want it to show an alarm. I can't have it be "and" because then the condition would be impossible to satisfy.
(In other words, if the temperature is either higher than X+10, or lower than X-10, then the first condition is satisfied)
Copy link to clipboard
Copied
Oh, my bad. I assumed it to be within a range.
Copy link to clipboard
Copied
Did you check out my expression examples or try my project file? I've got three different ways to use a Temperature slider, a Minimum and Maximum temp for High and Low thresholds, and a Fade slider to define the range of the temperature change it takes to dissolve between the three colors.
Here's the animation preset. Create a new shape layer, add a Sold Fill, select the fill color, and apply the animation preset. You should be ready to go.
If you add some sliders and some color controls and name them properly this expression will work with any Color property you can add to a layer.
Hot = effect("Hot")("Color");
Normal = effect("Normal")("Color");
Cold = effect("Cold")("Color");
t = effect("Temperature")("Slider");
tMin = effect("Min")("Slider");
tMax = effect("Max")("Slider");
FadeTime = effect("Fade")("Slider")/5;
if (t > tMin + FadeTime && t < tMax - FadeTime)
Normal
else if (t < tMin + FadeTime)
linear(t, tMin, tMin + FadeTime, Cold, Normal);
else if (t >= tMax - FadeTime)
linear(t, tMax - FadeTime + 1, tMax, Normal, Hot)
else
Normal
Copy link to clipboard
Copied
I haven't yet (super tight deadline on this particular project so I had to just fix the syntax I had) but over the weekend I fully intend to. Your description sounds intriguing and it looks like an approach I hadn't considered, so I want to learn more.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more