Skip to main content
Known Participant
January 11, 2023
Answered

make 100% black in document

  • January 11, 2023
  • 3 replies
  • 2573 views
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.

 

This topic has been closed for replies.
Correct answer m1b

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.

 

3 replies

m1b
Community Expert
Community Expert
January 11, 2023

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;

    };

})();

 

aviel222Author
Known Participant
January 12, 2023

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.

aviel222Author
Known Participant
January 12, 2023

And one more little thing:

4. If there is a 100% magenta or 100% cyan color. They will not turn black.

aviel222Author
Known Participant
January 11, 2023

as a script

Legend
January 11, 2023

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

aviel222Author
Known Participant
January 11, 2023
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.
Legend
January 11, 2023

Sorry - I did not notice the scripting tag.