Copy link to clipboard
Copied
Hi community, hope you're doing awesome. Can anyone help me to modify this code?
Credits to @femkeblanco
function WriteColours(myVariable){
var font = "Arial-BoldItalicMT";
var size = 20.02;//size font
if((myVariable == "Truck") || (myVariable == "Mini Van") || (myVariable == "Bus")){
var w = size * 2.3, h = size, x = 440, y = -73; // width, height, left and top
}
if((myVariable == "Civic") || (myVariable == "Corolla")){
var w = size * 2.3, h = size, x = 64, y = -628; // width, height, left and top
}
var doc = app.activeDocument;
var group = doc.groupItems.add();
for (var i = 2; i < doc.swatches.length; i++) {
var rect = doc.pathItems.rectangle(y, x, w, h);
var text = doc.textFrames.areaText(rect);
text.textRange.textFont = textFonts[font];
text.contents = doc.swatches[i].name + ",";
text.textRange.size = size;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += size;
}
x += text.textPath.width;
text.move(group, ElementPlacement.PLACEATEND);
}
}
I pass trough different options different parameter to write/draw the text boxes depending of what it comes to the function, position x and y changes depending on the option, but right now it generates the colors from the swatches in the document not form the selection, can anybody help me modifying this to get colors only from what i have selected? Thanks!
1 Correct answer
Okay I see, like this?
(function () {
// example usage:
WriteColours4('Truck');
// initial code courtesy @femkeblanco
// modified by @AntonioPacheco
// modified by @m1b
function WriteColours4(myVariable) {
var font = "Arial-BoldItalicMT";
var size = 20.02; //size font
if (myVariable == "Truck"
|| myVariable == "Mini Van"
|| myVariable == "Bus"
) {
var w = size * 2.3,
h = size,
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi,
Try following link if that can help.
https://productivista.com/make-a-list-of-colors-from-your-selection/
Copy link to clipboard
Copied
thank you, kind of struggling already hehe if someone can help me with the code I provided I will appreciate it
Copy link to clipboard
Copied
text.fillColor = app.activeDocument.selection[0].fillColor; //or text.strokeColor for strokes respectively
Copy link to clipboard
Copied
Thank you for your help! I still don't try this but it seems that it will color the text frame, what I would like to do is to set in text.contents the colors from the selection instead of getting the colors from the doc.swatches.
Copy link to clipboard
Copied
If you have the spot color in the selection[0].fillColor, then you get exactly spot. But if you want to get exactly just RGB color then you can get it by
selection[0].fillColor.spot.color
Or maybe it will need to take for each channel (dont sure, cant test it now):
selection[0].fillColor.spot.color.blue //( , red, green)
If you work in ESTK then you can to see this properties in the Data Browser window (and if not, I really advise you to try it. When I discovered it, it opened up new horizons for me)
I usually create a variable to which I assign the necessary item and see what happens inside it
Copy link to clipboard
Copied
let me try it out!
Copy link to clipboard
Copied
unfortunately not working, all I get is "undefined" hehe
Copy link to clipboard
Copied
I found a way to show in alert the colors from a selection:
with (app.activeDocument.selection[0]) {
if (pathItems.length > 0)
{
alert(pathItems.length);
for (var g = 0 ; g < pathItems.length; g++)
{
if (pathItems[g].filled == true)
{
alert(pathItems[g].fillColor.spot.name);
}
}
}
} with (app.activeDocument.selection[0]) {
if (pathItems.length > 0)
{
alert(pathItems.length);
for (var g = 0 ; g < pathItems.length; g++)
{
if (pathItems[g].filled == true)
{
alert(pathItems[g].fillColor.spot.name);
}
}
}
}
Now I need to adapt it to the code, maybe somebody may find it useful
Copy link to clipboard
Copied
function WriteColours(myVariable) {
var font = "Arial-BoldItalicMT";
var size = 20.02; //size font
if ((myVariable == "Truck") || (myVariable == "Mini Van") || (myVariable == "Bus")) {
var w = size * 2.3, h = size, x = 440, y = -73; // width, height, left and top
}
if ((myVariable == "Civic") || (myVariable == "Corolla")) {
var w = size * 2.3, h = size, x = 64, y = -628; // width, height, left and top
}
var doc = app.activeDocument;
if (doc.selection.length > 0) {
var group = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
var rect = doc.pathItems.rectangle(y, x, w, h);
var text = doc.textFrames.areaText(rect);
text.textRange.textFont = textFonts[font];
text.contents = doc.selection[i].fillColor.spot.name;
text.textRange.size = size;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += size;
}
x += text.textPath.width;
text.move(group, ElementPlacement.PLACEATEND);
}
}
}
WriteColours("Truck");
Copy link to clipboard
Copied
let me try it out later !
Copy link to clipboard
Copied
I am about to try it right now
Copy link to clipboard
Copied
Unfortunately nothing happened 😞
Copy link to clipboard
Copied
Update, I change the line from:
doc.selection[i].fillColor.spot.name;
To
pathItems[g].fillColor.spot.name;
And I added the cycle from above answers, it work but it only writes one color, like it is not entering the cycle or is finishing the cycle, not sure which variable to change or modify. Hope this helps too.
function WriteColours2(myVariable) {
var font = "Arial-BoldItalicMT";
var size = 20.02; //size font
if((myVariable == "Truck") || (myVariable == "Mini Van") || (myVariable == "Bus")){
var w = size * 2.3, h = size, x = 440, y = -73; // width, height, left and top x = 385, y = -203
}
if((myVariable == "Civic") || (myVariable == "Corolla")){
var w = size * 2.3, h = size, x = 64, y = -628; // width, height, left and top x = 385, y = -203
}
var doc = app.activeDocument;
var miColor = "";
if (doc.selection.length > 0) {
var group = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
var rect = doc.pathItems.rectangle(y, x, w, h);
var text = doc.textFrames.areaText(rect);
text.textRange.textFont = textFonts[font];
with (app.activeDocument.selection[0]) {
if (pathItems.length > 0)
{
for (var g = 0 ; g < pathItems.length; g++)
{
if (pathItems[g].filled == true)
{
miColor = pathItems[g].fillColor.spot.name;//Previously was doc.selection[i].fillColor.spot.name;
text.contents = miColor;
text.textRange.size = size;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += size;
}
x += text.textPath.width;
}
}
}
}
text.move(group, ElementPlacement.PLACEATEND);
}
}
}
Copy link to clipboard
Copied
Hi @AntonioPacheco, I had a look at this for you. It was hard to work out what you were doing, and I changed quite a few things so that it made sense to me. For example, now I gather all the colors of the selection first, then I make the color chips. I have made a function that helps to gather the colors from a pageItem so I used that.
Did I understand your needs correctly?
- Mark
(function () {
// example usage:
WriteColours3('Truck');
// initial code courtesy @femkeblanco
// modified by @Antonio Pacheco HN
// modified by @m1b
function WriteColours3(myVariable) {
var font = "Arial-BoldItalicMT";
var size = 20.02; //size font
if (myVariable == "Truck"
|| myVariable == "Mini Van"
|| myVariable == "Bus"
) {
var w = size * 2.3,
h = size,
x = 440,
y = -73; // width, height, left and top x = 385, y = -203
}
if (
myVariable == "Civic"
|| myVariable == "Corolla"
) {
var w = size * 2.3,
h = size,
x = 64,
y = -628; // width, height, left and top x = 385, y = -203
}
var doc = app.activeDocument;
var miColor = "";
if (doc.selection.length == 0) {
alert('Please make a selection and try again.');
return;
}
// get colors from the selection
var selectedSpotColors = [];
for (var i = 0; i < doc.selection.length; i++) {
var item = app.activeDocument.selection[i];
var colorsOfSelectedItem = getBasicColorsFromItem(item);
for (var c = 0; c < colorsOfSelectedItem.fillColors.length; c++) {
var miColor = colorsOfSelectedItem.fillColors[c];
if (miColor.typename == 'SpotColor')
selectedSpotColors.push(miColor);
}
}
// now make color chips for each spot color found
var group = doc.groupItems.add();
for (var c = 0; c < selectedSpotColors.length; c++) {
var miColor = selectedSpotColors[c];
// draw the text
var text = doc.textFrames.pointText([x, y]);
text.textRange.textFont = textFonts[font];
text.contents = miColor.spot.name;
text.textRange.size = size;
// draw rectangle around text
var rect = doc.pathItems.rectangle(y + text.height, x, text.width, text.height);
rect.fillColor = miColor;
// advance x
x += text.width;
// group
text.move(group, ElementPlacement.PLACEATEND);
rect.move(group, ElementPlacement.PLACEATEND);
}
};
/**
* Returns array of swatches or colors
* found in fill or stroke of page item.
* @author m1b
* @version 2022-10-11
* @param {PageItem} item - an Illustrator page item.
* @returns {Object} - {fillColors: Array<Color>, strokeColors: Array<Color>}
*/
function getBasicColorsFromItem(item) {
if (item == undefined)
throw Error('getItemColor: No item supplied.');
var noColor = "[NoColor]",
colorables = [],
foundColors = {
fillColors: [],
strokeColors: []
};
// collect all the colorables
if (item.constructor.name == 'PathItem') {
colorables.push(item);
}
else if (
item.constructor.name == 'CompoundPathItem'
&& item.pathItems
) {
colorables.push(item.pathItems[0]);
}
else if (
item.constructor.name == 'TextFrame'
&& item.textRanges
) {
for (var i = item.textRanges.length - 1; i >= 0; i--)
colorables.push({
fillColor: item.textRanges[i].characterAttributes.fillColor,
strokeColor: item.textRanges[i].characterAttributes.strokeColor
});
}
if (colorables.length > 0)
for (var i = 0; i < colorables.length; i++) {
if (
colorables[i].hasOwnProperty('fillColor')
&& colorables[i].fillColor != noColor
&& (
!colorables[i].hasOwnProperty('filled')
|| colorables[i].filled == true
)
&& colorables[i].fillColor != undefined
)
foundColors.fillColors.push(colorables[i].fillColor);
if (
colorables[i].hasOwnProperty('strokeColor')
&& colorables[i].strokeColor != noColor
&& (
colorables[i].constructor.name == 'CharacterAttributes'
|| colorables[i].stroked == true
)
&& colorables[i].strokeColor != undefined
)
foundColors.strokeColors.push(colorables[i].strokeColor);
}
else if (item.constructor.name == 'GroupItem') {
// add colors from grouped items
for (var i = 0; i < item.pageItems.length; i++) {
var found = getBasicColorsFromItem(item.pageItems[i]);
foundColors.fillColors = foundColors.fillColors.concat(found.fillColors);
foundColors.strokeColors = foundColors.strokeColors.concat(found.strokeColors);
}
}
return foundColors;
};
})();
Copy link to clipboard
Copied
Ohh my...!! It's like my basic functions went to the gym and came back with more musles! Let me try it out.
Copy link to clipboard
Copied
@m1bfor this time I don't need the chips, I just need the code to write in a single text frame (or various) the colors (fill colors) in a selected object, like:
Red 186, Yellow 112, Black C.
Copy link to clipboard
Copied
Okay I see, like this?
(function () {
// example usage:
WriteColours4('Truck');
// initial code courtesy @femkeblanco
// modified by @AntonioPacheco
// modified by @m1b
function WriteColours4(myVariable) {
var font = "Arial-BoldItalicMT";
var size = 20.02; //size font
if (myVariable == "Truck"
|| myVariable == "Mini Van"
|| myVariable == "Bus"
) {
var w = size * 2.3,
h = size,
x = 440,
y = -73; // width, height, left and top x = 385, y = -203
}
if (
myVariable == "Civic"
|| myVariable == "Corolla"
) {
var w = size * 2.3,
h = size,
x = 64,
y = -628; // width, height, left and top x = 385, y = -203
}
var doc = app.activeDocument;
var miColor = "";
if (doc.selection.length == 0) {
alert('Please make a selection and try again.');
return;
}
// get colors from the selection
var selectedSpotColors = [];
for (var i = 0; i < doc.selection.length; i++) {
var item = app.activeDocument.selection[i];
var colorsOfSelectedItem = getBasicColorsFromItem(item);
for (var c = 0; c < colorsOfSelectedItem.fillColors.length; c++) {
var miColor = colorsOfSelectedItem.fillColors[c];
if (miColor.typename == 'SpotColor')
selectedSpotColors.push(miColor);
}
}
// now list the colors in a text frame
var group = doc.groupItems.add(),
colorNames = [],
unique = {};
for (var c = 0; c < selectedSpotColors.length; c++) {
var colorName = selectedSpotColors[c].spot.name;
// only add if unique
if (unique[colorName] == undefined) {
unique[colorName] = true;
colorNames.push(colorName);
}
}
// draw the text
var text = doc.textFrames.pointText([x, y]);
text.textRange.textFont = textFonts[font];
text.contents = colorNames.join(', ');
text.textRange.size = size;
};
/**
* Returns array of swatches or colors
* found in fill or stroke of page item.
* @author m1b
* @version 2022-10-11
* @Param {PageItem} item - an Illustrator page item.
* @Returns {Object} - {fillColors: Array<Color>, strokeColors: Array<Color>}
*/
function getBasicColorsFromItem(item) {
if (item == undefined)
throw Error('getItemColor: No item supplied.');
var noColor = "[NoColor]",
colorables = [],
foundColors = {
fillColors: [],
strokeColors: []
};
// collect all the colorables
if (item.constructor.name == 'PathItem') {
colorables.push(item);
}
else if (
item.constructor.name == 'CompoundPathItem'
&& item.pathItems
) {
colorables.push(item.pathItems[0]);
}
else if (
item.constructor.name == 'TextFrame'
&& item.textRanges
) {
for (var i = item.textRanges.length - 1; i >= 0; i--)
colorables.push({
fillColor: item.textRanges[i].characterAttributes.fillColor,
strokeColor: item.textRanges[i].characterAttributes.strokeColor
});
}
if (colorables.length > 0)
for (var i = 0; i < colorables.length; i++) {
if (
colorables[i].hasOwnProperty('fillColor')
&& colorables[i].fillColor != noColor
&& (
!colorables[i].hasOwnProperty('filled')
|| colorables[i].filled == true
)
&& colorables[i].fillColor != undefined
)
foundColors.fillColors.push(colorables[i].fillColor);
if (
colorables[i].hasOwnProperty('strokeColor')
&& colorables[i].strokeColor != noColor
&& (
colorables[i].constructor.name == 'CharacterAttributes'
|| colorables[i].stroked == true
)
&& colorables[i].strokeColor != undefined
)
foundColors.strokeColors.push(colorables[i].strokeColor);
}
else if (item.constructor.name == 'GroupItem') {
// add colors from grouped items
for (var i = 0; i < item.pageItems.length; i++) {
var found = getBasicColorsFromItem(item.pageItems[i]);
foundColors.fillColors = foundColors.fillColors.concat(found.fillColors);
foundColors.strokeColors = foundColors.strokeColors.concat(found.strokeColors);
}
}
return foundColors;
};
})();
Copy link to clipboard
Copied
It worked! It took me a couple of minutes to find out how to work with it but now all okay !
Copy link to clipboard
Copied
Excellent!
Copy link to clipboard
Copied
Are you okay with it if i use your getBasicColorsFromItem function, edit it so i can use it my function ive put together for converting selections to PMS colors? I had a already a working version, but this would do all pathItems in the open document. This would cause illustrator to freeze/crash when i ran the code. I needed a function which loop over all pathItems of a selection. I tested your function with some adjustments made and it works perfectly together with my prepared script.
Im doing some more work on the Logo Packer CEP panel and i am adding a functionality where it converts CMYK or RGB logos into a PMS color variation. Doing this process can normally be done with the ReColor function, but we can not use that by using scripts. Ive made a mashup of different scripts ive found and with lots of edits to them, it now works as intended.
The panel in question is this one, https://github.com/schroef/logo-packer
It was originally made by mevCJ, i've completly overhauled and redesigned the initial panel and have added many more functionalities to it.
Here's a quick preview of what it does and how the panel looks. its basically a panel which automates the logo variation creation and the export of all the files. Normally, when you do this manually, its very tedious work. It can take easily up to an hour or more. You also need to be very consistent with file naming etc etc
Ill also add a preview of the complete process
Copy link to clipboard
Copied
Hi @schroef, sure go for it! Your plug-in looks cool. 🙂
- Mark
Copy link to clipboard
Copied
Great thanks @m1b

