Search code examples
javascriptphotoshopphotoshop-script

In photoshop scripting how do you loop each selection and apply a gradient effect during each iteration?


Basic idea

Please ignore the blue line. Basically, I want to use select > color range or magic wand to make some selections and for each selected patch I would like to apply some gradient in the center of the patch.

Pseudo code would be:

select current active layer

apply color range or magic wand selection

for each sub selection in main selection:

    do: 
        calculate the center coordinate of the current sub-selection area
        apply a custom gradient using the coordinate as the starting point

I am not sure if this is do able because I don't know if the color range or magic want selection contains "sub-selections". By sub-selections I mean each individual patch in the above image.


Solution

  • There're no sub-selections, however you can transform selection to Path object, each individual path can be used for iteration. Depending on selection you have (color range may give semi-transparent selections), you may want to save current selection as a separate channel first and modify the selection to make it 100% non-transparent.

    To calculate center, I guess something like this should work: Find centerpoint of polygon in JavaScript because you can get all the coordinate for control points of each subpath.

    And then you'll need to create selection islands one by one so that gradients won't influence each other. To do that you can use path points as points for Lasso selection and then intersect these lasso-selections with original selection channel to regain lost transparency.