Specific Color Replacement Script
Copy link to clipboard
Copied
I need to replace a specific RGB value with another specific RGB value multiple times in Photoshop. I know this can be accomplished with an action but due to the volume of color adjustments (1000+), I want to make a script. My current thought process for the script is to make a selection of the original RBG value, set the foreground color to corrected RGB value, and then fill the selection with foreground color. I can get the last two parts working but I don't know how to make the initial selection based off of color. What is the best way to accomplish this? Is there a way to script based off color range,load in .axt files, or replace color? Or anything else to make the selections? I've included mock example below of the changes that need to be made.
Explore related tutorials & articles
Copy link to clipboard
Copied
The problem I have is I do not know how one would select a paticular RGB color. When I recorded a Select Color Range ssample color the Action manager script code recorded seem to use Labcolor even though my document editing mode was RGB and color space sRGB in 16bit color depth. One click sample sample sie set to point yet a range seemed to be recorded. b abd b2 are not the same nor doe I know what colorModel 0 means
// =======================================================
colorRange(0, 90.44, -52.72, -10.47, 90.44, -52.72, -10.47, 0);
function colorRange(fuzziness, luminance, a, b, luminance2, a2, b2, colorModel) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
descriptor.putInteger( stringIDToTypeID( "fuzziness" ), fuzziness );
descriptor2.putDouble( stringIDToTypeID( "luminance" ), luminance );
descriptor2.putDouble( stringIDToTypeID( "a" ), a );
descriptor2.putDouble( stringIDToTypeID( "b" ), b );
descriptor.putObject( stringIDToTypeID( "minimum" ), stringIDToTypeID( "labColor" ), descriptor2 );
descriptor3.putDouble( stringIDToTypeID( "luminance" ), luminance2 );
descriptor3.putDouble( stringIDToTypeID( "a" ), a2 );
descriptor3.putDouble( stringIDToTypeID( "b" ), b2 );
descriptor.putObject( charIDToTypeID( "Mxm " ), stringIDToTypeID( "labColor" ), descriptor3 );
descriptor.putInteger( stringIDToTypeID( "colorModel" ), colorModel );
executeAction( stringIDToTypeID( "colorRange" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
Bonjour
I use this feature given by Mike Hale
function selectColorRange(scObj){
var desc = new ActionDescriptor();
desc.putInteger( charIDToTypeID( "Fzns" ), 0 );
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 );
}
See HERE
Copy link to clipboard
Copied
That does seem to selec a color but does not through an error when nothing is selected so I would think when you use that function you need to test if a selection has been made before your fill. You may need to deselect before using function. However, it does seem to deselect.
Copy link to clipboard
Copied
With Try catch ?
Copy link to clipboard
Copied
No the function does not through a error I believe others have posted functions to test there is a selection. If the document has a selection. You would need to use something like that.
For example I tried using selection bounds white to red you could mak an array of colors and use a for loop. My test worked in CS3 and CC 2019 would most likely fail ine CS2 selection bound had a bug in it.
It seem to work in CC versions r-bin found the CC Scriptlistener records Lab color Like found and posted hot to convert to use RGB. However Mike code see to wotk in both CS and CC versions. Yoy do need to deselect after to do the fill. For the next select to work correctly. You may not need the refresh();
ChangeColors = [
[97,33,150,107,58,160],
[99,34,151,108,59,161],
[101,35,152,109,60,162],
[103,36,153,110,60,163],
[105,37,154,111,60,164],
[107,38,155,112,60,165],
[109,39,156,113,60,166],
[111,40,157,114,60,167],
[113,41,158,115,60,168],
[115,42,159,116,60,169],
[117,43,160,117,60,170],
[119,44,161,118,60,171],
[121,45,162,119,70,172],
[123,46,163,120,71,173],
[125,47,164,121,72,174],
[127,48,165,122,73,175],
[129,49,166,123,74,176],
[131,50,167,124,75,177],
[133,51,168,125,76,178],
[135,52,169,126,77,179],
[137,53,170,127,78,180],
[139,54,171,128,79,181],
[141,55,172,129,80,182],
[143,56,173,130,81,183],
[145,57,174,131,82,184],
[147,58,175,132,83,185],
];
CurColor = new SolidColor();
ReplaceClr = new SolidColor();
for (var i = 0; ChangeColors.length > i; i++) {
CurColor.rgb.red = ChangeColors[0];
CurColor.rgb.green = ChangeColors[1];
CurColor.rgb.blue = ChangeColors[2];
ReplaceClr.rgb.red = ChangeColors[3];
ReplaceClr.rgb.green = ChangeColors[4];
ReplaceClr.rgb.blue = ChangeColors[5];
selectColorRange(CurColor);
if (hasSelection()){
activeDocument.selection.fill(ReplaceClr);
activeDocument.selection.deselect();
refresh();
}
//else alert("Nothing Selected");
}
function selectColorRange(scObj){
var desc = new ActionDescriptor();
desc.putInteger( charIDToTypeID( "Fzns" ), 0 );
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() {
try {
var SB = activeDocument.selection.bounds; // Get Selection bounds
return true;
}
catch(e) { return false}
}
Copy link to clipboard
Copied
Bonjour
I just intended to provide the function (CS and CS2) but not to realize the question asked!
Thanks to you JJMack !! !
Copy link to clipboard
Copied
I no longer have CS2 installed. I know Selection bounds was broken in CS2 and I had to program around it. You may need to create a different function hasselection() for CS2. I did not do any Scripting in PS7 or CS. Scripting was an optional download plug-in in PS7 and new in CS.

