3 Replies Latest reply on Sep 7, 2012 7:49 AM by CarlosCanto

    Changing the color of a selection to HEX

    Olyas1

      Hi guys,

      i'm new to illustrator scripting (but not to javascript or programming).

      I have an AI file that has multiple layers. each layer has several items that are supposed to be of same color.

      I have an array of HEX values.

      I would like to loop over all items in a single layer, select them and change their color to some given HEX.

      Once that works, I will run on all HEX entries in array and save each variation in a separate PNG file.

       

      For the first part i wrote some script (see below), but i do not see any change in the file after i run the script. No errors and no change.

      I run the script once i select all items in a specific layer.

       

      Can you please advise what am i doing wrong?

      Thanks in advance!

       

      My code:

      -------------------------------

       

      function hexToRgb(hex) {

          var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);

          return result ? {

              r: parseInt(result[1], 16),

              g: parseInt(result[2], 16),

              b: parseInt(result[3], 16)

          } : null;

      }

       

       

      var fillRGBColor = function (pathItem, r, g, b){

          pathItem.fillColor.red = r;

          pathItem.fillColor.green = g;

          pathItem.fillColor.blue = b;

      }

       

       

      var fillHexColor = function (pathItems, hex){

          var rgb = hexToRgb(hex);

          alert(rgb.r +","+rgb.g+","+rgb.b);

          fillRGBColor(pathItems, rgb.r, rgb.g, rgb.b);

      }

       

      // check if a document is open in Illustrator.

      if( app.documents.length > 0)

      {

                mySelection = app.activeDocument.selection;

       

                if (mySelection.length > 0)  {

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

                        var currentObject = mySelection[i]; 

                        if(currentObject.typename != "CompoundPathItem" &&  

                                                                  currentObject.typename != "GroupItem")  {

                           

                               fillHexColor(currentObject, '#8F623B');

                                                   }

                                    }

                } else {

                               alert("Nothing is selected.");

                }

       

      } else {

                alert("No document Open.");

      }