Skip to main content
Participating Frequently
Sep 27, 2023 6:06 AM UTC
Question

Grayscale Color conver to CMYK Color

It has a problem converting Grayscale color to CMYK.
There is no problem in the codes I wrote until
I select the objects. But I couldn't convert the
selected color to CMYK color.

Where am I wrong?

 

var refDoc = app.activeDocument;
function myCmykColor(c, m, y, k) {
    var ink = new CMYKColor();
    ink.cyan = c;
    ink.magenta = m;
    ink.yellow = y;
    ink.black = k;
    return ink;
}
var textColor = new GrayColor();
textColor.gray = 3;
refDoc.defaultFillColor = textColor;
/// So far there are no problems. He makes the choice.
app.executeMenuCommand("Find Fill Color menu item");

/// But here the Grayscale can not convert to CMYK ??
refDoc.selection.fillColor = myCmykColor(0, 0, 0, 3);

 

 

 

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

CarlosCanto
Community Expert
Community Expert
Sep 27, 2023 6:32 AM UTC

what m1b said,

 

another way, after making your selection, you can change the default document color again

 

/// But here the Grayscale can not convert to CMYK ??
//refDoc.selection.fillColor = myCmykColor(0, 0, 0, 3);

refDoc.defaultFillColor = myCmykColor(0, 0, 0, 3);

Translate
Participating Frequently
Sep 27, 2023 7:27 AM UTC
thanks CarlosCanto
Translate
m1b
Community Expert
Community Expert
Sep 27, 2023 6:27 AM UTC

Hi @Goldberg_1453, the problem may be that in the last line "refDoc.selection" resolves to an Array, which doesn't have a fillColor property. You can loop over the elements of the array and try setting the fillColor of each:

(function () {

    var refDoc = app.activeDocument;
    var textColor = new GrayColor();
    textColor.gray = 3;
    refDoc.defaultFillColor = textColor;
    /// So far there are no problems. He makes the choice.
    app.executeMenuCommand("Find Fill Color menu item");

    // loop over each element of selection:
    var items = refDoc.selection;
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        item.fillColor = myCmykColor(0, 0, 0, 3);
    }

})();

function myCmykColor(c, m, y, k) {
    var ink = new CMYKColor();
    ink.cyan = c;
    ink.magenta = m;
    ink.yellow = y;
    ink.black = k;
    return ink;
};

I put the code inside an IIFE to keep the global scope clean and moved the function to the end (just for neatness). You don't have to do those, but look at the loop part.

- Mark

Translate