Try to loop through all objects selected and adjust the color based on a variable.
Hey y'all,
I'm trying to create a script that will cycle through all the items in a document and change them to either Black (RGB 0,0,0) or White (RGB 255,255,255) based on the sum of their RGB values.
If the sum of all RGB values >= 500, turn object white.
If the sum of all RGB values < 500, turn the object black.
I have a functional script that I will post below that works for a single object, but I can't get it to loop through all objects.
I've tried tweaking it with these methods with no luck.
For Loops
While Loops
pathItems
selection
run script and lock selection, select all, and repeat.
Any help is appreciated!
doc = app.activeDocument;
var sel = doc.selection[0];
var colorSum = sel.fillColor.red + sel.fillColor.green + sel.fillColor.blue;
if (colorSum >= 500 ) {
var whiteFillColor = new RGBColor();
whiteFillColor.red = 255;
whiteFillColor.green = 255;
whiteFillColor.blue = 255;
sel.fillColor = whiteFillColor;}
else{
var blackFillColor = new RGBColor();
blackFillColor.red = 0;
blackFillColor.green = 0;
blackFillColor.blue = 0;
sel.fillColor = blackFillColor;}