11 Replies Latest reply on Jan 13, 2015 3:27 PM by MrJHMaximus

    Applying/Editing Fill and Opacity Values to Gradients?

    MrJHMaximus

      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[i].fillColor.tint == 50 && paths[i].opacity == 100){  
              paths[i].fillColor.tint = 100;  
              paths[i].opacity = 50;  
                
              }  
          else if (paths[i].fillColor.tint == 100 && paths[i].opacity == 50){  
              paths[i].fillColor.tint = 50;  
              paths[i].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!

        • 1. Re: Applying/Editing Fill and Opacity Values to Gradients?
          pixxxel schubser Level 5

          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

           

          • 2. Re: Re: Applying/Editing Fill and Opacity Values to Gradients?
            MrJHMaximus Level 1

            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[i]; 
            var aGrad = pathA.fillColor.gradient; 
            
            for (i=0; i< paths.length; i++){
                 if ( pathA[i].fillColor.typename == 'GradientColor' ) {
                      if (aGrad.gradientStops[i].fillColor.tint = 50 && aGrad.gradientStops[i].opacity = 100){
                      aGrad.gradientStops[i].fillColor.tint = 100
                      aGrad.gradientStops[i].opacity = 50
                      }
                 }
                 else if (paths[i].fillColor.tint == 50 && paths[i].opacity == 100){
                      paths[i].fillColor.tint = 100;
                      paths[i].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!

             

             

            • 3. Re: Re: Re: Applying/Editing Fill and Opacity Values to Gradients?
              pixxxel schubser Level 5

              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[i].fillColor.typename == 'GradientColor' ) {
                      theStops = thePaths[i].fillColor.gradient.gradientStops;
                      for (j=0; j< theStops.length; j++) {
                          tnt = theStops[j].color.tint;
                          opac = theStops[j].opacity;
                          if (tnt && tnt != opac) {
                              theStops[j].color.tint = opac;
                              theStops[j].opacity = tnt;
                              }
                          }
                      }
                  }
              

               

               

              • 4. Re: Applying/Editing Fill and Opacity Values to Gradients?
                williamadowling Level 4

                tint is also a property of a global process color as well. when maximus asked his first question, i had originally asked whether he was using spot colors or global process.. but i ran the test on both and it didn't make a difference.

                • 5. Re: Re: Re: Re: Applying/Editing Fill and Opacity Values to Gradients?
                  MrJHMaximus Level 1

                  Thanks so much to both of you!

                   

                  The following is working for my needs, and could definitely be simplified further, but I need the code to fail at times as a red flag for when my files are set up incorrectly.

                   

                  pixxxel schubser: I was able to adjust the code snippet you provided with the code provided by williamadowling to have the same result.  That code is listed in the original post if anyone would like to review, as for the current code effecting gradients in the same way, here is what I have:

                  for (i=0; i< paths.length; i++){
                  else if ( paths[i].fillColor.typename == 'GradientColor' ) {
                          var theStops = paths[i].fillColor.gradient.gradientStops;
                          for (j=0; j< theStops.length; j++) {
                              var tnt = theStops[j].color.tint;
                              var opac = theStops[j].opacity;
                              if (tnt == 20 && opac == 100) {
                                  theStops[j].color.tint = 100;
                                  theStops[j].opacity = 20;
                                 }  
                              }     
                  }         
                  }
                  

                   

                   

                  Much obliged to you both!  Thank you for your assistance!

                  • 6. Re: Applying/Editing Fill and Opacity Values to Gradients?
                    williamadowling Level 4

                    so.. are you just going to change the code each time?? or are  you going to try to write something that extracts the values? as you have that written, it will only work if the tint is 20 and the opacity is 100.

                    • 7. Re: Applying/Editing Fill and Opacity Values to Gradients?
                      MrJHMaximus Level 1

                      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.

                      • 8. Re: Re: Applying/Editing Fill and Opacity Values to Gradients?
                        williamadowling Level 4

                        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[j].color.tint;
                                      newOpac = tnt;
                                      var opac = theStops[j].opacity;
                                      newTint =opac;
                                      theStops[j].color.tint = newTint;
                                      theStops[j].opacity = newOpac;
                        
                        
                                  }
                            }
                        
                        • 9. Re: Re: Applying/Editing Fill and Opacity Values to Gradients?
                          MrJHMaximus Level 1

                          This is great! I will put this to use at home for sure, thanks for sharing!

                          • 10. Re: Applying/Editing Fill and Opacity Values to Gradients?
                            williamadowling Level 4

                            i had tested this on my own test file and it worked great, however i looked at the file you sent yesterday and it seems that you have a mixture of spot colors and process colors. since this needs to extract the tint percentage (which isn't a property of CMYKColor), you need to use all spot colors or convert your process whites to global process colors. i think that could be scripted as well... with a simple if clause.. "if theStops[j].color.typename == CMYKColor { (i don't know the syntax, but convert to global)} then proceed with the swap.

                            • 11. Re: Applying/Editing Fill and Opacity Values to Gradients?
                              MrJHMaximus Level 1

                              I'll spend some time looking at it, realized it'll help on my end to add in something similar for grayscale as well.