I’m developing a Photoshop UXP plugin that needs to create Lab Fill Layers with masks based on arbitrary pixel data (Uint8Array with 0-255 values). Geometric selections work perfectly, but I cannot convert custom pixel data into masks.
Environment:
Photoshop UXP Plugin (API version without putSelectionFromPixels())
Document mode: Lab Color
Target: Fill Layers (contentLayer/solidColorLayer) with masks from pixel data
What Works
This approach using geometric selections works flawlessly:
Does this works on RGB? If so, you could temporary change it to RGB, do the mask thing and then convert back to LAB. Proper solution? I dunno but it’ll be a workaround to make it work so you can move on with your development and not get stuck on that.
I splitted the code into two blocks so we can see the colors as If I paste it in one block it shows up white I dunno why, but here’s it is, try it out, it worked for me:
await createMaskFromAlphaBuffer();
async function createMaskFromAlphaBuffer(alphaBuffer) {
await executeAsModal(async () => {
const doc = app.activeDocument;
const docW = doc.width;
const docH = doc.height;
// alphaBuffer goes here... Make sure it is the size of your document
// you can override the new Uint8Array()... if you pass a Uint8Array here
const pixels = new Uint8Array(docW * docH);
for (let i = 0; i < pixels.length; i++) {
pixels[i] = 255// White pixels just to have something, Add your mask pixels here / computation...
}
const options = {
width: doc.width,
height: doc.height,
components: 1,
colorProfile: "Dot Gain 20%",
colorSpace: "Grayscale"
}
const imageData = await imaging.createImageDataFromBuffer(pixels, options);
await imaging.putLayerMask({
imageData: imageData,
documentID: app.activeDocument.id,
layerID: app.activeDocument.activeLayers[0].id,
commandName: "Save Pixels in layer"
});
await core.redrawDocument({ documentID: doc.id });
imageData.dispose(); // Release memory before garbage collector to speed up things
}
Thank you so much. This is exactly what I need. I will be trying it out later today.
Thanks for the suggestion! I am not able to make it work. Maybe I am missing something. imaging.putLayerMask() does not exist in Photoshop UXP - attempting to call it throws “Error: Unsupported napi type”.
I also tested imaging.putPixels({ targetMask: true }) which DOES exist, but it only works on pixel layers. When you try to write mask data to a Fill Layer (which is a procedural/adjustment layer, not a pixel buffer), it fails with “Error: target sheet is not a pixel sheet”.
The only working approach I found for Fill Layers with custom masks is the selection-based method:
Create temp pixel layer with RGBA data (alpha channel = mask)
Load transparency as selection (batchPlay “set selection to transparencyEnum”)
Delete temp layer
Create Fill Layer + mask in single batchPlay call using revealSelection
Clear selection
This works because revealSelection is part of Photoshop’s native selection-to-mask workflow, which works on all layer types. It’s slower (~500-800ms per layer due to temp layer creation), but it’s reliable.