Last active
November 8, 2024 14:13
-
-
Save creold/2b7684dbc194b406de77681eb0f93b72 to your computer and use it in GitHub Desktop.
Recolor text lines in specified HEX, RGB, or CMYK colors or in selected swatches. Adobe Illustrator script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Recolor text lines in specified HEX, RGB, or CMYK colors or in selected swatches | |
Discussion: https://community.adobe.com/t5/illustrator-discussions/can-i-automatically-colorize-every-text-line-in-a-text-with-a-selected-color-swatch/m-p/14152682#M383649 | |
Author: Sergey Osokin, email: [email protected] | |
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 Donatty https://donatty.com/sergosokin | |
- via DonatePay https://new.donatepay.ru/en/@osokin | |
- via YooMoney https://yoomoney.ru/to/410011149615582 | |
*/ | |
//@target illustrator | |
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false); // Fix drag and drop a .jsx file | |
// Main function | |
function main() { | |
// Separate colors in an array with a comma. No comma after the last | |
// RGB format inside square brackets [10, 100, 250] | |
// CMYK format inside square brackets [100, 0, 20, 50] | |
// HEX inside double quotes "#0000FF" | |
var colorArr = [ | |
"#0000FF", | |
"#37AA32", | |
"#F39100" | |
]; | |
var isShuffle = false; // Randomize color array | |
if (!documents.length) return; | |
var doc = app.activeDocument; | |
var tfs = getTextFrames(app.selection); | |
var selSw = doc.swatches.getSelected(); | |
var hasSelSw = selSw.length > 0; | |
// If doc has selected swatches, use them | |
if (hasSelSw) colorArr = selSw; | |
// Set colors | |
for (var j = 0; j < colorArr.length; j++) { | |
colorArr[j] = hasSelSw ? colorArr[j].color : setColor(colorArr[j]); | |
} | |
if (isShuffle) shuffle(colorArr); | |
for (var j = 0; j < tfs.length; j++) { | |
var tfLines = tfs[j].textRange.lines; | |
for (var k = 0; k < tfLines.length; k++) { | |
var idx = k % colorArr.length; | |
tfLines[k].fillColor = colorArr[idx]; | |
} | |
} | |
} | |
// Get TextFrames array from collection | |
function getTextFrames(coll) { | |
var tfs = []; | |
for (var i = 0, len = coll.length; i < len; i++) { | |
if (/text/i.test(coll[i].typename)) | |
tfs.push(coll[i]); | |
else if (/group/i.test(coll[i].typename)) | |
tfs = tfs.concat(getTextFrames(coll[i].pageItems)); | |
} | |
return tfs; | |
} | |
// Generate color | |
function setColor(data) { | |
var isRGB = activeDocument.documentColorSpace == DocumentColorSpace.RGB; | |
var c = isRGB ? new RGBColor() : new CMYKColor(); | |
if (data instanceof Array) { | |
if (isRGB) { | |
c.red = data[0] !== "undefined" ? getValidRGB(data[0]) : 0; | |
c.green = data[1] !== "undefined" ? getValidRGB(data[1]) : 0; | |
c.blue = data[2] !== "undefined" ? getValidRGB(data[2]) : 0; | |
} else { | |
c.cyan = data[0] !== "undefined" ? getValidCMYK(data[0]) : 100; | |
c.magenta = data[1] !== "undefined" ? getValidCMYK(data[1]) : 100; | |
c.yellow = data[2] !== "undefined" ? getValidCMYK(data[2]) : 100; | |
c.black = data[3] !== "undefined" ? getValidCMYK(data[3]) : 100; | |
} | |
} else if (typeof data === "string" && data.match(/#[0-9A-Fa-f]{1,6}/g)) { | |
var arr = hex2rgb(data); | |
if (!isRGB) arr = rgb2cmyk(arr); | |
return setColor(arr); | |
} | |
return c; | |
} | |
// Check number is in the range 0-255 | |
function getValidRGB(n) { | |
if (n > 255) return 255; | |
if (isNaN(n) || n == "" || n < 0) return 0; | |
return n; | |
} | |
// Check number is in the range 0-100 | |
function getValidCMYK(n) { | |
if (n > 100) return 100; | |
if (isNaN(n) || n == "" || n < 0) return 0; | |
return n; | |
} | |
// Convert HEX to RGB color | |
function hex2rgb(hex) { | |
var hex = hex.replace(/[^0-9A-F]/gi, ""); | |
if (hex.length == 3) { | |
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; | |
} else if (hex.length && hex.length < 6) { | |
hex = ("000000" + hex).slice(-6); | |
} | |
return hex.length ? [ | |
parseInt(hex.substr(0, 2), 16), | |
parseInt(hex.substr(2, 2), 16), | |
parseInt(hex.substr(4, 2), 16) | |
] : []; | |
} | |
// Convert RGB to CMYK color | |
function rgb2cmyk(rgb) { | |
var arr = convertColor("RGB", "CMYK", rgb); | |
for (var i = 0, len = arr.length; i < len; i++) { | |
arr[i] = Math.round(arr[i]); | |
} | |
return arr; | |
} | |
// Convert color via native converter | |
function convertColor(src, dest, srcColor) { | |
return app.convertSampleColor(ImageColorSpace[src], srcColor, ImageColorSpace[dest], ColorConvertPurpose.defaultpurpose); | |
} | |
// Shuffle array | |
function shuffle(arr) { | |
var j, tmp; | |
for (var i = arr.length - 1; i > 0; i--) { | |
j = Math.floor(Math.random() * (i + 1)); | |
tmp = arr[j]; | |
arr[j] = arr[i]; | |
arr[i] = tmp; | |
} | |
return arr; | |
} | |
// Run script | |
try { | |
main(); | |
} catch (e) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More about script in Telegram channel.