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.
This example is intentionally simple and ideal for beginners exploring Illustrator automation.
Before running the script:
Save the following code as label-hex.jsx and run it via:
File → Scripts → Other Script…
This script is a great first step into using JavaScript as a productivity tool inside Adobe Illustrator—simple, safe, and immediately useful.
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:
- Open a document in Adobe Illustrator.
- Select multiple objects that have fill colors.
- 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
- Reads the current selection.
- Extracts the RGB fill color of each object.
- Converts RGB values into a HEX string.
- Creates a text label showing the HEX value.
- Places the label centered above the object.
- 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.