Skip to content

Instantly share code, notes, and snippets.

@creold
Last active March 12, 2024 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creold/44b75fead3602545510ffc22c270cbbd to your computer and use it in GitHub Desktop.
Save creold/44b75fead3602545510ffc22c270cbbd to your computer and use it in GitHub Desktop.
Export selection to PNG with HEX color name in Adobe Illustrator
/*
Separate export of selected paths or text to PNG with HEX color name
Discussion: https://community.adobe.com/t5/illustrator-discussions/workflow-is-there-a-faster-way-to-change-the-color-of-a-layer-on-multiple-images-randomly/td-p/12872250
Author: Sergey Osokin, email: hi@sergosokin.ru
Check my other scripts: https://github.com/creold
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee https://www.buymeacoffee.com/aiscripts
- via DonatePay https://new.donatepay.ru/en/@osokin
- via Donatty https://donatty.com/sergosokin
- via YooMoney https://yoomoney.ru/to/410011149615582
- via QIWI https://qiwi.com/n/OSOKIN
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
// Main function
function main() {
if (!documents.length) return;
if (selection.length == 0 || selection.typename == 'TextRange') return;
var doc = activeDocument,
docSel = selection,
docFolder = Folder(doc.path).exists ? doc.path : Folder.desktop,
isReplaceFile = false; // Replace when names match
var exportOptions = new ExportOptionsPNG24();
exportOptions.antiAliasing = true;
exportOptions.transparency = true;
selection = null;
for (var i = 0, len = docSel.length; i < len; i++) {
exportAsPNG(docSel[i], docFolder, isReplaceFile, exportOptions);
}
}
// Export single item
function exportAsPNG(item, folder, isReplace, exportOptions) {
var rgb = {};
if (item.typename == 'CompoundPathItem' && item.pathItems) {
rgb = item.pathItems[0].fillColor;
} else if (item.typename == 'PathItem') {
rgb = item.fillColor;
} else if (item.typename == 'TextFrame') {
rgb = item.textRange.fillColor;
} else {
return;
}
if (rgb == undefined || rgb.typename !== 'RGBColor') return;
var hex = rgb2Hex(rgb.red, rgb.green, rgb.blue).toUpperCase(),
fileName = folder + '/' + hex,
file = File(fileName + '.png');
if (file.exists && !isReplace) {
var counter = 2;
do {
var newName = fileName + '_' + counter++;
file = File(newName + '.png');
} while (file.exists)
}
item.selected = true;
app.activeDocument.exportSelectionAsPNG(file, exportOptions);
selection = null;
}
// https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function rgb2Hex(r, g, b) {
return componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? '0' + hex : hex;
}
// Run script
try {
main();
} catch (e) {}
@creold
Copy link
Author

creold commented Apr 17, 2022

demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment