Skip to content

Instantly share code, notes, and snippets.

@julik
Created December 7, 2014 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save julik/4c001101ba882cd6de8c to your computer and use it in GitHub Desktop.
Save julik/4c001101ba882cd6de8c to your computer and use it in GitHub Desktop.
Unpremultiply the selected Photoshop layer
/*
Will unpremultiply the currently selected Photoshop layer
*/
var doc = app.activeDocument;
// Duplicate this layer
var premultLayer = app.activeDocument.activeLayer.duplicate();
premultLayer.blendMode = BlendMode.NORMAL;
// Apply layer mask to the duplicate
// Create a black layer underneath the premult one
var beneathLayer = app.activeDocument.artLayers.add();
doc.selection.selectAll();
var blackColor = new SolidColor();
blackColor.rgb.red = 0;
blackColor.rgb.green = 0;
blackColor.rgb.blue = 0;
doc.selection.fill(blackColor, ColorBlendMode.NORMAL, 100, false);
beneathLayer.move(premultLayer, ElementPlacement.PLACEAFTER);
// Create a black layer above the premult one
var aboveLayer = beneathLayer.duplicate();
aboveLayer.move(premultLayer, ElementPlacement.PLACEBEFORE);
// Fill the premult opaque areas with white for the layer above
// Select transparency on the premult layer. Unfortunately we need the
// action ref abominations for that.
function SelectTransparency()
{
var idChnl = charIDToTypeID( "Chnl" );
var actionSelect = new ActionReference();
actionSelect.putProperty( idChnl, charIDToTypeID( "fsel" ) );
var actionTransparent = new ActionReference();
actionTransparent.putEnumerated( idChnl, idChnl, charIDToTypeID( "Trsp" ) );
var actionDesc = new ActionDescriptor();
actionDesc.putReference( charIDToTypeID( "null" ), actionSelect );
actionDesc.putReference( charIDToTypeID( "T " ), actionTransparent );
executeAction( charIDToTypeID( "setd" ), actionDesc, DialogModes.NO );
}
doc.activeLayer = premultLayer;
SelectTransparency();
doc.activeLayer = aboveLayer;
var whiteColor = new SolidColor();
whiteColor.rgb.red = 255;
whiteColor.rgb.green = 255;
whiteColor.rgb.blue = 255;
app.activeDocument.selection.fill(whiteColor, ColorBlendMode.NORMAL, 100, false);
// Set the layer above to "divide"
aboveLayer.blendMode = BlendMode.DIVIDE;
// Merge the cake of 3 layers together, using a layer set.
var unpremultSet = doc.layerSets.add();
beneathLayer.move(unpremultSet, ElementPlacement.INSIDE);
premultLayer.move(unpremultSet, ElementPlacement.INSIDE);
aboveLayer.move(unpremultSet, ElementPlacement.INSIDE);
unpremultSet.merge();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment