3 Replies Latest reply: Nov 12, 2011 6:52 AM by carleq RSS

    Color Select Script Needs Refining

    carleq Community Member

      Hi

       

      I've been running this simple script (created by a fine PS scripter) in an action:

       

      #target Photoshop

      function main(){

      if(!documents.length) return;

      app.activeDocument.colorSamplers.removeAll();

      var X = activeDocument.width.as('px')/2;

      var sample = activeDocument.colorSamplers.add( [ new UnitValue( X, 'px' ), new UnitValue( 1, 'px' ) ] );

      app.backgroundColor=sample.color;

      app.activeDocument.colorSamplers.removeAll();

      }

      main();

       

      It selects the top center pixel color, point sample, for background. I now need it to select a 5x5 avg. Secondly, is there some way to hide the INFO panel, which is opened by running the script? This is'nt callable from an action, of course, and I'm no scripter.

       

      Thanks for any help.

        • 1. Re: Color Select Script Needs Refining
          Paul Riggott Community Member

          This should do an average...

           

           

          #target Photoshop
          function main(){
          if(!documents.length) return;
          var startRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          try{
          app.activeDocument.colorSamplers.removeAll();
          var X = activeDocument.width/2;
          var LB=[];
          LB[0] = X.value;
          LB[1] = 1;
          LB[2] = X.value +5;
          LB[3] = 6;
          var tmpColour = new SolidColor();
          var savedState = activeDocument.activeHistoryState;
          activeDocument.selection.select([[LB[0],LB[1]], [LB[2],LB[1]], [LB[2],LB[3]], [LB[0], LB[3]]], SelectionType.REPLACE, 0, false);
          activeDocument.activeLayer.applyAverage();
          var sample = activeDocument.colorSamplers.add( [ new UnitValue( X+1, 'px' ), new UnitValue( 2, 'px' ) ] );
          tmpColour=sample.color;
          activeDocument.activeHistoryState = savedState;
          app.backgroundColor=tmpColour;
          app.preferences.rulerUnits = startRulerUnits;
          }catch(e){alert(e + " - " + e.line);}
          } 
          main();
          
          

           

          I haven't been able to stop the info panel appearing so I add it to my open panels so it's not intrusive.

          • 2. Re: Color Select Script Needs Refining
            carleq Community Member

            It's superb, Paul, superb.

             

            The only problem was that running the script opened Units & Rulers preferences twice and I needed to dialog "OK."

             

            Because the script was just setting rulers to pixels, which I almost always use anyway, I just removed these lines from the script:

             

            var startRulerUnits = app.preferences.rulerUnits;

            app.preferences.rulerUnits = Units.PIXELS;

             

            app.preferences.rulerUnits = startRulerUnits;

             

            ...and everything works great! Thanks, Paul.

            • 3. Re: Color Select Script Needs Refining
              carleq Community Member

              And I almost forgot to say thanks, Paul for the INFO panel idea. Just keep it open, out of the way. That works.