12 Replies Latest reply on Dec 6, 2017 1:57 AM by geppettol66959005

    create selection with color sampler

    geppettol66959005 Level 2

      Hello everybody

       

      I would like to create a selection using the selected points of the color sampler

      I tried ScriptingListenerJS but I could not

       

      I know I could use the color range

      but I would like to do it without opening this window.

        • 1. Re: create selection with color sampler
          c.pfaffenbichler Level 9

          Could you please elaborate about the exact nature and basis of the intended selection?

           

          How do you select multiple Colour Samplers?

          As far as I can tell they do not even have an »active« or »selected« property, so once they have been created with the Colour Sampler Tool they simply »are there«, so to speak.

          • 2. Re: create selection with color sampler
            c.pfaffenbichler Level 9

            This would create selections based on all the Colour Samplers’ colors in turn.

             

            // 2017, use it at your own risk;

            #target photoshop

            var myDoc = activeDocument;

            var theSamplers = myDoc.colorSamplers;

            for (var m = 0; m < theSamplers.length; m++) {

            myDoc.selection.deselect();

            var theL = theSamplers[m].color.lab.l;

            var theA = theSamplers[m].color.lab.a;

            var theB = theSamplers[m].color.lab.b;

            colorRange([theL, theA, theB], [theL, theA, theB], 40)

            };

            ////// color range //////

            function colorRange (theMin, theMax, theFuzziness) {

            // =======================================================

            var idClrR = charIDToTypeID( "ClrR" );

                var desc13 = new ActionDescriptor();

                var idFzns = charIDToTypeID( "Fzns" );

                desc13.putInteger( idFzns, theFuzziness );

                var idMnm = charIDToTypeID( "Mnm " );

                    var desc14 = new ActionDescriptor();

                    var idLmnc = charIDToTypeID( "Lmnc" );

                    desc14.putDouble( idLmnc, theMin[0] );

                    var idA = charIDToTypeID( "A   " );

                    desc14.putDouble( idA, theMin[1] );

                    var idB = charIDToTypeID( "B   " );

                    desc14.putDouble( idB, theMin[2] );

                var idLbCl = charIDToTypeID( "LbCl" );

                desc13.putObject( idMnm, idLbCl, desc14 );

                var idMxm = charIDToTypeID( "Mxm " );

                    var desc15 = new ActionDescriptor();

                    var idLmnc = charIDToTypeID( "Lmnc" );

                    desc15.putDouble( idLmnc, theMax[0] );

                    var idA = charIDToTypeID( "A   " );

                    desc15.putDouble( idA, theMax[1] );

                    var idB = charIDToTypeID( "B   " );

                    desc15.putDouble( idB, theMax[2] );

                var idLbCl = charIDToTypeID( "LbCl" );

                desc13.putObject( idMxm, idLbCl, desc15 );

                var idcolorModel = stringIDToTypeID( "colorModel" );

                desc13.putInteger( idcolorModel, 0 );

            executeAction( idClrR, desc13, DialogModes.NO );

            };

            1 person found this helpful
            • 3. Re: create selection with color sampler
              geppettol66959005 Level 2

              This already seems like a good thing

              Thank you

              • 4. Re: create selection with color sampler
                Kukurykus Adobe Community Professional
                BT = '(function BT(v1, v2) {bt = new BridgeTalk(), bt.target = v1, \
                bt.body = v2, bt.onResult = function(v) {return res = v.body} bt.send(2)})'
                
                function rw(v1, v2, v3) {
                     v2.open(v1), v1 < 'w' ? v3 = v2.read() : v2.write(v3), v2.close(); return v3
                }
                
                function log(v) {
                     if ((prf = preferences).useHistoryLog) prf.useHistoryLog = false
                     if ((fle = File('~/Desktop/Log.txt')).exists) fle.remove()
                     if (v) {
                          prf .useHistoryLog = true, prf.saveLogItemsFile = fle
                          prf.editLogItems = EditLogItemsType.DETAILED
                          prf.saveLogItems = SaveLogItemsType.LOGFILE
                     }
                }
                
                if (!(len = (aD = activeDocument).colorSamplers.length)) log(true)
                
                else {
                     c = rw('r', File('~/Desktop/log.txt')).match(/[\d+,]{5,}(?= )/g)
                     for(i in c) if (i < 8) c[i] = (c[i].replace(/,/, '.'))
                     for(str = '', i = 0; i <= (2 * len) - 1; i += 2) {
                          str += ('[' + c[i] + ', ' + c[i + 1] + '], ')
                     }
                
                     if (!eval($.getenv('arr'))) $.setenv('arr', eval('"[' + str.slice(0, -2) + ']"'))
                     else {
                          ARR = eval($.getenv('arr')), ARR = uneval(ARR.concat(eval
                          (eval('"[' + str.slice(0, -2) + ']"')))), $.setenv('arr', ARR)
                     }
                
                     for(i = 0; i < (pnt = eval(eval('"[' + str.slice(0, -2) + ']"'))).length; i++) {
                          wh = pnt[i], w = wh[0], h = wh[1];
                          (sel = aD.selection).select([[w - .5, h - .5], [w + .5, h - .5],
                          [w + .5, h + .5], [w - .5, h + .5]], SelectionType.EXTEND)
                     }
                
                     function sTT(v) {return stringIDToTypeID(v)}
                
                     (ref1 = new ActionReference()).putEnumerated(
                     sTT('colorSampler'), sTT('ordinal'), sTT('allEnum'));
                     (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1)
                     executeAction(sTT('delete'), dsc1, DialogModes.NO), log(true)
                }
                
                if (len && eval($.getenv('arr'))) {
                     (ref = new ActionReference()).putProperty(sTT('property'), sTT('tool'))
                     if (ref.putClass(sTT('application')), typeIDToStringID(executeActionGet
                     (ref).getEnumerationType(sTT('tool'))) == 'moveTool') {
                
                          dst = '"' + aD.path + '/' + aD.name + '"'
                
                          ENV = (function ENV(v, v1, v2) {
                               for (arr = [], i = 0; i < v2.length; i++) {
                                    arr.push("'" + new Color((new BitmapData
                                    (new Thumbnail(File(v1)).spec, true))
                                    .getPixel(v2[i][0] , v2[i][1])).toString().slice(3) + "'")
                               }
                
                               arr = '"' + arr + '"', colR = (function colR(v) {
                                    function sTT(v) {return stringIDToTypeID(v)}; function cR(f, l0, a0,  b0, l1, a1, b1) {
                                         (dsc1 = new ActionDescriptor()).putInteger(sTT('fuzziness'), f)
                                         for(i = 0, n = 2; i < (arr = ['minimum', 'maximum']).length; i++) {
                                              ln = "(D = dsc" + n + " = new ActionDescriptor()).putDouble"
                                              ln +="(sTT('luminance'), arguments[1 + p = l1 ? (i * 3) : 0])"
                                              eval(ln), n++, eval("D.putDouble(sTT('a'), arguments[2 + p])")
                                              eval("D.putDouble(sTT('b'), arguments[3 + p])")
                                              dsc1.putObject(sTT(arr[i]), sTT('labColor'), D)
                                         }
                                         dsc1.putInteger(sTT('colorModel'), 0)
                                         executeAction(sTT('colorRange'), dsc1, DialogModes.NO);
                                    }
                                    for(L = A = B = j = 0; j < (arr = eval('[' + v + ']')).length; j++) {
                                         (col = new SolidColor).rgb.hexValue = arr[j]
                                         L += col.lab.l, A += col.lab.a, B += col.lab.b
                                    }
                                    function div(v) {return v / j}; cR(50, div(L), div(A), div(B))
                               })
                               eval("(" + v + ")('photoshop', '(' + " + colR.toString() + " + ')' + '([' + arr + '])')" )
                           })
                
                          sel.deselect(), eval(BT + "('bridge', '(' + " + ENV.toString() + " + ')' + \
                          '(' + BT + ', ' + dst + ', ' + $.getenv('arr') + ')')"), $.setenv('arr', null)
                     }
                }
                

                 

                There are only 4 color samplers in Photoshop. This script let you imitiate much more of them. Simply put all 4 samplers on image and then play the script. It takes positions of samplers to memory and create 1x1 pixel squares in their places. Then you can play scirpt how many times you want and each time use up to 4 samplers again. When you are ready change current tool to Move tool and play script again. That change will be signal for script that you want to use all selected pixels (before samplers) to make colour range. (Script will add all 3 lab values to every 3 of other selected pixels separately, so value L to L, A to A, B to B of all pixels, and then each of these groups devide by pixels (samplers) length. FYI I used sessions variables (setenv, getenv) also took pixels color using BridgeTalk BitmapDatas getPixel() as well as Photoshop Log).

                • 5. Re: create selection with color sampler
                  geppettol66959005 Level 2

                  I can not use your script

                  I get an error in line 20

                   

                  c = rw('r', File('~/Desktop/log.txt')).match(/[\d+,]{5,}(?= )/g) 

                   

                  use mac with photoshop cc2014

                  • 6. Re: create selection with color sampler
                    r-bin Level 4

                    It's all good.

                    But do you know about the bug in the CC version Re: sampled colors in color range is not recorded by scripts
                    which can negate all your efforts to use the script with Color Range.

                    • 7. Re: create selection with color sampler
                      r-bin Level 4

                      The correct function for this topic

                       

                      color_range_from_samples(10)
                      
                      
                      function color_range_from_samples(fzns)
                          {   
                          try {   
                              var sel_names = new Array();  
                      
                              for (var i = 0; i < app.activeDocument.colorSamplers.length; i++) 
                                  {
                                  app.activeDocument.selection.deselect();
                                  color_range(app.activeDocument.colorSamplers[i].color, fzns)
                              
                                  if (has_selection()) sel_names.push(save_selection());
                                  }
                      
                              app.activeDocument.selection.deselect();
                      
                              for (var i = 0; i < sel_names.length; i++) 
                                  {
                                  if (!i) load_selection(sel_names[i]);
                                  else    add_selection(sel_names[i]);
                      
                                  del_selection(sel_names[i]);
                                  }
                      
                              function color_range(color, fzns)
                                  {   
                                  try {   
                                      var d1 = new ActionDescriptor();
                                      d1.putInteger( charIDToTypeID( "Fzns" ), fzns );
                                      var d2 = new ActionDescriptor();
                                      d2.putDouble( charIDToTypeID( "Lmnc" ), color.lab.l );
                                      d2.putDouble( charIDToTypeID( "A   " ), color.lab.a );
                                      d2.putDouble( charIDToTypeID( "B   " ), color.lab.b );
                                      d1.putObject( charIDToTypeID( "Mnm " ), idLbCl = charIDToTypeID( "LbCl" ), d2 );
                                      var d2 = new ActionDescriptor();
                                      d2.putDouble( charIDToTypeID( "Lmnc" ), color.lab.l );
                                      d2.putDouble( charIDToTypeID( "A   " ), color.lab.a );
                                      d2.putDouble( charIDToTypeID( "B   " ), color.lab.b );
                                      d1.putObject( charIDToTypeID( "Mxm " ), charIDToTypeID( "LbCl" ), d2 );
                                      d1.putBoolean( charIDToTypeID( "Invr" ), false );
                                      d1.putInteger( stringIDToTypeID( "colorModel" ), 0 );
                                      executeAction( charIDToTypeID( "ClrR" ), d1, DialogModes.NO );
                                      }
                                  catch (e) { throw(e); }
                                  }
                      
                              function save_selection()
                                  {
                                  try {
                                      var name = Math.random().toString().substr(2);
                                      var d = new ActionDescriptor();
                                      var r = new ActionReference();
                                      r.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
                                      d.putReference( charIDToTypeID( "null" ), r );
                                      d.putString( charIDToTypeID( "Nm  " ), name );
                                      executeAction( charIDToTypeID( "Dplc" ), d, DialogModes.NO );
                      
                                      return name;
                                      }
                                  catch (e) { throw(e); }
                                  }
                              
                              function load_selection(name)
                                  {
                                  try {
                                      var d = new ActionDescriptor();
                                      var r1 = new ActionReference();
                                      r1.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
                                      d.putReference( charIDToTypeID( "null" ), r1 );
                                      var r2 = new ActionReference();
                                      r2.putName( charIDToTypeID( "Chnl" ), name );
                                      d.putReference( charIDToTypeID( "T   " ), r2 );
                                      executeAction( charIDToTypeID( "setd" ), d, DialogModes.NO );
                                      }
                                  catch (e) { throw(e); }
                                  }
                                      
                              function add_selection(name)
                                  {
                                  try {
                                      var d = new ActionDescriptor();
                                      var r1 = new ActionReference();
                                      r1.putName( charIDToTypeID( "Chnl" ), name );
                                      d.putReference( charIDToTypeID( "null" ), r1 );
                                      var r2 = new ActionReference();
                                      r2.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
                                      d.putReference( charIDToTypeID( "T   " ), r2 );
                                      executeAction( charIDToTypeID( "Add " ), d, DialogModes.NO );
                                      }
                                  catch (e) { throw(e); }
                                  }
                                      
                              function del_selection(name)
                                  {
                                  try {
                                      var d = new ActionDescriptor();
                                      var r = new ActionReference();
                                      r.putName( charIDToTypeID( "Chnl" ), name );
                                      d.putReference( charIDToTypeID( "null" ), r );
                                      executeAction( charIDToTypeID( "Dlt " ), d, DialogModes.NO );
                                      }
                                  catch (e) { throw(e); }
                                  }
                      
                              function has_selection()
                                  {
                                  try
                                      {
                                      var r = new ActionReference();
                                      r.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "selection" ) );
                                      r.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                                      if (executeActionGet(r).hasKey(stringIDToTypeID("selection"))) return true; 
                                      return false;
                                      }
                                  catch (e) { throw(e); }
                                  }
                              }
                          catch (e) { _alert(e); }
                          }
                              
                      
                      1 person found this helpful
                      • 9. Re: create selection with color sampler
                        Kukurykus Adobe Community Professional

                        I tested this script for many cases I didn't noticeded before once after I created it. Now I know there were some situations that it didn't want to work. Current verstion of script was changed in few parts that now it has not 86 but 104 lines. Detailed instruction how to use it as well as sole code you find on forums: Unlimited point samplers script - PS-SCRIPTS.COM

                        • 10. Re: create selection with color sampler
                          Kukurykus Adobe Community Professional

                          Thx for info about bug. I have CS6 EXTENDED on Windows 10 and there is no problem like you mentioned. Fortunatelly there were other problems in my script I solved, but if it is like in linked post maybe there will be another you found solution for. I only don't understand why that code you wrote in that theard you didn't use in this one to avoid wrong behaviour of Color Range? @geppettol66959005 uses like he said CC2014 so is there bug or not or your script for him doesn't need that correction?

                          • 12. Re: create selection with color sampler
                            r-bin Level 4

                            I'm thinking @geppettol66959005 asks questions from a purely academic interest and tries to just study the creation of scripts in Photoshop. This I understood on the issues that he asked me with personal messages.

                            Accordingly, my answers are also of an educational nature only. If a person wants to use my code, he will probably take into account those comments that I made about possible problems.