Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Color from external txt-file

  • Color from external txt-file

    Posted by Marten Kopp on October 10, 2016 at 10:51 am

    Hi guys,

    Currently I am working with an expression explained by Dan Egberts in an earlier post. Here he explains how to use a color value from an external txt file.

    I have changed this expression to this:

    number = 01;
    myPath = “mypath/my_project.txt”;
    try{
    $.evalFile (myPath);
    r = parseInt((color+number).substr(0,2),16)/255;
    g = parseInt((color+number).substr(2,2),16)/255;
    b = parseInt((color+number).substr(4,2),16)/255;
    [r,g,b,1]
    }catch (err){
    [1,1,1,1]
    }

    What I need is this: I have a txt-file with about 20 colors. They are all formatted like this:

    color01 = “xxxxxx”;

    I would like to save this Fill effect as a preset and, when applied, only having to change the first line.
    How do I get this to work?

    Regards,

    Marten

    Marten Kopp replied 9 years, 6 months ago 2 Members · 2 Replies
  • 2 Replies
  • David Conklin

    October 10, 2016 at 4:57 pm

    The reason your expression is not working is because doing color+number is simply concatenation. This means that if it’s basically looking for the value of the variable color, and then adding a number to it, not dynamically calling the variable color01. There are 2 ways to fix this.

    The first way (and this is the WRONG way) is to use eval(). Using eval() basically forces whatever is inside the parenthesis to execute as code, so the string “color02” wrapped in eval() would actually return the value of the color02 variable. Again, this is NOT THE RIGHT WAY to do this, as eval() is typically considered dangerous for security reasons. While it most likely would be okay in a small project that you never plan on sending out, it’s typically best to try and develop without relying on eval().

    The expression w/ eval() would look like this:
    number = 01;
    myPath = "~/Desktop/colors.txt";
    try{
    $.evalFile (myPath);
    r = parseInt((eval(color+number)).substr(0,2),16)/255;
    g = parseInt((eval(color+number)).substr(2,2),16)/255;
    b = parseInt((eval(color+number)).substr(4,2),16)/255;
    [r,g,b,1]
    }catch (err){
    [1,1,1,1]
    }

    The second, and better, method is to construct a new variable inside $.evalFile() which can then be used in the r,g and b variable lines as a shorthand. This way you just change the variable c to whatever color you would like to retrieve. The code for this method looks like this:
    myPath = "~/Desktop/colors.txt";
    try{
    $.evalFile (myPath);

    var c = color01;

    r = parseInt(c.substr(0,2),16)/255;
    g = parseInt(c.substr(2,2),16)/255;
    b = parseInt(c.substr(4,2),16)/255;
    [r,g,b,1]
    }catch (err){
    [1,1,1,1]
    }

    Best of luck, friend.

    David Conklin
    mographcode.tumblr.com

  • Marten Kopp

    October 11, 2016 at 10:00 am

    Hi David,

    Thank you for your clear reply. I understand it now. My knowledge of coding is very small (as well as my English sadly).
    But with your help I managed to make a master color txt-file containing all colors that apear in my productvideos. Then then can be selected by applying a preset.

    Thanks!

    Regards,

    Marten

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