Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rounding Text Color Values?

Explorer ,
Jan 02, 2025 Jan 02, 2025

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!

TOPICS
Actions and scripting
585
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 08, 2025 Jan 08, 2025

 

Screenshot 2025-01-08 at 14.11.32.pngScreenshot 2025-01-08 at 14.12.06.png

 

// 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"));
};
////////////////////////////////////
///////////////////
...
Translate
Adobe
Community Expert ,
Jan 02, 2025 Jan 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2025 Jan 08, 2025

 

Screenshot 2025-01-08 at 14.11.32.pngScreenshot 2025-01-08 at 14.12.06.png

 

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

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 08, 2025 Jan 08, 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!!!!

Iuigidesu_2-1736355769186.pngIuigidesu_3-1736355792728.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2025 Jan 08, 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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2025 Jan 08, 2025
quote

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2025 Jan 09, 2025

I edited the code in the previous post (to avoid having multiple versions of code on the page), please give it a try. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2025 Jan 09, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2025 Jan 09, 2025

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% 

Screenshot 2025-01-10 at 08.18.40.pngScreenshot 2025-01-10 at 08.20.01.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2025 Jan 10, 2025
LATEST

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines