6 Replies Latest reply on Feb 16, 2016 3:04 AM by karthickm50893796

    How to find find color x, replace with color y?

    toxfordDMN Level 1

      Trying to figure out why this isn't working. I want to find pathItems that have a specific CMYK color and replace it with a new CMYK color.

      I discovered that I should round my CMYK values, but it's still not working. Any ideas?

       

      // Define colors
      
      
      var targetBlue = new CMYKColor();
      targetBlue.cyan = 49;
      targetBlue.magenta = 25;
      targetBlue.yellow = 0;
      targetBlue.black = 0;
      
      
      var dmnRed = new CMYKColor();
      dmnRed.cyan = 0;
      dmnRed.magenta = 80;
      dmnRed.yellow = 80;
      dmnRed.black = 0;
      
      
      // Set variable for path items on active document
      var docRef = app.activeDocument;
      
      
      var fillRef = docRef.pathItems;
      
      
      //Loop through each item and check if its fill color is targetBlue.
      // If it is, change the color to dmnRed
      
      
      for ( var i = 0; i < fillRef.length; i++) {
          
          fillRef[i].fillColor.cyan = Math.floor(fillRef[i].fillColor.cyan);
          fillRef[i].fillColor.magenta = Math.floor(fillRef[i].fillColor.magenta);
          fillRef[i].fillColor.yellow = Math.floor(fillRef[i].fillColor.yellow);
          fillRef[i].fillColor.black = Math.floor(fillRef[i].fillColor.black);
          
          
              if (fillRef[i].fillColor == targetBlue) {
                     fillRef[i].selected = true;
                     fillRef[i].filled = true;
                     fillRef[i].fillColor = dmnRed; 
                  }
              else {
                     $.writeln("Item " + i + " isn't targetBlue");
                  }
          }
      
        • 1. Re: How to find find color x, replace with color y?
          Silly-V Adobe Community Professional

          The issue is that no matter how much you try to floor it, the Illustrator color values will get conveniently distorted the instant they are set into a color object - unlike versions previous to some of the new versions, I believe.

          Even if you set your new red color, if you alert the magenta value in the fillColor of the affected selection, you'll see your value as something like 80.0000011920929

          Also I am not sure the Illustrator color object comparison would work either, comparing a fillColor to a targetBlue color object. (?)
          So here is what you do: you compare each of the fillColor's 4 color values with your own.

           

                  if (Math.floor(fillRef[i].fillColor.cyan) == targetBlue.cyan &&
                      Math.floor(fillRef[i].fillColor.magenta) == targetBlue.magenta &&
                      Math.floor(fillRef[i].fillColor.yellow) == targetBlue.yellow &&
                      Math.floor(fillRef[i].fillColor.black) == targetBlue.black) { 
                        fillRef[i].selected = true; 
                        fillRef[i].filled = true; 
                        fillRef[i].fillColor = dmnRed; 
                  } else { 
                    alert("Item " + i + " isn't targetBlue"); 
                  } 
          
          • 2. Re: How to find find color x, replace with color y?
            Vinicius Baptista Level 2

            There are several ways to do this, the problem is in math.floor and if condition to change color they are inconsistent.

             

            try this:

             

            // Define colors  
              
              /*
            var targetBlue = new CMYKColor();  
            targetBlue.cyan = 49;  
            targetBlue.magenta = 25;  
            targetBlue.yellow = 0;  
            targetBlue.black = 0;  
              */
              
            var dmnRed = new CMYKColor();  
            dmnRed.cyan = 0;  
            dmnRed.magenta = 80;  
            dmnRed.yellow = 80;  
            dmnRed.black = 0;  
              
              
            // Set variable for path items on active document  
            var docRef = app.activeDocument;  
              
              
            var fillRef = docRef.pathItems;  
              
              
            //Loop through each item and check if its fill color is targetBlue.  
            // If it is, change the color to dmnRed  
              
              
            for ( var i = 0; i < fillRef.length; i++) {  
            
            
            /*
                fillRef[i].fillColor.cyan = Math.floor(fillRef[i].fillColor.cyan);  
                fillRef[i].fillColor.magenta = Math.floor(fillRef[i].fillColor.magenta);  
                fillRef[i].fillColor.yellow = Math.floor(fillRef[i].fillColor.yellow);  
                fillRef[i].fillColor.black = Math.floor(fillRef[i].fillColor.black);  
                  */
                  
                    if (fillRef[i].fillColor.cyan == 49 && fillRef[i].fillColor.magenta == 25 && fillRef[i].fillColor.yellow == 0 && fillRef[i].fillColor.black == 0) { 
                           fillRef[i].selected = true;  
                           fillRef[i].filled = true;  
                           fillRef[i].fillColor = dmnRed;   
                        }  
                    else {  
                           $.writeln("Item " + i + " isn't targetBlue");  
                        }  
                }  
            
            • 3. Re: How to find find color x, replace with color y?
              Silly-V Adobe Community Professional

              There are some strange things happening with the color values, and I get different results with one script than with another! I'll do some more tests later when I get a moment.

              • 4. Re: How to find find color x, replace with color y?
                toxfordDMN Level 1

                That worked! Thanks much. Wish there was a more efficient way since I'll have multiple colors I'll need to replace but it's still better than doing it manually.

                • 5. Re: How to find find color x, replace with color y?
                  Vinicius Baptista Level 2

                  If these colors already exist, you can populate them in a listbox and then select the colors that you want to replace, or create a window to make changes. Anyway, I'm glad you found a way.

                   

                  Good luck, best regards.

                  Vinícius

                  • 6. Re: How to find find color x, replace with color y?
                    karthickm50893796 Level 1

                    Great post Thank u, its work well