Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Number Colors and Absolute Value

  • Number Colors and Absolute Value

    Posted by Mark Fletcher on May 13, 2014 at 11:27 pm

    Hello. An expressions noob here needing help.

    In AfterEffects, I’m trying to script a way for displayed numbers (using effects > text > numbers) to have their Fill Color be linked to their value. For example, positive numbers display in black, negative numbers display in red, and zero displays in white. I don’t want the negative sign to display when a number is less than zero — I only want it to be red. I’ve written the expression below which makes the color portion of the values work. However, when I introduce code for absolute value (Math.abs(value)) to remove the negative sign, it messes up the calculations . . . which then messes up the colors. Any ideas? Thanks!

    fColor = effect("Numbers")("Fill Color");
    calcValue = effect("Numbers")("Value/Offset/Random Max");

    if (calcValue <0)
    {
    fColor = [.8,0,0,1]
    }
    else
    {
    fColor
    }

    if (calcValue >0)
    {
    fColor = [0,0,0,1]
    }
    else
    {
    fColor
    }

    if (calcValue =0)
    {
    fColor = [1,1,1,1]
    }
    else
    {
    fColor
    }

    Mark Fletcher replied 12 years, 4 months ago 2 Members · 6 Replies
  • 6 Replies
  • Kevin Camp

    May 14, 2014 at 12:02 am

    you might try using a slider to set the actual +/- values, then point your expression to the slider value.

    that way you’ll have the minus value to use to set the color to red and you can then use Math.abs() on the slider value for the numbers value.

    you could also simplify your expression a bit with else ifs, something like this:

    n = effect(“Slider Control”)(“Slider”);
    if (n > 0) [0,0,0,1];
    else if (n < 0) [.8,0,0,1];
    else [1,1,1,1];

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Mark Fletcher

    May 14, 2014 at 7:15 pm

    Thanks Kevin. Your simplified expression at the bottom is really nice. However, when I introduce Math.abs() on the slider, it messes up the negative calculations and turns the color to black when it should be red. -M

  • Kevin Camp

    May 14, 2014 at 7:26 pm

    sorry i think i didn’t explain that well.

    i’d use the slider’s value for the numbers effect value, but use the expression (in the numbers value) to remove the minus sign… (still sounds confusing)

    try this:

    remove any expressions on the slider’s value.

    add this expression to the numbers value:

    Math.abs(effect(“Slider Control”)(“Slider”));

    if the slider is on the same layer and still named ‘slider control’ that should work.

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Mark Fletcher

    May 14, 2014 at 8:29 pm

    Hi Kevin,

    I’m sure your technique will work, perhaps I don’t have the code in the right location. I’m going to try uploading my aep file so you can see my two approaches with your code.

    Thanks,

    Mark

    7495_numberissuev02.aep.zip

    <a href="https://f1.creativecow.net/7495/re-number-colors-and-absolute-value">7495_numberissuev02.aep.zip</a>

  • Kevin Camp

    May 14, 2014 at 9:34 pm

    So the first thing you’ll want to do is add a slider control to the calculatedNumber layer (effect>expression controls>slider control).

    then select all of your numbers value/offset/random keyframes and copy them. then select the slider control slider property and paste those keyframes.

    now you slider values should mirror your numbers values.

    once that is done, you can delete the keyframes from the numbers value and enable expresssion for that property and enter (or paste) this:

    Math.abs(effect(“Slider Control”)(“Slider”))

    now you’ll also need to direct your fill color expression to look at the slider value rather than the numbers value. so you’ll need to change the second line of your expression to this:

    calcValue = effect(“Slider Control”)(“Slider”);

    and that should do it.

    the problem you were having before was that the color expression needed the negative values of the numbers value to work properly, so when you used Math.abs() to drop the minus sign, the color expression only saw positive values.

    to fix that, we needed a value that still had positive and negative values, so an easy way is to use a slider control.

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Mark Fletcher

    May 14, 2014 at 9:55 pm

    That works great! You’ve been a huge help, Kevin. Thanks! Cheers – M

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