Skip to main content
Known Participant
January 12, 2015
Answered

Applying/Editing Fill and Opacity Values to Gradients?

  • January 12, 2015
  • 1 reply
  • 1151 views

Running off of previously answered question here.

Trying to see what I would have to call to adjust values of each stop in a gradient, specifically the fill.tint and opacity values.  I would like to apply the following script to the fills.

var docRef = app.activeDocument; 

var paths = docRef.pathItems; 

 

 

for (i=0; i< paths.length; i++){ 

    if (paths.fillColor.tint == 50 && paths.opacity == 100){ 

        paths.fillColor.tint = 100; 

        paths.opacity = 50; 

         

        } 

    else if (paths.fillColor.tint == 100 && paths.opacity == 50){ 

        paths.fillColor.tint = 50; 

        paths.opacity = 100; 

        

        } 

    }

Here is one of the test files I'm using.   In this file looking to make white stops (Fill of 0) have an opacity of 0, and stops of magenta have an opacity equal to their fill.

Any assistance would be much appreciated.  Thank you!

This topic has been closed for replies.
Correct answer pixxxelschubser

Sorry, this is a long way.

There are could be not only path items (e.g. compound paths, group items and nested variations).

There are could be colors or spot colors. Tint is a property of spot colors.

Here is a simple snippet for you for playing:

var aDoc = app.activeDocument;

var thePaths = aDoc.pathItems;

var theStops, tnt, opac;

for (i=0; i< thePaths.length; i++) {

    if ( thePaths.fillColor.typename == 'GradientColor' ) {

        theStops = thePaths.fillColor.gradient.gradientStops;

        for (j=0; j< theStops.length; j++) {

            tnt = theStops.color.tint;

            opac = theStops.opacity;

            if (tnt && tnt != opac) {

                theStops.color.tint = opac;

                theStops.opacity = tnt;

                }

            }

        }

    }

1 reply

pixxxelschubser
Community Expert
Community Expert
January 12, 2015

Hi MrJHMaximus,

???

what exactly is the problem? To find the properties? See in OMV for gradient and gradientStop and color.

You will find things like these:

var pathA = app.activeDocument.pathItems[0];

var aGrad = pathA.fillColor.gradient;

if ( pathA.fillColor.typename == 'GradientColor' ) {

    var howmany = aGrad.gradientStops.length;

    $.writeln(howmany);

    var opac = aGrad.gradientStops[0].opacity;

    $.writeln(opac);

    var col = aGrad.gradientStops[0].color;

    $.writeln(col);

    $.writeln(col.tint);

    }

Or to set the values for the properties:

aGrad.gradientStops[0].color.tint = 40;

Hope this helps a little bit.

Have fun

Known Participant
January 12, 2015

Thanks so much pixxxel schubser,

This is helpful, hopefully with this I can adjust the question for clarity and keep toying with it to see what I can get to work.

This is the code I have so far:

var docRef = app.activeDocument;

var paths = docRef.pathItems;

var pathA = app.activeDocument.pathItems;

var aGrad = pathA.fillColor.gradient;

for (i=0; i< paths.length; i++){

     if ( pathA.fillColor.typename == 'GradientColor' ) {

          if (aGrad.gradientStops.fillColor.tint = 50 && aGrad.gradientStops.opacity = 100){

          aGrad.gradientStops.fillColor.tint = 100

          aGrad.gradientStops.opacity = 50

          }

     }

     else if (paths.fillColor.tint == 50 && paths.opacity == 100){

          paths.fillColor.tint = 100;

          paths.opacity = 50;

          }

     }

I'm still learning all this and been staring at it all day long; but hopefully this clarifies the goal.  What I'd like to do is change the opacity and fillColor.tint of each stop in an existing gradient.  As an example:

GRADIENT EXAMPLEColor Stop #1Color Stop #2Color Stop #3
Existing Tint/Opacity100/10050/100100/100
RUN SCRIPT
New Tint/Opacity100/100100/50100/100

Am I on the right track? Am I miles away from a solution?  Any help is much appreciated!

Disposition_Dev
Legend
January 13, 2015

For now, I have a longer code that has each of the values spelled out, since I have specific value constraints to work within.  But when i have more free time, I'll play with it and create a script that can use any variable. for the tint and opacity.


ok i've got it. this will just swap the opac and tnt so no numbers will need to be hard coded. you could add an if clause if you wanted to regarding 100% fill/tint.. but if the fill and tint are both 100% anyway, nothing will change.

if (paths.fillColor.typename == 'GradientColor' ){

          var theStops = paths.fillColor.gradient.gradientStops;

          for (j=0; j< theStops.length; j++){

              var newTint = 0;

              var newOpac = 0;

              var tnt = theStops.color.tint;

              newOpac = tnt;

              var opac = theStops.opacity;

              newTint =opac;

              theStops.color.tint = newTint;

              theStops.opacity = newOpac;

          }

    }