Copy link to clipboard
Copied
Hi everyone,
I’m back with another very specific request.
Someone on my team accidentally altered the colors of some text layers. We usually work with solid black (#000000) and white (#FFFFFF), but now some layers are slightly off (e.g., #0A0A0A or #FCFCFC). These subtle variations are causing issues with our stroke automation and are a nightmare to spot and fix manually.
Is there a way to automatically adjust these text layer colors to the nearest solid black or white?
I’m dealing with about 200 documents, so manually fixing them one by one, layer by layer, even with actions, isn’t really practical.
Any tips or solutions would be greatly appreciated. Thanks in advance, and happy new year!
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theMode = myDocument.mode;
switch (theMode) {
case DocumentMode.RGB:
var theValue = 128;
var theTotal = 255;
break;
case DocumentMode.GRAYSCALE:
var theValue = 50;
var theTotal = 100;
break;
default:
break;
};
var theChangedLayers = changeColorOfTypeLayers ();
alert (theChangedLayers.length+"\n\n"+theChangedLayers.join("\n"));
};
////////////////////////////////////
///////////////////
...
Copy link to clipboard
Copied
Actions cannot make if-else decisions.
Only a script can do that.
According to the scheme:
Document open - yes
Text layers available - yes
Loop through the text layers
if text color values RGB below 128 128 128 -> 000 000 000
if not,
if text color values RGB above 128 128 128 -> 255 255 255
next text layer
Alert or save
However, if only individual words or letter colors differ, the whole thing becomes much more difficult and complex.
Copy link to clipboard
Copied
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theMode = myDocument.mode;
switch (theMode) {
case DocumentMode.RGB:
var theValue = 128;
var theTotal = 255;
break;
case DocumentMode.GRAYSCALE:
var theValue = 50;
var theTotal = 100;
break;
default:
break;
};
var theChangedLayers = changeColorOfTypeLayers ();
alert (theChangedLayers.length+"\n\n"+theChangedLayers.join("\n"));
};
////////////////////////////////////
////////////////////////////////////
function changeColorOfTypeLayers () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if type layer;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && layerDesc.hasKey(stringIDToTypeID("textKey")) == true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var rangeList = textDesc.getList(stringIDToTypeID('textStyleRange'));
//var paragraphList = textDesc.getList(stringIDToTypeID('paragraphStyleRange'));
var theColor = rangeList.getObjectValue(0).getObjectValue(stringIDToTypeID('textStyle')).getObjectValue(stringIDToTypeID('color'));
if (theTotal == 255) {
var theRed = theColor.getDouble(stringIDToTypeID('red'));
var theGreen = theColor.getDouble(stringIDToTypeID('grain'));
var theBlue = theColor.getDouble(stringIDToTypeID('blue'));
var theBright = (theRed + theGreen + theBlue) / 3;
} else {
var theBright = theColor.getDouble(stringIDToTypeID('gray'));
};
// change color;
if (theBright != 0 && theBright != theTotal) {
selectLayerByID(theID, false);
var newColor = new SolidColor ();
if (theBright < theValue) {
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
if (theTotal == 100) {newColor.gray.gray = 0}
} else {
newColor.rgb.red = 255;
newColor.rgb.green = 255;
newColor.rgb.blue = 255;
if (theTotal == 100) {newColor.gray.gray = 100}
};
myDocument.activeLayer.textItem.color = newColor
theLayers.push([theName, theID])
};
};
}
catch (e) {};
};
return theLayers
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
edited 2025-01-09
Copy link to clipboard
Copied
It works perfectly, thanks!!
I guess for the way it's coded it doesn't work in Grayscale images, which most of my documents are. So I set up an action that converts the document to RGB, runs the script and then converts the document back to Grayscale, so now it's working all good.
Thank you so much!!!!
Copy link to clipboard
Copied
I would not recommend that approach! (Maybe I am overly cautious …)
Adapting the Script for grayscale images should not be a problem, but I won‘t be able to do so today.
Copy link to clipboard
Copied
I would not recommend that approach! (Maybe I am overly cautious …) .
By @c.pfaffenbichler
If @Iuigidesu is happy that the conversion from Grayscale > RGB > Grayscale isn't shifting values or otherwise having a negative impact (dither?), then all good.
One can bypass ICC based colour conversion in the action with some long winded steps if needed.
Otherwise having separate scripts or a script that conditionally process based on colour mode would be ideal.
Copy link to clipboard
Copied
I edited the code in the previous post (to avoid having multiple versions of code on the page), please give it a try.
Copy link to clipboard
Copied
Hey, It works perfectly now on my documents, thanks!!!!
Just wondering, why isn't recommendable to convert from Grayscale to RGB and then to Grayscale back again? Does it messes up something? I couldn't find a single thing that changed or looked different.
Copy link to clipboard
Copied
Multiple Color Conversions can result in changes, impercetible maybe, but if they can be avoided I’d prefer to.
To verify whether this applies you can
• open a grayscale image
• Image > Duplicate
• Image > Mode > RGB
• drag to original image
• set to Blend Mode »Difference«
• view at least at VIew > 100%
Copy link to clipboard
Copied
One more point:
If there are Type Layers that combine faux-black and faux-white text the Script in its current form would disregard that and use the color of the first letter/s.
If this a likely scenario the Script would need some further amendments.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now