if active layer contains a range of colors?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

christopherjohnson

if active layer contains a range of colors?

Post by christopherjohnson »

Does anyone have any scripts to check to see if an image contains a range of colors in the color scheme?

I like to make designs match artworks so I have multiple color versions. If I could have the script check the colors of the image I could save fewer versions (and save harddrive space!). in other words not apply a green design on an image the has less than 10%. Not apply purple design on the image that doesn't have purple or pink, etc.

another thing I'd like that is related would be a script that could extract the main colors of the activelayer/background/layer0 and apply them in stripes to a new image of the same dimensions.
Mike Hale

if active layer contains a range of colors?

Post by Mike Hale »

You can use select-color range to determine if a document has colors in that range. But unless you have an extended version of Photoshop it is hard to tell what percentage of the image is in that color range.

Do you have Illustrator? It does a better job of extracting colors from an image. And I am not clear what you mean by main colors. Colors that take up larger percentages? Of just colors in the subject/product.
christopherjohnson

if active layer contains a range of colors?

Post by christopherjohnson »

by main colors the colors that are in the majority of the image. a pink flower image would probably have pink (petals) and green (leaves) perhaps blue for the sky or some other color for a background.

how do you use select-color range to see if the range of colors exist in the current layer (which would most likely be background or layer 0?

I'd like to for example if image contains blue, apply blue design, if it contains orange apply orange design. if it contains purple or pink apply mauve design... so I need to find a way to see if the color is there and well then have my existing script do its thing. This would help a lot since right now I'm applying all designs I think might work within limits and then have a bunch of files I don't really need filling up my harddrive.
Mike Hale

if active layer contains a range of colors?

Post by Mike Hale »

To use select-color range you need to use Action Manager. One way would be to install the scriptlistener plugin that ships with Photoshop. Once install just use the menu item to select a color range as you would normally do. Then look in the log file on the desktop to get the code to select that color. If you need to select/test for a lot of colors you could make a function using the log code as a starting point. Here is an example
Code: Select allfunction selectColorRange(scObj,fuzz){// solidColor object, integer
    var desc = new ActionDescriptor();
    desc.putInteger( charIDToTypeID( "Fzns" ), fuzz );
        var cDesc = new ActionDescriptor();
        cDesc.putDouble( charIDToTypeID( "Rd  " ), scObj.rgb.red);
        cDesc.putDouble( charIDToTypeID( "Grn " ), scObj.rgb.green);
        cDesc.putDouble( charIDToTypeID( "Bl  " ), scObj.rgb.blue );
    desc.putObject( charIDToTypeID( "Mnm " ), charIDToTypeID( "RGBC" ), cDesc );
    desc.putObject( charIDToTypeID( "Mxm " ), charIDToTypeID( "RGBC" ), cDesc );
   executeAction( charIDToTypeID( "ClrR" ), desc, DialogModes.NO );
}
christopherjohnson

if active layer contains a range of colors?

Post by christopherjohnson »

so doing that will be able to tell me (my script) if orange is present in the current active layer or if blue is present?
I'm sorry but I really don't want a script to give me a log. I want to see if orange or blue, etc is one of the main colors and then do something in my script based on that info.
Mike Hale

if active layer contains a range of colors?

Post by Mike Hale »

The log is from the scriptlistener plugin. There are some things you can only do in a script with Action Manager. That plugin converts what you do in Photoshop into Action Manager code.

Select-color range will try to select a color. If there is no selection there is no area in the document that has that color. If the color does exists the selection will give you a rough idea of where the color is in the document. So yes if you just want a yes/no answer using color range then checking the selection will tell you if the color is in the document or not. But keep in mind that if there are only say 5 blue pixels in a 3000x3000px image that method will say there is blue in the document even if the eye doesn't see it.

If you have an extended version of Photoshop you can get the area of the selection to determine the color's percentage of document. If you don't then you have to use just the selection bounds which can be way off in terms of area.

Code: Select allvar blue = new SolidColor();
blue.rgb.red = 0;
blue.rgb.green = 0;
blue.rgb.blue = 255;
selectColorRange(blue,32);
var hasColor = hasSelection();
if(hasColor){
    // do something because the document has blue
    alert('has blue');
}


function selectColorRange(scObj,fuzz){// solidColor object, integer
    var desc = new ActionDescriptor();
    desc.putInteger( charIDToTypeID( "Fzns" ), fuzz );
        var cDesc = new ActionDescriptor();
        cDesc.putDouble( charIDToTypeID( "Rd  " ), scObj.rgb.red);
        cDesc.putDouble( charIDToTypeID( "Grn " ), scObj.rgb.green);
        cDesc.putDouble( charIDToTypeID( "Bl  " ), scObj.rgb.blue );
    desc.putObject( charIDToTypeID( "Mnm " ), charIDToTypeID( "RGBC" ), cDesc );
    desc.putObject( charIDToTypeID( "Mxm " ), charIDToTypeID( "RGBC" ), cDesc );
   executeAction( charIDToTypeID( "ClrR" ), desc, DialogModes.NO );
};
function hasSelection(){
   var res = false;
   try{
      app.activeDocument.selection.bounds;
      res = true;
   }catch(e){}
   return res;
};
christopherjohnson

if active layer contains a range of colors?

Post by christopherjohnson »

I've been experimenting with it, seem to either get no result or false positives even when I simplify the image with the mosaic filter. It is funny that something so easy for us is so complicated for Photoshop.
Thank you for the example code.
christopherjohnson

if active layer contains a range of colors?

Post by christopherjohnson »

I give up on this. I can't get even 50% accuracy. I found another script here in german that I've had to heavily modify, but even that only gets around 70% right.