Copy link to clipboard
Copied
I was wondering if someone could help me edit this Script Written by John Wundes. Its almost perfect for what i need, but my problem is the text box inside of the swatch when it is rendered is to small. Ideally i'd like it to be 90% the size of the actual swatch. I've copied and pasted it here if someone could help i'd greatly appreciate it. I tried asking John on his Blog a few times but i haven't gotten any response. Also i'd like to delete the CMYK values that it returns with just the PMS name only.
Render Swatch Legend v1.1 -- CS, CS2, CS3, CS4, CS5
//>=--------------------------------------
//
// 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.
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com
// copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//
//////////////////////////////////////////////////////////////////
doc = activeDocument,
swatches = doc.swatches,
cols = 10,
displayAs = "CMYKColor", //or "RGBColor"
rectRef=null,
textRectRef=null,
textRef=null,
rgbColor=null,
w=150;
h=150,
h_pad = 100,
v_pad = 100,
t_h_pad = 100,
t_v_pad = 100,
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
x= (w+h_pad)*(c% cols);
y=(h+v_pad)*(Math.floor((c+.01)/cols))*-1 ;
rectRef = doc.pathItems.rectangle(y,x, w,h);
rgbColor = swatches
rectRef.fillColor = rgbColor;
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
textRef.textRange.fillColor = is_dark(swatches
//
rectRef.move( swatchGroup, ElementPlacement.PLACEATBEGINNING );
textRef.move( swatchGroup, ElementPlacement.PLACEATBEGINNING );
swatchGroup.move( newGroup, ElementPlacement.PLACEATEND );
}
function getColorValues(color)
{
if(color.typename)
{
switch(color.typename)
{
case "CMYKColor":
if(displayAs == "CMYKColor"){
return ([Math.floor(color.cyan),Math.floor(color.magenta),Math.floor(color.yellow),Math.floor(color.black)]);}
else
{
color.typename="RGBColor";
return [Math.floor(color.red),Math.floor(color.green),Math.floor(color.blue)] ;
}
case "RGBColor":
if(displayAs == "CMYKColor"){
return rgb2cmyk(Math.floor(color.red),Math.floor(color.green),Math.floor(color.blue));
}else
{
return [Math.floor(color.red),Math.floor(color.green),Math.floor(color.blue)] ;
}
case "GrayColor":
if(displayAs == "CMYKColor"){
return rgb2cmyk(Math.floor(color.gray),Math.floor(color.gray),Math.floor(color.gray));
}else{
return [Math.floor(color.gray),Math.floor(color.gray),Math.floor(color.gray)];
}
case "SpotColor":
return getColorValues(color.spot.color);
}
}
return "Non Standard Color Type";
}
function rgb2cmyk (r,g,b) {
var computedC = 0;
var computedM = 0;
var computedY = 0;
var computedK = 0;
//remove spaces from input RGB values, convert to int
var r = parseInt( (''+r).replace(/\s/g,''),10 );
var g = parseInt( (''+g).replace(/\s/g,''),10 );
var b = parseInt( (''+b).replace(/\s/g,''),10 );
if ( r==null || g==null || b==null ||
isNaN(r) || isNaN(g)|| isNaN(b) )
{
alert ('Please enter numeric RGB values!');
return;
}
if (r<0 || g<0 || b<0 || r>255 || g>255 || b>255) {
alert ('RGB values must be in the range 0 to 255.');
return;
}
// BLACK
if (r==0 && g==0 && b==0) {
computedK = 1;
return [0,0,0,1];
}
computedC = 1 - (r/255);
computedM = 1 - (g/255);
computedY = 1 - (b/255);
var minCMY = Math.min(computedC,
Math.min(computedM,computedY));
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
computedK = minCMY;
return [Math.floor(computedC*100),Math.floor(computedM*100),Math.floor(computedY*100),Math.floor(computedK*100)];
}
function is_dark(color){
if(color.typename)
{
switch(color.typename)
{
case "CMYKColor":
return (color.black>50 || (color.cyan>50 && color.magenta>50)) ? true : false;
case "RGBColor":
return (color.red<100 && color.green<100 ) ? true : false;
case "GrayColor":
return color.gray > 50 ? true : false;
case "SpotColor":
return is_dark(color.spot.color);
return false;
}
}
}
Natesroom1 wrote:
1. text is to small
2. delete the CMYK values that it returns with just the PMS name only
Just a quick glance at the code, try the following:
1.) For the Size, after this line:
textRef = doc.textFrames.areaText(textRectRef);
Add the following line, change the numeric value to whatever size suits you:
textRef.textRange.characterAttributes.size = 20;
2.) Regarding removing the CMYK values for for PMS colors, change this line:
...textRef.contents = swatches
.name+ "\r" + getColorVal
Copy link to clipboard
Copied
You can't remove those, unless you want Illustrator to crash immediately. They are apparently required.
Copy link to clipboard
Copied
i just want to remove them from showing up when the swatch palette is created using RenderSwatchLegend. I don't want to delete them from the swatch panel...
Copy link to clipboard
Copied
Would just be a case of a name check
there is a line:
var swatchRefName=docRef.swatches
under that add something like this and wrap the rest of the code that is executed in an if statement:
if(swatchRefName !== "Registration Black" && swatchRefName !== "None") {
//The rest of the code to follow
}
Or if you want something you can easily amend in the future for new colours you want to ignore:
if(shouldDisplay(swatchRefName)) {
//The rest of the code to follow
}
function shouldDisplay(name) {
var ignore = [
"Registration Black",
"None"
]
for (var i = 0; i < ignore.length; i++) {
if (name == ignore) {
return false;
}
}
return true;
}
you'd just keep adding to the ignore array
Copy link to clipboard
Copied
Thanks to all who have contributed to this thread! I have found it very helpful! My issue is that I am also trying to prevent the [None] and [Registration] swatches from displaying when the swatch palette is generated using RenderSwatchLegend, but I can't figure out where exactly in the script to add the commands that itsBenMason suggested above. Can someone assist by explaining exactly where and what the commands are to eliminate the [None] and [Registration] swatches? Below is a screenshot of a section of the swatches menu and a section of the palette that is generated from the script. You can see the 2 swatches I need eliminated. Thanks for any assistance!
Copy link to clipboard
Copied
Hi, I have two versions of this scripts. The following version skips [None] and [Registration]. You can try following script. May be this will help you.
/////////////////////////////////////////////////////////////////
// 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 = 4, // 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 = 10, // output text size value in points
rectRef = null,
textRectRef = null,
textRef = null,
swatchColor = null,
w = 150;
h = 120,
h_pad = 10,
v_pad = 10,
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[c].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[c].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[c].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[i]];
outputColors[i] = app.convertSampleColor(sourceSpace, colorComponents, targetSpace, ColorConvertPurpose.previewpurpose);
for (var j = outputColors[i].length - 1; j >= 0; j--) {
outputColors[i][j] = printColors[i].charAt(j) + ": " + Math.round(outputColors[i][j]);
if (j == outputColors[i].length - 1) {
outputColors[i][j] += "\r";
};
};
outputColors[i] = outputColors[i].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;
}
}
}
Thanks
Copy link to clipboard
Copied
Thanks, charu16
That helped! Any idea why, after the color Redwood Red in the legend, a new row starts instead of continuing along? I have 13 columns defined in the script.
Here's what I have now:
// 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 = 13, // number of columns in group
displayAs = "RGBColor", //or "CMYKColor"
printColors = ["RGB"], // RGB, CMYK, LAB and/or GrayScale
colorSeparator = " ", // Character used to separate the colours eg "|" output = R: XXX|G: XXX|B: XXX
textSize = 8, // output text size value in points
rectRef = null,
textRectRef = null,
textRef = null,
swatchColor = null,
w = 75;
h = 75,
h_pad = 15,
v_pad = 50,
t_h_pad = 0,
t_v_pad = 55,
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[c].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[c].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[c].name + "\r" + getColorValues(swatchColor);
textRef.textRange.fillColor = black;
textRef.position = [rectRef.left + 2, (rectRef.top - rectRef.height - 6)];
textRef.textRange.characterAttributes.textFont = textFonts.getByName("HelveticaNeue");
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[i]];
outputColors[i] = app.convertSampleColor(sourceSpace, colorComponents, targetSpace, ColorConvertPurpose.previewpurpose);
for (var j = outputColors[i].length - 1; j >= 0; j--) {
outputColors[i][j] = printColors[i].charAt(j) + ": " + Math.round(outputColors[i][j]);
if (j == outputColors[i].length - 1) {
outputColors[i][j] += "\r";
};
};
outputColors[i] = outputColors[i].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;
}
}
}
Thank you very much!!
Copy link to clipboard
Copied
Hi, I am not sure why only 5 are coming in first row, because this script was wriiten by someone else. But I ahve updated this version now it will have as expected columns. So, it is difficult to explain the reason behind the existing one as it was written by other writer. Please try below version
/////////////////////////////////////////////////////////////////
// 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 = 13, // 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 = 10, // output text size value in points
rectRef = null,
textRectRef = null,
textRef = null,
swatchColor = null,
w = 150;
h = 120,
h_pad = 10,
v_pad = 10,
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);
y = 0;
var count = 0;
for (var c = 2, len = swatches.length; c < len; c++) {
var swatchGroup = doc.groupItems.add();
swatchGroup.name = swatches[c].name;
x = (w + h_pad) * ((c - 2) % cols);
if (cols == count) {
y = (h + v_pad) * (Math.floor(((c)) / (cols))) * -1;
count = 0;
}
rectRef = doc.pathItems.rectangle(y, x, w, h);
swatchColor = swatches[c].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[c].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);
count++
}
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[i]];
outputColors[i] = app.convertSampleColor(sourceSpace, colorComponents, targetSpace, ColorConvertPurpose.previewpurpose);
for (var j = outputColors[i].length - 1; j >= 0; j--) {
outputColors[i][j] = printColors[i].charAt(j) + ": " + Math.round(outputColors[i][j]);
if (j == outputColors[i].length - 1) {
outputColors[i][j] += "\r";
};
};
outputColors[i] = outputColors[i].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;
}
}
}
Let us know if this helps
Copy link to clipboard
Copied
Awesome! Thank you charu16 That worked! Thanks you so much!!!
Copy link to clipboard
Copied
Hello. I was wondering if there is a way I can make this script render just the names of the PMS colors in the actual color of the PMS and not in black ?
I don’t need the box with color in it. Just the names in their respective colors.
Also. Is there a script that will render the name of the file that illustrator puts in outside the marks in the size I want it. Bigger. And bolder.
Copy link to clipboard
Copied
As I do to eliminate the 2 blank spaces where it should not appear (none and registration) example this is what appears.
And I want it to look more like this.
Can someone make this possible please?
Copy link to clipboard
Copied
check BenMason's answer to that in post#26
Copy link to clipboard
Copied
It does not work for me or I'm not placing it in the right place.
Can you tell me if it worked or you tried it?
Copy link to clipboard
Copied
Can someone help me change this to display the RGB values instead of the CMYK values?
Disregard! I figured it out 🙂
Copy link to clipboard
Copied
Hi,
So you would like to have as per below screenshot?
If yes, then easy way is Open the document in RGB mode, the script will automatically display the RGB values.
Thanks
Copy link to clipboard
Copied
for some reason when i run his script, the hex value is always wrong...