• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I tell if a layer is solid

Community Beginner ,
May 21, 2023 May 21, 2023

Copy link to clipboard

Copied

I want to get the RGB color values of the upper left and upper right corner and the lower left and right corner through Photoshop script to determine whether the current layer is a solid color. I don't know how to achieve this

TOPICS
Actions and scripting

Views

361

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 21, 2023 May 21, 2023
EDIT: using the average filter may remove minor differences and trigger a false when true, so this isn't robust enough to detect minor differences.
 

Duplicate the target layer. Filter > Blur > Average. Change to Difference blend mode. Check the histogram for the mean or median value that is not equal to zero. Remove the dupe layer.

 

/*
Check If Active Layer Is a Flat Color.jsx
v1.0, 22nd May 2023 - Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-tell-if-
...

Votes

Translate

Translate
Community Expert ,
May 21, 2023 May 21, 2023

Copy link to clipboard

Copied

Try the various scripts in the following topic thread:

 

 
Do you also need to check for layer opacity below 100% in addition to the layer containing transparency?
 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2023 May 21, 2023

Copy link to clipboard

Copied

Thank you very much for your answer. Maybe my title is wrong. I mean to judge whether the current layer is a pure color layer 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2023 May 21, 2023

Copy link to clipboard

Copied

quote

Thank you very much for your answer. Maybe my title is wrong. I mean to judge whether the current layer is a pure color layer 


By @meadX

 

Ah, well that is different.

 

Will sampling the four corners and the centre be accurate enough?

 

What about the histogram? The rounded RGB Luminosity mean value should equal the rounded median value. You could also compare the separate R, G and B rounded mean vs. rounded median values, or work in a copy in Lab colour mode only comparing the L channel value etc.

 

P.S. I am only "brainstorming" ideas, I'm not the guy to write the code!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2023 May 21, 2023

Copy link to clipboard

Copied

EDIT: using the average filter may remove minor differences and trigger a false when true, so this isn't robust enough to detect minor differences.
 

Duplicate the target layer. Filter > Blur > Average. Change to Difference blend mode. Check the histogram for the mean or median value that is not equal to zero. Remove the dupe layer.

 

/*
Check If Active Layer Is a Flat Color.jsx
v1.0, 22nd May 2023 - Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-do-i-tell-if-a-layer-is-solid/td-p/13806717
Special thanks to SuperMerlin for the Histogram Mean value code:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/smart-exposure-compensation-script/m-p/11092968
*/

#target photoshop

if (app.documents.length > 0) {

    // Selected layer check by jazz-y
    s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
    r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
    if (executeActionGet(r).getList(p).count) {

        function main() {

            dupeLayer("_Temp_Diff-Check");
            activeDocument.activeLayer.blendMode = BlendMode.DIFFERENCE;
            Avrg();
            var mean = meanHist(activeDocument.histogram).toFixed(0);
            if (mean != 0) {
                var notFlat = "The layer isn't a flat color!";
                alert(notFlat);
            } else {
                var isFlat = "The layer is a flat color!";
                alert(isFlat);
            }
            if (activeDocument.activeLayer.name = "_Temp_Diff-Check") {
                activeDocument.activeLayer.remove();
            }
        }

        app.activeDocument.suspendHistory("Flat color check", "main()");

    } else {
        alert('A layer must be selected!');
    }

} else {
    alert('You must have a document open!');
}


function meanHist(hist) {
    var acc = 0;
    var cnt = 0;
    for (var i = 0; i < hist.length; i++) {
        acc += i * hist[i];
        cnt += hist[i];
    }
    return acc / cnt;
}

function dupeLayer(layName) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };

    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();

    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("null"), reference);
    descriptor.putString(s2t("name"), layName);
    descriptor.putInteger(s2t("version"), 5);
    list.putInteger(6);
    descriptor.putList(s2t("ID"), list);
    executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
}

function Avrg() {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    executeAction(c2t("Avrg"), undefined, DialogModes.NO);
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 22, 2023 May 22, 2023

Copy link to clipboard

Copied

Thank you for your help, I also achieved it through one of the most cumbersome methods 

var doc = app.activeDocument;var layer = doc.activeLayer;var bounds = layer.bounds;var topLeftCorner = {    x: bounds[0].value,    y: bounds[1].value};var topRightCorner = {    x: bounds[2].value,    y: bounds[1].value};var bottomLeftCorner = {    x: bounds[0].value,    y: bounds[3].value};var bottomRightCorner = {    x: bounds[2].value,    y: bounds[3].value};var x1 = 1;var y1 = 1;var rgb1 = getRGB(x1, y1);var x2 = bottomRightCorner.x.toFixed() - 1var y2 = bottomRightCorner.y.toFixed() - 1var rgb2 = getRGB(x2, y2);var x3 = 1;var y3 = bottomLeftCorner.y.toFixed() - 1var rgb3 = getRGB(x3, y3);var x4 = bottomRightCorner.x.toFixed() - 1var y4 = 1;var rgb4 = getRGB(x4, y4);var ifrgb1 = rgb1.join("-");var ifrgb2 = rgb2.join("-");var ifrgb3 = rgb3.join("-");var ifrgb4 = rgb4.join("-");if (ifrgb1 === ifrgb2 && ifrgb1 === ifrgb3 && ifrgb1 === ifrgb4 && ifrgb2 === ifrgb3 && ifrgb3 === ifrgb4) {    alert("pure color");} else {    alert("Is not a pure color");}function getRGB(x, y) {    var pointSample = app.activeDocument.colorSamplers.add([(x - 1), (y - 1)]);    var rgb = [        pointSample.color.rgb.red,        pointSample.color.rgb.green,        pointSample.color.rgb.blue    ];    var d = new ActionDescriptor();    var r = new ActionReference();    r.putEnumerated(stringIDToTypeID("colorSampler"), stringIDToTypeID("ordinal"), stringIDToTypeID("allEnum"));    d.putReference(stringIDToTypeID("null"), r);    executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);    return rgb;}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2023 May 22, 2023

Copy link to clipboard

Copied

LATEST

@meadX 

 

Great stuff!

 

It will really come down to the images in question as to how robust the solution should be.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines