Copy link to clipboard
Copied
Hello,
I'm trying to write a script to use the magic wand to select a specific color and contract the selection by 3 pixels. However, the magic wand script requires identify the location of a pixel which doesn't work for me because the location of the specific color changes from image to image. Is there any way to get around this?
function magicWand(x,y,t,a,c,s) {
if(arguments.length < 2) return;// make sure have x,y
if(undefined == t) var t = 32;// set defaults of optional arguments
if(undefined == a) var a = true;
if(undefined == c) var c = false;
if(undefined == s) var s = false;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
desc.putReference( charIDToTypeID('null'), ref );
var positionDesc = new ActionDescriptor();
positionDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), x );// in pixels
positionDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), y );
desc.putObject( charIDToTypeID('T '), charIDToTypeID('Pnt '), positionDesc );
desc.putInteger( charIDToTypeID('Tlrn'), t);// tolerance
desc.putBoolean( charIDToTypeID('Mrgd'), s );// sample all layers
if(!c) desc.putBoolean( charIDToTypeID( "Cntg" ), false );// contiguous
desc.putBoolean( charIDToTypeID('AntA'), a );// anti-alias
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
Copy link to clipboard
Copied
If you kown the color use color range instead of the magic wand. get its lab valuse.
function colorRange(fuzziness, luminance, a, b, luminance2, a2, b2, colorModel) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
descriptor.putInteger( stringIDToTypeID( "fuzziness" ), fuzziness );
descriptor2.putDouble( stringIDToTypeID( "luminance" ), luminance );
descriptor2.putDouble( stringIDToTypeID( "a" ), a );
descriptor2.putDouble( stringIDToTypeID( "b" ), b );
descriptor.putObject( stringIDToTypeID( "minimum" ), stringIDToTypeID( "labColor" ), descriptor2 );
descriptor3.putDouble( stringIDToTypeID( "luminance" ), luminance2 );
descriptor3.putDouble( stringIDToTypeID( "a" ), a2 );
descriptor3.putDouble( stringIDToTypeID( "b" ), b2 );
descriptor.putObject( charIDToTypeID( "Mxm " ), stringIDToTypeID( "labColor" ), descriptor3 );
descriptor.putInteger( stringIDToTypeID( "colorModel" ), colorModel );
executeAction( stringIDToTypeID( "colorRange" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I had trouble with finding the right command to add a layer. So I recorded the original color of a pixel in coordinate (0,0), colored pixel in (0,0), and used magic wand on pixel (0,0). When I was done with the pixel, I colored it back to the original color.
Copy link to clipboard
Copied
activeDocument.artLayers.add()