Copy link to clipboard
Copied
Hello everyone,
I need to do an action based on the content of a background layer.
Sometimes the background is totally white (as a result of creating a new document CTRL+N):
and sometimes the background isn't white (for example if I opened a jpg file)
what is the best way to differ between these two?
(i can do an average color of the layer but there must be a better way)
Thanks
Copy link to clipboard
Copied
I think the Histogram should provide a useful approach.
Copy link to clipboard
Copied
#target photoshop
if(documents.length){
var doc = activeDocument
app.preferences.rulerUnits = Units.PIXELS;
var h = doc.histogram
var empty = false
for (i=0;i<h.length;i++){
if(h == Number(doc.width*doc.height)){
empty = true;
break;
}
}
if (empty){
alert("White Document");
// doAction("ActionName","ActionSet");
}else{
alert("Not White Document");
//doAction("ActionName","ActionSet");
}
}
Copy link to clipboard
Copied
And if you should be bored:
What about newly created documents that were not created with white as »Background Contents«?
If the task is determining a newly created document I wonder if other approaches (History maybe) would not make sense, too.
Copy link to clipboard
Copied
Very true!
It depends on how deep it should go, this might surfice?
#target photoshop
if(documents.length) testColour();
function testColour(){
var doc = activeDocument
app.preferences.rulerUnits = Units.PIXELS;
var pixels = Number(doc.width*doc.height);
var h = doc.histogram
if(h[0] == pixels){
alert("Solid Black Document");
// doAction("ActionName","ActionSet");
return;
}
if(h[255] == pixels ){
alert("Solid White/Transparent Document");
// doAction("ActionName","ActionSet");
return;
}
var empty = false
for (i=0;i<h.length;i++){
if(h == Number(doc.width*doc.height)){
empty = true;
break;
}
}
if (empty){
alert("Solid Colour Document");
// doAction("ActionName","ActionSet");
}else{
alert("Not Solid Colour Document");
//doAction("ActionName","ActionSet");
}
};
Copy link to clipboard
Copied
Although not directly applicable to this situation, it is worth mentioning this article with related code:
Copy link to clipboard
Copied
AG_Ps_100 , maybe you could give more context.
What is the ultimate goal here and how does it fit in your workflow?
Because I think creating a new document could be linked to a Script or Action via Script Events Manager anyhow.