Mobile app version of vmapp.org
Login or Join
Shakeerah625

: Photoshop script to select specific colors Please note that I'm a complete beginner and this is my first coding question ever. If this is more appropriate on another site, tell me. I want

@Shakeerah625

Posted in: #AdobePhotoshop #Javascript #PhotoshopScripting

Please note that I'm a complete beginner and this is my first coding question ever. If this is more appropriate on another site, tell me.

I want to automate a process were the program select a colour and change it to another one.

This is the code I have, thank to the script listener:

var id159 = charIDToTypeID( "ClrR" );
var desc34 = new ActionDescriptor();
var id160 = charIDToTypeID( "Fzns" );
desc34.putInteger( id160, 0 );
var id161 = charIDToTypeID( "Mnm " );
var desc35 = new ActionDescriptor();
var id162 = charIDToTypeID( "Rd " );
desc35.putDouble( id162, 0.000000 );
var id163 = charIDToTypeID( "Grn " );
desc35.putDouble( id163, 255.000000 );
var id164 = charIDToTypeID( "Bl " );
desc35.putDouble( id164, 0.000000 );
var id165 = charIDToTypeID( "RGBC" );
desc34.putObject( id161, id165, desc35 );
var id166 = charIDToTypeID( "Mxm " );
var desc36 = new ActionDescriptor();
var id167 = charIDToTypeID( "Rd " );
desc36.putDouble( id167, 0.000000 );
var id168 = charIDToTypeID( "Grn " );
desc36.putDouble( id168, 255.000000 );
var id169 = charIDToTypeID( "Bl " );
desc36.putDouble( id169, 0.000000 );
var id170 = charIDToTypeID( "RGBC" );
desc34.putObject( id166, id170, desc36 ); executeAction( id159, desc34, DialogModes.NO );


When I run this in Photoshop it does work but since I have a lot of colours to select, I want to know


If there is a more efficient way to do it, using less lines of code. It could be either with the colour range or another tool.
I would prefer to use hexadecimal values but I have no idea how to write it. I managed to change it to RGB but it was originally using Lab colours.


In this example, the hex would be 00ff00

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shakeerah625

1 Comments

Sorted by latest first Latest Oldest Best

 

@Yeniel278

This is more of a long commentary than an answer. I am not sure selecting color range is the best way to replace color but yes you could do this.


Number of lines dont make more efficient code! One line of code can well be less efficient than a 100. Besides theres code below code so its not like your scripting language is pedal to the metal. Also the scripting API is inherently inefficient if you care for speed then its time to start wirting c++ and use photoshop plugin API.

However, i will take it you mean more practical. Yes we can define some functions to make it easier to read and use, call and slightly more condense. Although making it more practical will increase the length slightly, but this is not meaningfull for you. So let us refactor the code:
#target photoshop

selectColorRange(
RGBc(0.0, 255.0, 0.0),
RGBc(0.0, 255.0, 0.0)
);


/******* Support functions *******/

function cTID(s) { return app.charIDToTypeID(s); }
function sTID(s) { return app.stringIDToTypeID(s); }

function RGBc(r, g, b) {
var color = new ActionDescriptor();
color.putDouble( cTID("Rd "), r);
color.putDouble( cTID("Grn "), g);
color.putDouble( cTID("Bl "), b);
return color
}

function selectColorRange(color1, color2){
var desc = new ActionDescriptor();
desc.putInteger(cTID("Fzns"), 0 );
desc.putObject( cTID("Mnm "), cTID("RGBC"), color1 );
desc.putObject( cTID("Mxm "), cTID("RGBC"), color2 );
executeAction( cTID("ClrR"), desc, DialogModes.NO );
}


So now from your point of view the code is only selectColorRange. Obviously, if you only ever intend to select only one color then you could change selectColorRange to only take one input.

Also even if adobe had this as a javascipt function or object then your code wouldnt be more then 3 lines shorter most likely. Since objects need to be populated anyway.
Well this is pretty easy, you can find many implementations of this by googling. So lot us define a function to do this:

function hexC(hex) {
// stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb hex = hex.substring(1,7)
bigint = parseInt(hex, 16);
var color = new ActionDescriptor()
color.putDouble(cTID("Rd "), (bigint >> 16) & 255);
color.putDouble(cTID("Grn "), (bigint >> 8) & 255);
color.putDouble(cTID("Bl "), bigint & 255);
return color
}


So you would call hexC like this,

colorReplace(
hexC("#00ff00"),
hexC("#00ff00")
);


instead of RGBc.


Ok, so now we have all pieces in your code in place. If you have many colors then just call a for loop.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme