从透明像素中获取颜色样本

Get colour sample from transparent pixel

我正在尝试检测像素是否完全透明。更具体地说,图像是否在透明背景上。我以为

  // Add a Color Sampler at a given x and y coordinate in the image.
  var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);

  // Obtain array of RGB values.
  var rgb = [
  pointSample.color.rgb.red,
  pointSample.color.rgb.green,
  pointSample.color.rgb.blue
 ];

会起作用 - 它确实...

除非像素的不透明度为 0%(即 100% 透明),否则 Photoshop 会抛出 General 8800 error。我会将上面的内容与 try catch 一起使用,但这可能不是解决问题的方法。

有什么想法吗?

好吧,我找到了一种方法来确定图像是否具有透明度:

delete_all_colour_samples();
var isImageTransparent = has_transparency();
var x = 0;
var y = 0;

alert(isImageTransparent ? "Image has transparency." : "Image is opaque.");

if (isImageTransparent)
{

    // do stuff
}
else
{
    // get colour sample

    // Add a Color Sampler at a given x and y coordinate in the image.
    var pointSample = app.activeDocument.colorSamplers.add([(x),(y)]);

    // Obtain array of RGB values.
    var rgb = [
    pointSample.color.rgb.red,
    pointSample.color.rgb.green,
    pointSample.color.rgb.blue
 ];
}



// function to see if image has transparency
function has_transparency() 
{
    var desc52 = new ActionDescriptor();
    var ref47 = new ActionReference();
    ref47.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc52.putReference( charIDToTypeID('null'), ref47 );
    var ref48 = new ActionReference();
    ref48.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Trsp') );
    desc52.putReference( charIDToTypeID('T   '), ref48 );
    desc52.putBoolean( charIDToTypeID('Invr'), true );
    try
    {
        executeAction( charIDToTypeID('setd'), desc52, DialogModes.NO );
    }
    catch(eek)
    {
        return false;

    }
    activeDocument.selection.deselect();
    return true;
}


function delete_all_colour_samples()
{
   // Kill all colour samples
   app.activeDocument.colorSamplers.removeAll();
}