5 Replies Latest reply on Mar 14, 2011 4:57 AM by JJMack

    How to get the foreground/background color in photoshop.

    manish.mishra

      I want to get the foreground or background color at runtime ( by any action ) in photoshop.

      What steps should I follow.

        • 1. Re: How to get the foreground/background color in photoshop.
          c.pfaffenbichler Level 9

          What do you want to use that color-data for exactly?

           

          #target photoshop
          var foregroundColor = app.foregroundColor;
          var backgroundColor = app.backgroundColor;
          alert ("foregroundcolor\n"+foregroundColor.rgb.red+"\n"+foregroundColor.rgb.green+"\n"+foregroundColor.rgb.blue);
          alert ("backgroundcolor\n"+backgroundColor.rgb.red+"\n"+backgroundColor.rgb.green+"\n"+backgroundColor.rgb.blue);
          

          • 2. Re: How to get the foreground/background color in photoshop.
            Michael L Hale Level 5

            There is a start application event you can use to script tasks at runtime. You can set it up with the Scripts Event Manager found under File-Scripts menu.

             

            But as c.pfaffenbichler pointed out a script that just gets the colors is not very useful. Unless the script takes some action based on the colors found or stores the data somewhere like customOptions or to a file all the data in a script is removed from memory when the script ends.

            • 3. Re: How to get the foreground/background color in photoshop.
              JJMack Most Valuable Participant

              For example here is a script I use within Photoshop Actions to save and restore the users foreground and background colors so the action can mess with Photoshops foreground and background colors during its processing.

               

              SaveAndRestoreColors.jsx

               

               

              /* ======================================================================================

              // 2009  John J. McAssey (JJMack)  http://www.mouseprints.net/

              //

              // This script is supplied as is. It is provided as freeware.

              // The author accepts no liability for any problems arising from its use.

              //

              // This script is designed to be used by a Photoshop Action twice

              // A good pratice to use when creating an actions that use this scipt is for the action

              // not to do a save or play some other action between its two useages of this Script.

              //

              // The first time this script is used by an action Photoshops Current Forground

              // and Background Colors are saved into the document's meta-data Info Instructions field.

              //

              // The second time this script used by the action the script retreives what was

              // saved in the meta-data during the first usage and these colors are set.

              // and the saved data is removed from the document's meta-data Info Instructions field.

              //

              // ===================================================================================== */

               

              /*

              <javascriptresource>

              <about>$$$/JavaScripts/SaveAndRestoreColors/About=JJMack's SaveAndRestoreColors.^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^r^rFirst Run records Photoshops foreground and background swatch colors.^rSecond Run restores the recorded colors and removes the recording.</about>

              <category>JJMack's Action Run Twice Utility</category>

              </javascriptresource>

              */

               

              if (app.documents.length > 0) {

              if (app.activeDocument.info.instructions.indexOf("<Colorf>") == -1 ){ // no footprint fisrt useage

              //alert("first");

              // Retreive Document information for Foot Print

              saveforeColor = new SolidColor;

              saveforeColor.rgb.red = app.foregroundColor.rgb.red;

              saveforeColor.rgb.green = app.foregroundColor.rgb.green;

              saveforeColor.rgb.blue = app.foregroundColor.rgb.blue;

               

              savebackColor = new SolidColor;

              savebackColor.rgb.red = app.backgroundColor.rgb.red;

              savebackColor.rgb.green = app.backgroundColor.rgb.green;

              savebackColor.rgb.blue = app.backgroundColor.rgb.blue;

               

               

              // put footprint in metadata info instructions

               

              app.activeDocument.info.instructions = app.activeDocument.info.instructions + "<Colorf>" + saveforeColor.rgb.red + "," + saveforeColor.rgb.green + "," + saveforeColor.rgb.blue + "</Colorf>" + "<Colorb>" + savebackColor.rgb.red + "," + savebackColor.rgb.green + "," + savebackColor.rgb.blue + "</Colorb>";

              //alert( "Saved ="  + "<Colorf>" + saveforeColor.rgb.red + "," + saveforeColor.rgb.green + "," + saveforeColor.rgb.blue + "</Colorf>" + "<Colorb>" + savebackColor.rgb.red + "," + savebackColor.rgb.green + "," + savebackColor.rgb.blue + "</Colorb>");

               

               

              }

              else {

              //alert("second");

               

              // Retreive saved information

              colorfOffset = app.activeDocument.info.instructions.indexOf("<Colorf>") + "<Colorf>".length;

              colorfLength = app.activeDocument.info.instructions.indexOf("</Colorf") -colorfOffset;

              saveforeColor = app.activeDocument.info.instructions.substr(colorfOffset, colorfLength);

               

              colorbOffset = app.activeDocument.info.instructions.indexOf("<Colorb>") + "<Colorb>".length;

              colorbLength = app.activeDocument.info.instructions.indexOf("</Colorb") -colorbOffset;

              savebackColor = app.activeDocument.info.instructions.substr(colorbOffset, colorbLength);

               

              //alert("Colorf = " + saveforeColor + " Colorb = " + savebackColor );

               

              // Restore Colors

               

              app.foregroundColor.rgb.red = saveforeColor.substr(0,saveforeColor.indexOf(","));

              saveforeColor = saveforeColor.substr(saveforeColor.indexOf(",") + 1,saveforeColor.length);

              app.foregroundColor.rgb.green = saveforeColor.substr(0,saveforeColor.indexOf(","));

              saveforeColor = saveforeColor.substr(saveforeColor.indexOf(",") + 1,saveforeColor.length);

              app.foregroundColor.rgb.blue = saveforeColor ;

               

              app.backgroundColor.rgb.red = savebackColor.substr(0,savebackColor.indexOf(","));

              savebackColor = savebackColor.substr((savebackColor.indexOf(",") + 1),savebackColor.length);

              app.backgroundColor.rgb.green = savebackColor.substr(0,savebackColor.indexOf(","));

              savebackColor = savebackColor.substr(savebackColor.indexOf(",") + 1,savebackColor.length);

              app.backgroundColor.rgb.blue = savebackColor ;

               

               

              // Remove footprint from metadata info instructions

              before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexO f("<Colorf>"));

              afterOffset = app.activeDocument.info.instructions.indexOf("</Colorb>") + "</Colorb>".length;

              after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);

              //alert ("before = " + before + " after = " + after);

              app.activeDocument.info.instructions = before + after;

              }

              }

              else { alert("You must have at least one open document to run this script!"); }

              • 4. Re: How to get the foreground/background color in photoshop.
                manish.mishra Level 1

                Well I am sorry for not mentioning this I want to get the information of foreground and background color in my plugin( As my plugin is automation type) when any action is performed, So that I can have information about the color when that action is performed

                • 5. Re: How to get the foreground/background color in photoshop.
                  JJMack Most Valuable Participant

                  Is your Plug-in a compiled plug-in using Adobe's SDK or a Script  like Adobe'd Automate FitImage Script plug-in?  If it is a script you do it like the post here if compiled check the SDK for a way you would do it for the compiler languaqge your using.