change color swatch to other color swatch
Hello friends
I have a document with magenta color (magenta=100) and I am looking for a script that will turn it into a spot color whose name will be "cutcontour" (magenta=100 too)
Hello friends
I have a document with magenta color (magenta=100) and I am looking for a script that will turn it into a spot color whose name will be "cutcontour" (magenta=100 too)
charu? m1b?
Hi @aviel222, here is a modified version of the script I wrote for you on this thread. I think it will do what you want. Try to understand what is happening in the changeColor function. You can configure what you want to do there to some extent. Note that currently, this script will create the "CutContour" spot colour if it isn't in document already (see the call to my makeSpot function).
- Mark
/**
* Replaces colors of items in document.
* Currently configured via the 'changeColor'
* replacer function to:
* (a) change stroke [C=0, M=100, Y=0, K=0] to 'CutContour' spot.
* (b) not change [C=100 M=0 Y=0 K=0] or [C=0 M=100 Y=0 K=0] (even in spot color)
* (c) change 10% black or lighter to white
* (d) change anyhting else to 100% black
* @discussion https://community.adobe.com/t5/illustrator-discussions/make-100-black-in-document/m-p/13482548
* @discussion https://community.adobe.com/t5/illustrator-discussions/change-color-swatch-to-other-color-swatch/m-p/13491958
* Note: you can customise which colors are excluded by editing the `changeColor` function or writing your own replacer.
*/
(function () {
if (!confirm('Do you want to replace colors?'))
return;
var doc = app.activeDocument,
items = doc.pageItems,
white = makeWhite(),
black = makeBlack(),
cutContourSpot = makeSpot(doc, 'CutContour', [0, 100, 0, 0]);
for (var i = 0; i < items.length; i++)
replaceColors(items[i], changeColor);
/**
* Returns a color based on the given color.
* A `replacer` function for replaceColors.
* @param {Color} c - the input color.
* @param {Boolean} isFillColor - whether the input color is a fillColor.
* @returns {Color} - the output color
*/
function changeColor(startColor, isFillColor) {
var c = startColor;
if (c.typename == 'SpotColor')
c = c.spot.color;
if (
(
c.typename == 'GrayColor'
&& c.gray <= 10
)
|| (
c.typename == 'CMYKColor'
&& c.cyan == 0
&& c.magenta == 0
&& c.yellow == 0
&& c.black <= 10
)
)
return white;
else if (
isFillColor !== true
&& c.typename == 'CMYKColor'
&& c.cyan == 0
&& c.magenta == 100
&& c.yellow == 0
&& c.black == 0
)
return cutContourSpot;
else if (
(
c.typename == 'CMYKColor'
&& c.cyan == 100
&& c.magenta == 0
&& c.yellow == 0
&& c.black == 0
)
|| (
c.typename == 'CMYKColor'
&& c.cyan == 0
&& c.magenta == 100
&& c.yellow == 0
&& c.black == 0
)
)
return c;
else
return black;
};
/**
* Replace `item`s basic colors with
* a supplied `replacementColor` but only
* if `filterFunction` returns true;
* @author m1b
* @version 2023-01-17
* @param {PageItem} item - the item to replace colors of.
* @param {Function} replacer - function that returns a replacement color.
*/
function replaceColors(item, replacer) {
if (item == undefined)
throw Error('replaceColors: No item supplied.');
var noColor = "[NoColor]",
colorables = [];
// 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
)
colorables[i].fillColor = replacer(colorables[i].fillColor, true);
if (
colorables[i].hasOwnProperty('strokeColor')
&& colorables[i].strokeColor != noColor
&& (
colorables[i].constructor.name == 'CharacterAttributes'
|| colorables[i].stroked == true
)
&& colorables[i].strokeColor != undefined
)
colorables[i].strokeColor = replacer(colorables[i].strokeColor, false);
}
else if (item.constructor.name == 'GroupItem') {
for (var i = 0; i < item.pageItems.length; i++)
replaceColors(item.pageItems[i], replacer);
}
};
/**
* Returns a black color.
* @returns {CMYKColor}
*/
function makeBlack() {
return makeColor([0, 0, 0, 100]);
};
/**
* Returns a white color.
* @returns {CMYKColor}
*/
function makeWhite() {
return makeColor([0, 0, 0, 0]);
};
function makeColor(breakdown) {
var c;
if (breakdown.length == 1) {
c = new GrayColor();
c.gray = breakdown[0];
}
else if (breakdown.length == 3) {
c = new RGBColor();
c.red = breakdown[0];
c.green = breakdown[1];
c.blue = breakdown[2];
}
else if (breakdown.length == 4) {
c = new CMYKColor();
c.cyan = breakdown[0];
c.magenta = breakdown[1];
c.yellow = breakdown[2];
c.black = breakdown[3];
}
return c;
};
function makeSpot(doc, name, breakdown, colorModel) {
// check if exists in document
var newColor = getThing(doc.swatches, 'name', name);
if (newColor != undefined)
return newColor.color;
// or create it
var newSpot = doc.spots.add();
newSpot.name = name;
newSpot.colorType = colorModel || ColorModel.SPOT;
var col = makeColor(breakdown);
if (col == undefined)
throw Error('makeSpot: could not make color (breakdown: ' + breakdown + ').');
newSpot.color = col;
newColor = new SpotColor();
newColor.spot = newSpot;
return newColor;
};
function getThing(things, key, value) {
for (var i = 0; i < things.length; i++)
if (things[i][key] == value)
return things[i];
};
})();
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.