• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Can I automatically colorize every text line in a text with a selected color swatch?

Community Beginner ,
Oct 09, 2023 Oct 09, 2023

Copy link to clipboard

Copied

Hello, 

 

I'm searching help for a task, I want to generate a batch of text images in which each individual text line is colored differently (in a paragraph text). If I create a text and then put a different color on each text line there is no problem but if I want to use variables to produce different text it applies the same effect for the full text, this is forcing me to enter each Ai file and re color. I'm curious if there's a method or a script to automatically ensure that each successive text line receives a unique color. For example, I want the first line of the text to be bleu, second to be green, thirt to be orange, and then it repeats, forth line to be bleu, fifth to be green, sixth to be orange, etc

Also, I would like to color the text selecting a swatch group.

 

Example:

 

Original text:

Hello, how are you?! I'm good

 

Expected text:

Hello, how

are you?!

I'm good

 

Thank you in advance

TOPICS
Scripting

Views

272

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
Oct 09, 2023 Oct 09, 2023

Copy link to clipboard

Copied

 

 

//@target illustrator
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false); // Fix drag and drop a .jsx file

// Main function
function main() {
  // Define the color array in the desired order
  // RGB format [10, 100, 250]
  // CMYK format [100, 0, 20, 50]
  // HEX with quotes "#0000FF"
  var colorArr = [
    "#0000FF",
    "#37AA32",
    "#F39100"
  ];

  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]);
  }

  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;
}

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);
}

// Run script
try {
  main();
} catch (e) {}

 

recolorLines.gif

Upd 12-oct: Added HEX support and array number validation for RGB / CMYK formats.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2023 Oct 10, 2023

Copy link to clipboard

Copied

Thank you, tested and this works very well

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2023 Oct 11, 2023

Copy link to clipboard

Copied

Hi again, I wanted to know if it is also possible, instead of selecting the swatches, to add a list of hexadecimal color codes (lets say 004fab, 00cfb3, ff7000, 14a41c) on the top of the script that would be used to color the lines?

Thank you in advance

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 11, 2023 Oct 11, 2023

Copy link to clipboard

Copied

I've changed the code in the original post above so I don't have to write a new post.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

Oh I just realize that you are the guy from the youtube script videos that I was watching some time ago, and thank you again! 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

Yes, it's me. I hope you will like and subscribe to my channel after watching? 😄

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

LATEST

Hehe already did that 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines