Skip to main content
Participant
January 11, 2012
Answered

Assorted "color book" in Illustrator

  • January 11, 2012
  • 1 reply
  • 1520 views

Hello all,

I have been trying to create an assorted color book, essentially an array of differently colored rectangles with their respective CMYK formulas displayed underneath.  Currently, I have an excel document as well as a "tab-delimited" text document of a very large quantity of CMYK combinations that I would like to be able to make the aformentioned color book from.  Creating the rectangles and entering their formulas by hand would be possible, but also unrealistic for this large of a quantity so I am trying to automate the process in any way that I can.  Unfortunately, I am not very skilled with scripting in javascript, so I am looking for some help.  I know enough about programming to be able to make small changes to fine tune a script so any help would be appreciated

Essentially I would be looking for the script to take the formula, make a square with that CMYK combination, create a text box with that formula typed out underneath the block, and then keep repeating the previous steps for the remaining combinations by placing the blocks in a line.

http://forums.adobe.com/message/2877951

http://forums.adobe.com/message/3813617

The links above contain code that are similar to what i have been trying to work with before making this post.

I would greatly appreciate any help with this project and thank you all in advance for your help

This topic has been closed for replies.
Correct answer Jongware

Here you go, one Javascript to fit. It creates a new document in CMYK mode, with the required number of artboards. (Getting that right took more time than writing the entire drawing stuff, too!)

You can change the page and rectangle sizes near the top; adjust to fit your needs.

I do hope, though, you are using CS4 or older -- I can't test but I bet it will draw everything upside down for CS5!

//DESCRIPTION:Make Color Book From CSV

// A Jongware Script 15-Jan-2011

// Large parts for reading CSV shamelessly pilfered from

// Muppet Mark, http://forums.adobe.com/message/2877951#2877951

// The CSV is assumed to have this format:

// Cyan,Magenta,Yellow,Black

// separated by commas, color values in percents (so 100 = full)

// This sets up a landscape A4 document. Adjust for your own needs.

// My values are in mm, so they need converting to points for Illy.

// You can also enter sizes in inches -- use in2pt for that, as in:

// width = in2pt (8.5)

// height = in2pt (11);

// to set Letter size

// or use cm2pt(..) if you prefer centimeters

// ... no need to convert points, so if you like these

// simply enter

// width = 800;          // value is in points

width = mm2pt (297);

height = mm2pt (210);

// Size and distance per color rectangle

// The vertical distance needs to be enough for

// your by-line to fit.

rectwidth = mm2pt(20);

rectheight = mm2pt(15);

distancex = mm2pt (4);

distancey = mm2pt (8);

// These get recalculated to center the entire block

// of colors nicely on the page size.

leftoffset = 0;

topoffset = 0;

if (isOSX())

{

          var csvFile = File.openDialog('Select a CSV File', function (f) { return !f.hidden && ((f instanceof Folder) || f.name.match(/\.csv$/i));} );

} else

{

          var csvFile = File.openDialog('Select a CSV File','comma-separated-values(*.csv):*.csv;');

}

if (csvFile != null)

{

          fileArray = readInCSV(csvFile);

          var columns = fileArray[0].length;

//          alert('CSV file has ' + columns + ' columns…');

          var rows = fileArray.length;

//          alert('CSV file has ' + rows + ' rows…');

          if (columns == 4 && rows > 0)

          {

                    doc = createDocument (fileArray.length);

                    if (doc)

                              drawRects (doc, fileArray);

                    else

                              alert ("Rectangle and Page Sizes do not add up!");

          } else

          {

                    alert ("CSV is not in the expected format");

          }

}

function createDocument (numrects)

{

          // How many rectangles in horizontal?

          horzrange = Math.floor((width+distancex)/(rectwidth+distancex));

          // And vertical?

          vertrange = Math.floor((height+distancey)/(rectheight+distancey));

          if (horzrange < 1 || vertrange < 1)

                    return null;

 

          // How many artboards do we need?

          numartb = Math.ceil (numrects/(horzrange*vertrange));

//          alert ("We need "+numartb+" artboards");

          leftoffset = (width+distancex-horzrange*(rectwidth+distancex))/2;

          topoffset = (height+distancey-vertrange*(rectheight+distancey))/2;

          doc = app.documents.add (DocumentColorSpace.CMYK, width, height, numartb, DocumentArtboardLayout.GridByCol);

          return doc;

}

function drawRects (doc, array)

{

          black = new GrayColor();

          black.gray = 100;

          xpos = 0;

          ypos = 0;

          artb = 0;

//          alert (doc.artboards[artb].artboardRect);

          for (i = 0; i < array.length; i++)

          {

                    color = new CMYKColor();

                    try {

                              color.cyan = Number(array[0]);

                              color.magenta = Number(array[1]);

                              color.yellow = Number(array[2]);

                              color.black = Number(array[3]);

                    } catch (_)

                    {

                              alert ("Failed on line #"+i);

                              return;

                    }

                    rect = doc.pathItems.rectangle (doc.artboards[artb].artboardRect[1]-topoffset-ypos, doc.artboards[artb].artboardRect[0]+leftoffset+xpos, rectwidth, rectheight);

                    rect.strokeColor = black;

                    rect.strokeWidth = 0.5;

                    rect.fillColor = color;

 

                    t = doc.textFrames.add();

                    t.left = doc.artboards[artb].artboardRect[0]+leftoffset+xpos+rectwidth/2;

                    t.top = doc.artboards[artb].artboardRect[1]-topoffset-ypos-rectheight;

                    t.contents = array[0]+" / "+array[1]+" / "+array[2]+" / "+array[3];

                    t.paragraphs[0].justification = Justification.CENTER;

                    t.paragraphs[0].size = 8;

                    xpos += rectwidth + distancex;

                    if (xpos + rectwidth > width)

                    {

                              xpos = 0;

                              ypos += rectheight + distancey;

                              if (ypos+rectheight > height)

                              {

                                        ypos = 0;

                                        artb++;

                              }

                    }

          }

}

function mm2pt (val)

{

          return val*72/25.4;

}

function cm2pt (val)

{

          return val*72/2.54;

}

function in2pt (val)

{

          return val*72;

}

function isOSX()

{

  return $.os.match(/Macintosh/i);

}

function readInCSV(fileObj)

{

          var fileArray = new Array();

          fileObj.open('r');

          fileObj.seek(0, 0);

          while(!fileObj.eof)

          {

                    var thisLine = fileObj.readln();

                    if (thisLine.indexOf(',') > 0)

                    {

                              fileArray.push(thisLine.split(','));

                    }

          }

          fileObj.close();

          return fileArray;

}

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
January 15, 2012

Here you go, one Javascript to fit. It creates a new document in CMYK mode, with the required number of artboards. (Getting that right took more time than writing the entire drawing stuff, too!)

You can change the page and rectangle sizes near the top; adjust to fit your needs.

I do hope, though, you are using CS4 or older -- I can't test but I bet it will draw everything upside down for CS5!

//DESCRIPTION:Make Color Book From CSV

// A Jongware Script 15-Jan-2011

// Large parts for reading CSV shamelessly pilfered from

// Muppet Mark, http://forums.adobe.com/message/2877951#2877951

// The CSV is assumed to have this format:

// Cyan,Magenta,Yellow,Black

// separated by commas, color values in percents (so 100 = full)

// This sets up a landscape A4 document. Adjust for your own needs.

// My values are in mm, so they need converting to points for Illy.

// You can also enter sizes in inches -- use in2pt for that, as in:

// width = in2pt (8.5)

// height = in2pt (11);

// to set Letter size

// or use cm2pt(..) if you prefer centimeters

// ... no need to convert points, so if you like these

// simply enter

// width = 800;          // value is in points

width = mm2pt (297);

height = mm2pt (210);

// Size and distance per color rectangle

// The vertical distance needs to be enough for

// your by-line to fit.

rectwidth = mm2pt(20);

rectheight = mm2pt(15);

distancex = mm2pt (4);

distancey = mm2pt (8);

// These get recalculated to center the entire block

// of colors nicely on the page size.

leftoffset = 0;

topoffset = 0;

if (isOSX())

{

          var csvFile = File.openDialog('Select a CSV File', function (f) { return !f.hidden && ((f instanceof Folder) || f.name.match(/\.csv$/i));} );

} else

{

          var csvFile = File.openDialog('Select a CSV File','comma-separated-values(*.csv):*.csv;');

}

if (csvFile != null)

{

          fileArray = readInCSV(csvFile);

          var columns = fileArray[0].length;

//          alert('CSV file has ' + columns + ' columns…');

          var rows = fileArray.length;

//          alert('CSV file has ' + rows + ' rows…');

          if (columns == 4 && rows > 0)

          {

                    doc = createDocument (fileArray.length);

                    if (doc)

                              drawRects (doc, fileArray);

                    else

                              alert ("Rectangle and Page Sizes do not add up!");

          } else

          {

                    alert ("CSV is not in the expected format");

          }

}

function createDocument (numrects)

{

          // How many rectangles in horizontal?

          horzrange = Math.floor((width+distancex)/(rectwidth+distancex));

          // And vertical?

          vertrange = Math.floor((height+distancey)/(rectheight+distancey));

          if (horzrange < 1 || vertrange < 1)

                    return null;

 

          // How many artboards do we need?

          numartb = Math.ceil (numrects/(horzrange*vertrange));

//          alert ("We need "+numartb+" artboards");

          leftoffset = (width+distancex-horzrange*(rectwidth+distancex))/2;

          topoffset = (height+distancey-vertrange*(rectheight+distancey))/2;

          doc = app.documents.add (DocumentColorSpace.CMYK, width, height, numartb, DocumentArtboardLayout.GridByCol);

          return doc;

}

function drawRects (doc, array)

{

          black = new GrayColor();

          black.gray = 100;

          xpos = 0;

          ypos = 0;

          artb = 0;

//          alert (doc.artboards[artb].artboardRect);

          for (i = 0; i < array.length; i++)

          {

                    color = new CMYKColor();

                    try {

                              color.cyan = Number(array[0]);

                              color.magenta = Number(array[1]);

                              color.yellow = Number(array[2]);

                              color.black = Number(array[3]);

                    } catch (_)

                    {

                              alert ("Failed on line #"+i);

                              return;

                    }

                    rect = doc.pathItems.rectangle (doc.artboards[artb].artboardRect[1]-topoffset-ypos, doc.artboards[artb].artboardRect[0]+leftoffset+xpos, rectwidth, rectheight);

                    rect.strokeColor = black;

                    rect.strokeWidth = 0.5;

                    rect.fillColor = color;

 

                    t = doc.textFrames.add();

                    t.left = doc.artboards[artb].artboardRect[0]+leftoffset+xpos+rectwidth/2;

                    t.top = doc.artboards[artb].artboardRect[1]-topoffset-ypos-rectheight;

                    t.contents = array[0]+" / "+array[1]+" / "+array[2]+" / "+array[3];

                    t.paragraphs[0].justification = Justification.CENTER;

                    t.paragraphs[0].size = 8;

                    xpos += rectwidth + distancex;

                    if (xpos + rectwidth > width)

                    {

                              xpos = 0;

                              ypos += rectheight + distancey;

                              if (ypos+rectheight > height)

                              {

                                        ypos = 0;

                                        artb++;

                              }

                    }

          }

}

function mm2pt (val)

{

          return val*72/25.4;

}

function cm2pt (val)

{

          return val*72/2.54;

}

function in2pt (val)

{

          return val*72;

}

function isOSX()

{

  return $.os.match(/Macintosh/i);

}

function readInCSV(fileObj)

{

          var fileArray = new Array();

          fileObj.open('r');

          fileObj.seek(0, 0);

          while(!fileObj.eof)

          {

                    var thisLine = fileObj.readln();

                    if (thisLine.indexOf(',') > 0)

                    {

                              fileArray.push(thisLine.split(','));

                    }

          }

          fileObj.close();

          return fileArray;

}

Jongware
Community Expert
Community Expert
January 15, 2012

>... as well as a "tab-delimited" text document ...

Oh wait, I thought it was a CSV. For a tab-delimited file, make some changes in the readInCSV function at the very end of this script (it'll also make the function name wrong, but I can live with that).

Change the ',' twice to '\t', in the 9th and 11th line of that function, and it'll work with your file.

CarlosCanto
Community Expert
Community Expert
January 15, 2012

works fine in CS5