8 Replies Latest reply on Feb 18, 2016 3:29 PM by Qwertyfly...

    How to change specific colors to another color in Illustrator javescript

    saranr37921948 Level 1

      Hi,

      How to change specific colors to another color in Illustrator javescript

       

      From (Specific Color)

      C:85

      Y:50   

      M:0

      K:0


      To (Another Color )

      C:50

      Y:100   

      M:0

      K:0

       

      Screen Shot 2016-02-13 at 6.41.04 PM.png

       

       

       

       

       


        • 1. Re: How to change specific colors to another color in Illustrator javescript
          Qwertyfly... Level 4

          I'm sure to if statement in the modify function could be cleaned up.

          but this should work for you.

           

          function reColorSelection(){  
            
              var col = new CMYKColor;  
              col.cyan = 50;  
              col.magenta = 100;  
              col.yellow = 0;  
              col.black = 0; 
              
              var oldCol = [];
              oldCol.C = 85;
              oldCol.M = 50;
              oldCol.Y = 0;
              oldCol.K = 0;
              oldCol.tolerance = 1;
              
              var sel = app.activeDocument.selection;  
              for(var i=0; i < sel.length; i++){
                  if(sel[i].typename === "CompoundPathItem"){
                      for(var j=0; j< sel[i].pathItems.length; j++){
                          modify(sel[i].pathItems[j]);
                      }
                  }
                  modify(sel[i]);    
              }  
                
              function modify(obj){
                  if(obj.fillColor.cyan >= (oldCol.C-(oldCol.tolerance/2)) && obj.fillColor.cyan < (oldCol.C+(oldCol.tolerance/2))
                      && obj.fillColor.magenta >= (oldCol.M-(oldCol.tolerance/2)) && obj.fillColor.magenta < (oldCol.M+(oldCol.tolerance/2))
                      && obj.fillColor.yellow >= (oldCol.Y-(oldCol.tolerance/2)) && obj.fillColor.yellow < (oldCol.Y+(oldCol.tolerance/2))
                      && obj.fillColor.black >= (oldCol.K-(oldCol.tolerance/2)) && obj.fillColor.black < (oldCol.K+(oldCol.tolerance/2))){
                          obj.filled = true;   
                          obj.fillColor = col;  
                  }
              }
          }   
          reColorSelection(); 
          
          • 2. Re: How to change specific colors to another color in Illustrator javescript
            karthickm50893796

            Hi Qwertyfly


            Hey good to see this  thank u

             

            But i need one change on it, every time i am selecting objects to change colors,

             

            is there any other javascript to change colors with out selecting objects??

             

             

            • 3. Re: How to change specific colors to another color in Illustrator javescript
              Qwertyfly... Level 4

              I think this should work



              change this line:

              var sel = app.activeDocument.selection;


              to this:

              var sel = app.activeDocument.pathItems;


              untested!!

              • 4. Re: How to change specific colors to another color in Illustrator javescript
                gabriels98539908

                Thanks Qwertyfly 

                 

                It works out well. But, when we try using this, the stroke and text frame does not get changed as it should have been. It needs to changed as well. Please advise if there is any script or if any changes needs to be made in the given script. I have also attached a sample for easy understanding.

                Screen Shot 2016-02-17 at 2.54.38 PM.png

                 

                  function reColorSelection(){   

                       

                        var col = new CMYKColor;   

                        col.cyan = 50;   

                        col.magenta = 100;   

                        col.yellow = 0;   

                        col.black = 0;  

                         

                        var oldCol = []; 

                        oldCol.C = 85; 

                        oldCol.M = 50; 

                        oldCol.Y = 0; 

                        oldCol.K = 0; 

                        oldCol.tolerance = 1; 

                         

                        var sel = app.activeDocument.pathItems;   

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

                            if(sel[i].typename === "CompoundPathItem"){ 

                                for(var j=0; j< sel[i].pathItems.length; j++){ 

                                    modify(sel[i].pathItems[j]); 

                                } 

                            } 

                            modify(sel[i]);     

                        }   

                           

                        function modify(obj){ 

                            if(obj.fillColor.cyan >= (oldCol.C-(oldCol.tolerance/2)) && obj.fillColor.cyan < (oldCol.C+(oldCol.tolerance/2)) 

                                && obj.fillColor.magenta >= (oldCol.M-(oldCol.tolerance/2)) && obj.fillColor.magenta < (oldCol.M+(oldCol.tolerance/2)) 

                                && obj.fillColor.yellow >= (oldCol.Y-(oldCol.tolerance/2)) && obj.fillColor.yellow < (oldCol.Y+(oldCol.tolerance/2)) 

                                && obj.fillColor.black >= (oldCol.K-(oldCol.tolerance/2)) && obj.fillColor.black < (oldCol.K+(oldCol.tolerance/2))){ 

                                    obj.filled = true;    

                                    obj.fillColor = col;   

                            } 

                        } 

                    }    

                    reColorSelection();  

                • 5. Re: How to change specific colors to another color in Illustrator javescript
                  saranr37921948 Level 1

                  I am also facing same issues !!! It's there any solution


                  stroke and text frame does not get changed color


                  Saran



                  • 6. Re: How to change specific colors to another color in Illustrator javescript
                    Qwertyfly... Level 4

                    it's just a matter of testing for all the items you want changed.

                    Try this...

                     

                    function reColorSelection(){    
                        
                        var col = new CMYKColor;    
                        col.cyan = 50;    
                        col.magenta = 100;    
                        col.yellow = 0;    
                        col.black = 0;   
                          
                        var oldCol = [];  
                        oldCol.C = 85;  
                        oldCol.M = 50;  
                        oldCol.Y = 0;  
                        oldCol.K = 0;  
                        oldCol.tolerance = 1;  
                          
                        var stuff = app.activeDocument.pageItems;
                        
                        
                        for(var i=0; i < stuff.length; i++){  
                            if(stuff[i].typename === "CompoundPathItem"){  
                                for(var j=0; j< sel[i].pathItems.length; j++){  
                                    modify(stuff[i].pathItems[j]);  
                                }  
                            }
                            if(stuff[i].typename === "PathItem"||stuff[i].typename === "TextFrame"){
                                modify(stuff[i]);
                            }
                        }  
                            
                        function modify(obj){ 
                            if(obj.typename === "TextFrame"){
                                obj = obj.textRange;
                            }
                    
                    
                            if(obj.fillColor.cyan >= (oldCol.C-(oldCol.tolerance/2)) && obj.fillColor.cyan < (oldCol.C+(oldCol.tolerance/2))  
                                && obj.fillColor.magenta >= (oldCol.M-(oldCol.tolerance/2)) && obj.fillColor.magenta < (oldCol.M+(oldCol.tolerance/2))  
                                && obj.fillColor.yellow >= (oldCol.Y-(oldCol.tolerance/2)) && obj.fillColor.yellow < (oldCol.Y+(oldCol.tolerance/2))  
                                && obj.fillColor.black >= (oldCol.K-(oldCol.tolerance/2)) && obj.fillColor.black < (oldCol.K+(oldCol.tolerance/2))){  
                                    obj.filled = true;     
                                    obj.fillColor = col;    
                            }
                            if(obj.strokeColor.cyan >= (oldCol.C-(oldCol.tolerance/2)) && obj.strokeColor.cyan < (oldCol.C+(oldCol.tolerance/2))  
                                && obj.strokeColor.magenta >= (oldCol.M-(oldCol.tolerance/2)) && obj.strokeColor.magenta < (oldCol.M+(oldCol.tolerance/2))  
                                && obj.strokeColor.yellow >= (oldCol.Y-(oldCol.tolerance/2)) && obj.strokeColor.yellow < (oldCol.Y+(oldCol.tolerance/2))  
                                && obj.strokeColor.black >= (oldCol.K-(oldCol.tolerance/2)) && obj.strokeColor.black < (oldCol.K+(oldCol.tolerance/2))){  
                                    obj.stroked = true;     
                                    obj.strokeColor = col;    
                            }
                        }  
                    }     
                    reColorSelection();
                    
                    • 7. Re: How to change specific colors to another color in Illustrator javescript
                      saranr37921948 Level 1

                      yes this is working thank you very much Qwetyfly