Skip to main content
Known Participant
December 14, 2018
Question

Illustrator Javascript - Render Swatch Legend

  • December 14, 2018
  • 2 replies
  • 3270 views

Render Swatch Legend Script

I need this script to start in a certain location on my document is there any way to do that?

/////////////////////////////////////////////////////////////////

// Render Swatch Legend v1.3 -- CC

//>=--------------------------------------

//

//  This script will generate a legend of rectangles for every swatch in the main swatches palette.

//  You can configure spacing and value display by configuring the variables at the top

//  of the script.

//   update: v1.1 now tests color brightness and renders a white label if the color is dark.

//   update: v1.2 uses adobe colour converter, rather than rgb colour conversion for a closer match.

//   update: v1.3 adds multiple colour space values based on array printColors.

//>=--------------------------------------

// JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com

// copyright full text here:  http://www.wundes.com/js4ai/copyright.txt

//

// Edits by Adam Green (@wrokred) www.wrokred.com

//

//////////////////////////////////////////////////////////////////

doc = activeDocument,

swatches = doc.swatches,

cols = 1, // number of columns in group

displayAs = "RGBColor",  //or "CMYKColor"

printColors = ["RGB", "CMYK", "LAB", "GrayScale"], // RGB, CMYK, LAB and/or GrayScale

colorSeparator = " ", // Character used to separate the colours eg "|" output = R: XXX|G: XXX|B: XXX

textSize = 40, // output text size value in points

rectRef=null,

textRectRef=null,

textRef=null,

swatchColor=null,

w=600;

h=300,

h_pad = 20,

v_pad = 20,

t_h_pad = 10,

t_v_pad = 10,

x=null,

y=null,

black = new GrayColor(),

white = new GrayColor()

;

black.gray = 100;

white.gray = 0;

activeDocument.layers[0].locked= false;

var newGroup = doc.groupItems.add();

newGroup.name = "NewGroup";

newGroup.move( doc, ElementPlacement.PLACEATBEGINNING );

for(var c=2,len=swatches.length;c<len;c++)

{

    var swatchGroup = doc.groupItems.add();

    swatchGroup.name = swatches.name;

    x= (w+h_pad)*((c-2)% cols);

    y=(h+v_pad)*(Math.round(((c)+.03)/cols))*-1 ;

    rectRef = doc.pathItems.rectangle(y,x, w,h);

    swatchColor = swatches.color;

    rectRef.fillColor = swatchColor;

    textRectRef =  doc.pathItems.rectangle(y- t_v_pad,x+ t_h_pad, w-(2*t_h_pad),h-(2*t_v_pad));

    textRef = doc.textFrames.areaText(textRectRef);

    textRef.contents = swatches.name + "\r" + getColorValues(swatchColor);

    textRef.textRange.fillColor = is_dark(swatchColor)? white : black;

    textRef.textRange.size = textSize;

    rectRef.move( swatchGroup, ElementPlacement.PLACEATBEGINNING );

    textRef.move( swatchGroup, ElementPlacement.PLACEATBEGINNING );

    swatchGroup.move( newGroup, ElementPlacement.PLACEATEND );

}

function getColorValues(c)

{

    if(c.typename)

    {

        if (c.typename == "SpotColor") {

        return getColorValues(c.spot.color);

        };

      

        switch(c.typename)

        {

            case "RGBColor": sourceSpace = ImageColorSpace.RGB; colorComponents=[c.red,c.green,c.blue]; break;

            case "CMYKColor": sourceSpace = ImageColorSpace.CMYK; colorComponents=[c.cyan,c.magenta,c.yellow,c.black]; break;

            case "LabColor": sourceSpace = ImageColorSpace.LAB; colorComponents=[c.l,c.a,c.b]; break;

            case "GrayColor": sourceSpace = ImageColorSpace.GrayScale; colorComponents=[c.gray]; break;

        }

        outputColors = new Array();

        for (var i = printColors.length - 1; i >= 0; i--) {

            targetSpace = ImageColorSpace[printColors];

            outputColors = app.convertSampleColor(sourceSpace, colorComponents, targetSpace,ColorConvertPurpose.previewpurpose);

            for (var j = outputColors.length - 1; j >= 0; j--) {

                outputColors = printColors.charAt(j)+": "+Math.round(outputColors);

                if (j == outputColors.length -1) {

                    outputColors += "\r";

                };

            };

            outputColors = outputColors.join(colorSeparator);

        };

        return outputColors.join("");

    }

    return "Non Standard Color Type";

}

function is_dark(c){

if(c.typename)

{

    switch(c.typename)

    {

        case "CMYKColor":

        return (c.black>50 || (c.cyan>50 &&  c.magenta>50)) ? true : false;

        case "RGBColor":

        return (c.red<100  && c.green<100 ) ? true : false;

        case "GrayColor":

        return c.gray > 50 ? true : false;

        case "SpotColor":

        return is_dark(c.spot.color);

        return false;

    }

}

}

[Question moved to the Illustrator Scripting forum. -Mod]

2 replies

renél80416020
Inspiring
December 16, 2018

Salut !

au minimum :

//-------------------

  cols = 5;

  x= 0;

  y= 0 ;

  nbh = 20

for(var c = 2, len = swatches.length ;c < len; c++) {

    var swatchGroup = doc.groupItems.add();

    swatchGroup.name = swatches.name;

      if (c != 2){

        x= (w+h_pad)*((c-2)% cols)

          if((c-2)%cols == 0) {

            y-=(h+v_pad);

          }

      }

    rectRef = doc.pathItems.rectangle(y,x, w,h);

//-------------------

de LR

Disposition_Dev
Legend
December 14, 2018

change the "h_pad" and "v_pad" variables. those represent the starting position.

Known Participant
December 14, 2018

That only changes the Horizontal and Vertical Padding of the boxes it generates. The starting position of the boxes is not changed.

Disposition_Dev
Legend
December 14, 2018

woops. sorry about that. i didn't look too closely.

you can create two extra variables for starting position like this:

var start_x = 0;

var  start_y = (h*2);

then in the for loop, add those variables to the equation when you determine the x and y variables, like so:

x= start_x + (w+h_pad)*((c-2)% cols);

y= start_y + (h+v_pad)*(Math.round(((c)+.03)/cols))*-1 ;