Copy link to clipboard
Copied
Hello everyone
I have a document with objects (stroke and/or filled)
have different values
(for example: c=100 m=10 y=10)
And I want all the colors in the document to become 100 black.
Except the white will remain white.
Hi @aviel222 aren't you learning scripting? You should try to do these changes yourself and see how you go, You might surprise yourself with what you can achieve!
Anyway, here is a modified script. I hope you can learn from it.
/**
* Replaces colors of items in document.
* Currently configured to:
* (a) not change [C=100 M=0 Y=0 K=0] or [C=0 M=100 Y=0 K=0] (including in spot color)
* (b) change 10% black or lighter to white
* (c) change anyhting else to 100% black
* @discussion https:/
...
Copy link to clipboard
Copied
Select your art -
Edit > Edit Colors > Convert to Grayscale will get you most of the way there
Edit > Edit Colors > Saturate... will do the rest
Copy link to clipboard
Copied
Thanks, but I need it as a script, because I want to integrate it into other scripts.
In addition, it is important for me to have a window that displays before starting the operation if I want to execute it or cancel it.
Copy link to clipboard
Copied
Sorry - I did not notice the scripting tag.
Copy link to clipboard
Copied
as a script
Copy link to clipboard
Copied
Hi @aviel222, please try my script. I think it will do what you ask. First select what you want to adjust and run script. I haven't tested much, so if it fails, please post a test .ai doc (actually you need to save as pdf-with-illustrator-editing because of the forum rules) showing the error.
- Mark
/**
* Replaces all colors of selected items, except white, with black.
* @discussion https://community.adobe.com/t5/illustrator-discussions/make-100-black-in-document/m-p/13482548
* Note: you can customise the definition of "black" and "white" by editing
* the `makeBlack` and `notWhite` functions.
*/
(function () {
var doc = app.activeDocument,
items = doc.selection,
black = makeBlack(ColorType.RGB);
for (var i = 0; i < items.length; i++)
replaceColors(items[i], black, notWhite);
/**
* Replace `item`s basic colors with
* a supplied `replacementColor` but only
* if `filterFunction` returns true;
* @author m1b
* @version 2023-01-12
* @param {PageItem} item - the item to replace colors of.
* @param {Color} replacementColor - the replacement color.
* @param {Function} [filterFunction] - a function that filters the results.
*/
function replaceColors(item, replacementColor, filterFunction) {
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
&& (
filterFunction == undefined
|| filterFunction(colorables[i].fillColor)
)
)
colorables[i].fillColor = replacementColor;
if (
colorables[i].hasOwnProperty('strokeColor')
&& colorables[i].strokeColor != noColor
&& (
colorables[i].constructor.name == 'CharacterAttributes'
|| colorables[i].stroked == true
)
&& colorables[i].strokeColor != undefined
&& (
filterFunction == undefined
|| filterFunction(colorables[i].strokeColor)
)
)
colorables[i].strokeColor = replacementColor;
}
else if (item.constructor.name == 'GroupItem') {
for (var i = 0; i < item.pageItems.length; i++)
replaceColors(item.pageItems[i], replacementColor, filterFunction);
}
};
/**
* Returns true when color isn't white.
* @param {Color} c - the color to check.
* @returns {Boolean}
*/
function notWhite(c) {
var colorIsWhite = false;
switch (c.typename) {
case undefined:
case 'CMYKColor':
colorIsWhite = (
c.cyan == 0
&& c.magenta == 0
&& c.yellow == 0
&& c.black == 0
);
break;
case 'RGBColor':
colorIsWhite = (
c.red == 255
&& c.green == 255
&& c.blue == 255
);
break;
case 'GRAYColor':
colorIsWhite = (c.gray == 0);
break;
default:
break;
}
return !colorIsWhite;
};
/**
* Returns a black color of `colorType`.
* @param {ColorType} colorType - an Illustrator ColorType.
* @returns {Color}
*/
function makeBlack(colorType) {
var c;
switch (colorType) {
case undefined:
case ColorType.CMYK:
c = new CMYKColor();
c.cyan = 0;
c.magenta = 0;
c.yellow = 0;
c.black = 100;
break;
case ColorType.RGB:
c = new RGBColor();
c.red = 0;
c.green = 0;
c.blue = 0;
break;
case ColorType.GRAY:
c = new GrayColor();
c.gray = 100;
break;
default:
break;
}
return c;
};
})();
Copy link to clipboard
Copied
Hi thanks, it works, although I have a few comments, I would appreciate it if you could sort it out:
1. Select all objects automatically. (select all) without having to select them manually
2. If there is black gray (up to k=10). it will become white.
3. When the script starts, a window will appear - whether to run the script or not.
Because I combine it in another big script.
Copy link to clipboard
Copied
And one more little thing:
4. If there is a 100% magenta or 100% cyan color. They will not turn black.
Copy link to clipboard
Copied
Do you mean if the colour is C100 M0 Y0 K0 or C0 M100 Y0 K0 then do not turn black?
Copy link to clipboard
Copied
Yes. but it can be in spot color.
Copy link to clipboard
Copied
Hi @aviel222 aren't you learning scripting? You should try to do these changes yourself and see how you go, You might surprise yourself with what you can achieve!
Anyway, here is a modified script. I hope you can learn from it.
/**
* Replaces colors of items in document.
* Currently configured to:
* (a) not change [C=100 M=0 Y=0 K=0] or [C=0 M=100 Y=0 K=0] (including in spot color)
* (b) change 10% black or lighter to white
* (c) change anyhting else to 100% black
* @discussion https://community.adobe.com/t5/illustrator-discussions/make-100-black-in-document/m-p/13482548
* Note: you can customise which colors are excluded by editing the `changeColor` function or making your own replacer function.
*/
(function () {
if (!confirm('Do you want to replace colors?'))
return;
var doc = app.activeDocument,
items = doc.pageItems,
white = makeWhite(),
black = makeBlack();
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.
* @returns {Color} - the output color
*/
function changeColor(startColor) {
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 (
(
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-12
* @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);
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);
}
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() {
var c = new CMYKColor();
c.cyan = 0;
c.magenta = 0;
c.yellow = 0;
c.black = 100;
return c;
};
/**
* Returns a white color.
* @returns {CMYKColor}
*/
function makeWhite() {
var c = new CMYKColor();
c.cyan = 0;
c.magenta = 0;
c.yellow = 0;
c.black = 0;
return c;
};
})();
Edit: replacer now handles SpotColor.
Copy link to clipboard
Copied
Thanks!!! This is exactly what I need.
I would really like to learn to do it myself,
But I'm looking for a course where I live - and can't find it.
Maybe I'll look to learn through the internet.
Copy link to clipboard
Copied
Great! Yes you can absolutely learn through the internet. It's a very good resource. Bit by bit you will learn more and more.
Copy link to clipboard
Copied
Hi. It doesn't work to have magenta as a spot color.
could you please sort it out
Copy link to clipboard
Copied
please....
Copy link to clipboard
Copied
See edited script above. - Mark
Copy link to clipboard
Copied
Thanks