Created
December 7, 2014 12:31
-
-
Save julik/4c001101ba882cd6de8c to your computer and use it in GitHub Desktop.
Unpremultiply the selected Photoshop layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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