Using Illustrator Scripts to Modify Colors

Automating color changes across Illustrator artwork with ExtendScript — understanding RGB and CMYK color models and scripting color transformations.

← Back to Blog

Why Script Color Changes?

Manually changing colors across large Illustrator documents — particularly when a brand recolor or a spot color substitution is needed across hundreds of objects — is time-consuming and error-prone. Illustrator's scripting support (via ExtendScript and the Illustrator DOM) makes it straightforward to automate these changes systematically.

RGB vs CMYK in Illustrator

Before writing color scripts, it's important to understand how Illustrator represents color internally:

  • RGB colors are represented as three values (red, green, blue), each in the range 0–255. Used for screen-based and web output.
  • CMYK colors are represented as four values (cyan, magenta, yellow, black), each in the range 0–100. Used for print output.
  • Spot colors are named colors (e.g. Pantone values) with a tint value. They cannot be directly converted to RGB/CMYK without losing the spot color identity.

A document can contain objects with mixed color modes. Your script needs to handle each type appropriately.

Accessing Colors via ExtendScript

In the Illustrator scripting DOM, path items have fillColor and strokeColor properties. The color type determines which properties are available:

var doc = app.activeDocument;
var items = doc.pathItems;

for (var i = 0; i < items.length; i++) {
    var item = items[i];
    if (item.filled) {
        var color = item.fillColor;
        if (color.typename === "RGBColor") {
            // Access color.red, color.green, color.blue
        } else if (color.typename === "CMYKColor") {
            // Access color.cyan, color.magenta, color.yellow, color.black
        } else if (color.typename === "SpotColor") {
            // Access color.spot.name, color.tint
        }
    }
}

Replacing a Specific Color

To replace all objects with a specific RGB color with a new color:

function replaceRGBColor(doc, oldR, oldG, oldB, newR, newG, newB) {
    var items = doc.pathItems;
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.filled && item.fillColor.typename === "RGBColor") {
            var c = item.fillColor;
            if (Math.round(c.red) === oldR &&
                Math.round(c.green) === oldG &&
                Math.round(c.blue) === oldB) {
                var newColor = new RGBColor();
                newColor.red = newR;
                newColor.green = newG;
                newColor.blue = newB;
                item.fillColor = newColor;
            }
        }
    }
}

replaceRGBColor(app.activeDocument, 255, 0, 0, 0, 0, 255); // Replace red with blue

Iterating Through All Object Types

The example above only covers top-level path items. A complete solution needs to handle grouped objects and other container types recursively:

function processItem(item) {
    if (item.typename === "PathItem") {
        // Apply color change
    } else if (item.typename === "GroupItem") {
        for (var i = 0; i < item.pageItems.length; i++) {
            processItem(item.pageItems[i]);
        }
    } else if (item.typename === "CompoundPathItem") {
        for (var i = 0; i < item.pathItems.length; i++) {
            processItem(item.pathItems[i]);
        }
    }
}

var doc = app.activeDocument;
for (var i = 0; i < doc.pageItems.length; i++) {
    processItem(doc.pageItems[i]);
}

Converting Between Color Modes

Converting a CMYK color to RGB requires a conversion formula. Note that the conversion is not perfectly reversible due to the gamut differences between the two color spaces. For accurate results, use Illustrator's built-in color management rather than manual formula conversion wherever possible.

Further Reading

For more on Illustrator scripting and color handling, see the Adobe Illustrator Scripting Reference. For custom color automation solutions, contact Mapsoft — we build bespoke Illustrator scripts and plugins for production workflows. See also our article on Combining Illustrator Extension Technologies.

Custom Illustrator Automation

Mapsoft builds Illustrator scripts, plugins, and batch processing solutions for design and production workflows.