Skip to main content
Iuigidesu
Inspiring
January 2, 2025
Answered

Rounding Text Color Values?

  • January 2, 2025
  • 3 replies
  • 656 views

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!

Correct answer c.pfaffenbichler

 

 

// 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

 

 

3 replies

c.pfaffenbichler
Community Expert
January 10, 2025

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. 

c.pfaffenbichler
c.pfaffenbichlerCorrect answer
Community Expert
January 8, 2025

 

 

// 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

 

 

Iuigidesu
IuigidesuAuthor
Inspiring
January 8, 2025

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!!!!

 

c.pfaffenbichler
Community Expert
January 8, 2025

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. 

pixxxelschubser
Community Expert
January 2, 2025

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.