Labeling Objects with Their HEX Color in Adobe Illustrator Using JavaScript

Jotaze

New member
XNullUser
Joined
Jan 24, 2026
Messages
4
Reaction score
0
Points
1
Location
Ecuador
NullCash
80
When working with multiple colored objects—such as UI elements, icons, charts, or brand palettes—it’s often useful to visually identify each color by its hexadecimal value. Adobe Illustrator does not provide a built-in way to automatically label arbitrary objects with their HEX colors, but JavaScript scripting makes this task easy and repeatable.


This introductory article demonstrates a simple script that reads the fill color of selected objects and places a HEX color label centered above each one.


What You’ll Learn​


  • How Illustrator scripts access selected objects
  • How to read an object’s fill color
  • How to convert RGB values to HEX
  • How to place and align text programmatically

This example is intentionally simple and ideal for beginners exploring Illustrator automation.




Prerequisites​


Before running the script:


  1. Open a document in Adobe Illustrator.
  2. Select multiple objects that have fill colors.
  3. Ensure the document is in RGB color mode (recommended for this script).

Note: CMYK, gradients, and patterns are not handled in this introductory example.



The Script: Add HEX Labels to Selected Objects​


Save the following code as label-hex.jsx and run it via:


File → Scripts → Other Script…

JavaScript:
#target illustrator

(function () {
    if (app.documents.length === 0) {
        alert("No document open.");
        return;
    }

    var doc = app.activeDocument;
    var selection = doc.selection;

    if (!selection || selection.length === 0) {
        alert("Please select one or more colored objects.");
        return;
    }

    // Convert RGB values to HEX
    function toHex(value) {
        value = Math.round(value);
        var hex = value.toString(16).toUpperCase();
        return hex.length === 1 ? "0" + hex : hex;
    }

    function rgbToHex(rgb) {
        return "#" +
            toHex(rgb.red) +
            toHex(rgb.green) +
            toHex(rgb.blue);
    }

    // Create or reuse a layer for labels
    var labelLayer;
    try {
        labelLayer = doc.layers.getByName("HEX Labels");
    } catch (e) {
        labelLayer = doc.layers.add();
        labelLayer.name = "HEX Labels";
    }

    for (var i = 0; i < selection.length; i++) {
        var item = selection[i];

        if (!item.filled || item.fillColor.typename !== "RGBColor") {
            continue;
        }

        var hexValue = rgbToHex(item.fillColor);
        var bounds = item.visibleBounds;

        // Center position above the object
        var centerX = (bounds[0] + bounds[2]) / 2;
        var topY = bounds[1] + 12;

        var text = labelLayer.textFrames.add();
        text.contents = hexValue;
        text.textRange.size = 10;

        // Center-align text
        text.paragraphs[0].paragraphAttributes.justification =
            Justification.CENTER;

        // Position text
        text.position = [
            centerX - text.width / 2,
            topY
        ];
    }

})();

What the Script Does​

  1. Reads the current selection.
  2. Extracts the RGB fill color of each object.
  3. Converts RGB values into a HEX string.
  4. Creates a text label showing the HEX value.
  5. Places the label centered above the object.
  6. Keeps labels organized in a dedicated layer (HEX Labels).

Why This Is Useful​

  • Faster color auditing and reviews
  • Clear visual documentation for design systems
  • Helpful for handoff between designers and developers
  • Eliminates repetitive manual labeling

Next Steps​

Once you’re comfortable with this example, you can extend it by:
  • Supporting CMYK colors
  • Automatically matching text color for contrast
  • Adding background shapes to labels
  • Reading stroke colors instead of fill colors
  • Exporting color data to JSON or CSV

This script is a great first step into using JavaScript as a productivity tool inside Adobe Illustrator—simple, safe, and immediately useful.
 
Top