8 Replies Latest reply on Feb 17, 2014 7:10 AM by Ben_Lindsey

    Return Individual Values From Array as Variables

    Ben_Lindsey

      Is it possible to loop through the channels and return their individual RGB values and names so they can be assigned to a var to be used later in the script as well as to build and populate a scriptUI?

       

      I think I specifically need the RGB values so I can color the panels to represent the colors associated with each channel as the scriptUI .graphics.newBrush method only seems to accept RGB.  I could be completely wrong about that though as my scripting knowledge is limited.

       

      I need the name so I can make the channel active using getByName.  Not yet comfortable with targeting by index and think it would be much simpler to catch the name instead in this case

       

      Essentially, I'd love for the loop to return something like:

       

      for(var i = 1; i<doc.channels.length; i++){
      if (//loop finds that there are channels.ChannelType.SPOTCOLOR (they will all be spot colors, componentChannels will never be used) present){
      var c1Name = //whatever the first channel's name is.;
      var c1Color = newColor();
              c1Color.rgb.red = //first channel's rgb.red.value; 
              c1Color.rgb.green= //first channel's rgb.green.value;      
              c1Color.rgb.blue = //first channel's rgb.blue.value;
              }  
           //...and so on for as many spot color channels are present. c2, c3, c4 and so on...
      }
      

       

      I'm not sure if that makes any sense but this is the only way I would know how to continue out the rest of the functions I'd like to perform.  I'm fairly confident I can handle putting together the UI and the rest of the script as needed, it's just actually getting this information and then passing them into variables that's completely boggling my mind.

       

      Michael L. Hale put together a great script using the Action Manager instead of DOM objects that's blazingly fast which can be found in this forum, but even in looking that over, I'm at a complete loss of how to tweak this to fit my needs or if this is even what I should be aiming for.  I've also taken a look at the many "channels to layers" posts here but still can't get a grasp on how to do this.

       

      Any help, ideas or guidance would be much appreciated as I've yet to wrap my head around arrays.

        • 1. Re: Return Individual Values From Array as Variables
          c.pfaffenbichler Level 9

          I think you are missing something, a Channel has no Color value per se, its pixels can have various values.

           

          Getting the name should not be that difficult.

          var c1Name = app.activeDocument.channels[i].name


          • 2. Re: Return Individual Values From Array as Variables
            Ben_Lindsey Level 1

            Thank you for your reply!  By the channel's color, I mean the color value that has been assigned to it.  For instance, if I make a channel and assign it a color as in:

             

            var c1Color = new SolidColor();
                    c1Color.rgb.red = app.foregroundColor.rgb.red;
                    c1Color.rgb.green = app.foregroundColor.rgb.green;
                    c1Color.rgb.blue = app.foregroundColor.rgb.blue;
            
            
            var makeChannel = doc.channels.add();
            makeChannel.color = c1Color;
            

             

            All of the channels I'm looking to grab information for would have a color assigned to it in this fashion.  To do it within the program, you double click the channel and assign it in the lower left side of the channel options box.

             

            channel-option.jpg

             

            As far as assigning the var c1Name as you've shown, wouldn't this assign the var c1Name to every single channel present?

             

            What I'm aiming for is to have c1Name, c2Name, c3Name and so on as well as c1Color, c2Color, c3Color, etc, based off of however many channels there are in the document.  These would coincide with one another so the vars c4Name and c4Color would be the name and assigned color information from that specific channel.

            • 3. Re: Return Individual Values From Array as Variables
              c.pfaffenbichler Level 9

              I mean the color value that has been assigned to it. 

              Sorry, I had misunderstood you wanted to get colors from the composite Channels.

               

              As for the rest I’m not sure I understand what you want – why do you not want an Array but individual variables?

              An Array of Arrays ( [[name1, color1], [name2, color2], …]) should work for populating a dialog.

              • 4. Re: Return Individual Values From Array as Variables
                Ben_Lindsey Level 1

                Sorry, I should have been more clear about that in my original post.

                 

                In regards to wanting variables instead of an array, I'd like to use them to build the scriptUI and to also target specific channels and carry out functions on those channels.

                 

                Here's a crude example of what the UI will look like.

                 

                scriptUI-example.jpg

                 

                Essentially, it will return a list of the spot channels in the document with a checkbox next to them.  Whichever channel's checkboxes are ticked will get a list of specific function ran on them once the OK button is hit.  This is why I was thinking I'd need to get variables returned for each.  I figured I would need the RGB to color the panels and the name to list in the UI as well as use to target the channel itself to perform the functions afterwards.

                 

                I can make the UI and do the rest of the functions needed but don't know how to return the information I need from the loop to be able to make it a reality.  Actually, now that I think of it, I would need to find a way to populate the UI based on the number of channels returned from the loop.  I'm thinking I may in a little bit over my head at the moment.

                • 5. Re: Return Individual Values From Array as Variables
                  c.pfaffenbichler Level 9

                  I see no reason not to do this with an Array.

                  • 6. Re: Return Individual Values From Array as Variables
                    Ben_Lindsey Level 1

                    I'm sort of at a loss with arrays.  My problem, when approaching arrays is that I don't understand how to pull the information from them.  At this point, I understand how to set up the loop and run a method on the array like this:

                     

                    for(var i = 0; i<doc.channels.length; i++){
                         doc.channels[i].visible = true;
                         }
                    

                     

                    But returning values from them completely escapes me.  I would like to think the following is correct but am questioning its validity.

                     

                    for(var i = 0; i<doc.channels.length; i++){
                         var chanName = doc.channels[i].name;
                         var chanColor = doc.channels[i].color;
                    }
                    
                              chanName[0] = c1Name;
                              chanName[1] = c2Name; //and so on...
                    
                              chanColor[0] = c1Color;
                              chanColor[1] = c2Color; //and so on...
                    

                     

                    If this is right, I'd be exstatic!  Unfortunately, I'm doubting it is.  Is this even close?

                    • 7. Re: Return Individual Values From Array as Variables
                      c.pfaffenbichler Level 9

                      Maybe this can help a little:

                      var doc = app.activeDocument;

                      var theArray = new Array;

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

                      var aChannel = doc.channels[i];

                      try {var theColor = aChannel.color} catch (e) {var theColor = null};

                      theArray.push([aChannel.name, theColor]);

                      };

                      alert (theArray.join("\n"));

                      Subsequently each element within the Array of Arrays can be retrieved with something like this:

                      var nameChannelOne = theArray[0][0];

                       

                      Edit: Originally the square brackets in

                      theArray.push([aChannel.name, theColor]);

                      did not show (must have happened because I replied via mail, I guess), but they are important!

                      • 8. Re: Return Individual Values From Array as Variables
                        Ben_Lindsey Level 1

                        Thank you!  With a little discovery time with that snippet, I'm starting to see how it works.  I'm sure I'll have plenty more questions in the future though!