Skip to main content
Peter_Nitras
Participating Frequently
August 24, 2023
Answered

Illustrator coding: new CMYKColor(0, 0, 0, 100); does not work

  • August 24, 2023
  • 1 reply
  • 784 views

Hi

I am trying to create a set of artboards via script.
My document mode is CMYK

docPreset.colorMode = DocumentColorSpace.CMYK;


For whatever reason I am able to create a rectangle on a certain artboard and fill it with rgb color.

// blackRectangle.filled = true;
// blackRectangle.fillColor = new RGBColor(0, 0, 0); // Black CMYK color
blackRctangle.stroked = false;



but when i try to do this in CMYK it does not fill the color.
The no stroke does work.

// Set the fill color to CMYK black (0, 0, 0, 100) // does not work
blackRectangle.filled = true;
blackRectangle.fillColor = new CMYKColor(0, 0, 0, 100);
// Remove the stroke (works)
blackRectangle.stroked = false;


Thanks for your exxpertise.

This topic has been closed for replies.
Correct answer m1b

Hi @Peter_Nitras, here is some code that should clear things up for you. The simple answer is that (for some strange reason!) new RGBColor() and new CMYKColor() do not take arguments. You have to set the cyan, magenta, yellow and black properties afterwards. Have a look in my convenience function "colorWithBreakdown" to see it.

- Mark

/**
 * Example to show creating new document
 * and creating RGB and CMYK color
 * including some convenience functions
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/illustrator-coding-new-cmykcolor-0-0-0-100-does-not-work/m-p/14033691
 */
(function () {

    var docCMYK = newDocument('testCMYK', ImageColorSpace.CMYK, 100, 100),
        rectangleCMYK = makeRectanglePathItem(docCMYK, docCMYK.artboards[0].artboardRect);

    var docRGB = newDocument('testRGB', ImageColorSpace.RGB, 100, 100),
        rectangleRGB = makeRectanglePathItem(docRGB, docRGB.artboards[0].artboardRect);

    rectangleCMYK.fillColor = colorFromBreakdown([50, 0, 0, 50]);
    rectangleRGB.fillColor = colorFromBreakdown([50, 255, 0]);

})();


/**
 * Create a new basic document with some options.
 * @author m1b
 * @version 2022-07-21
 * @param {String} name - the title of the document.
 * @param {ImageColorSpace} colorSpace - ImageColorSpace.RGB or ImageColorSpace.CMYK.
 * @param {Number} width - width of the default artboard.
 * @param {Number} height - height of the default artboard.
 * @returns {Document}
 */
function newDocument(name, colorSpace, width, height) {

    var myDocPreset = new DocumentPreset(),
        myDocPresetType;

    myDocPreset.title = name;
    myDocPreset.width = width || 1000;
    myDocPreset.height = height || 1000;

    if (
        colorSpace == ImageColorSpace.CMYK
        || colorSpace == ImageColorSpace.GrayScale
        || colorSpace == ImageColorSpace.DeviceN
    ) {
        myDocPresetType = DocumentPresetType.BasicCMYK;
        myDocPreset.colorMode = DocumentColorSpace.CMYK
    }

    else { // if (colorSpace == ImageColorSpace.RGB) {
        myDocPresetType = DocumentPresetType.BasicRGB;
        myDocPreset.colorMode = DocumentColorSpace.RGB
    }

    return app.documents.addDocument(myDocPresetType, myDocPreset);

};


/**
 * Draws and returns a rectangle path item.
 * @author m1b
 * @version 2023-08-24
 * @param {Document} doc - an Illustrator Document.
 * @param {Array<Number>} rect - [L, T, R, B].
 * @returns {PathItem}
 */
function makeRectanglePathItem(doc, rect) {

    var left = rect[0],
        top = rect[1],
        right = rect[2],
        bottom = rect[3];

    var newPath = doc.activeLayer.pathItems.add();
    newPath.setEntirePath([
        [left, top],
        [right, top],
        [right, bottom],
        [left, bottom],
    ]);
    newPath.closed = true;

    return newPath;

};


/**
 * Returns Color object when given an array of values.
 * @author m1b
 * @version 2022-10-08
 * eg. [50] = GrayColor
 *     [255,128,0] = RGBColor
 *     [10,20,30,0] = CMYKColor
 * @param {Array<Number>} breakdown - an array of color breakdown values.
 * @returns {Color}
 */
function colorFromBreakdown(breakdown) {

    if (breakdown == undefined)
        return;

    // sanity check
    for (var i = 0; i < breakdown.length; i++)
        if (breakdown[i] < 0)
            breakdown[i] = 0;

    var colr;

    switch (breakdown.length) {

        case 1: // [K]
            colr = new GrayColor();
            colr.gray = breakdown[0];
            break;

        case 3: // [R,G,B]
            colr = new RGBColor();
            colr.red = breakdown[0];
            colr.green = breakdown[1];
            colr.blue = breakdown[2];
            break;

        case 4: // [C,M,Y,K]
            colr = new CMYKColor();
            colr.cyan = breakdown[0];
            colr.magenta = breakdown[1];
            colr.yellow = breakdown[2];
            colr.black = breakdown[3];
            break;

        default:
            throw Error('colorFromBreakdown: couldn\'t parse color (' + breakdown + ')');

    }

    return colr;

};

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
August 24, 2023

Hi @Peter_Nitras, here is some code that should clear things up for you. The simple answer is that (for some strange reason!) new RGBColor() and new CMYKColor() do not take arguments. You have to set the cyan, magenta, yellow and black properties afterwards. Have a look in my convenience function "colorWithBreakdown" to see it.

- Mark

/**
 * Example to show creating new document
 * and creating RGB and CMYK color
 * including some convenience functions
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/illustrator-coding-new-cmykcolor-0-0-0-100-does-not-work/m-p/14033691
 */
(function () {

    var docCMYK = newDocument('testCMYK', ImageColorSpace.CMYK, 100, 100),
        rectangleCMYK = makeRectanglePathItem(docCMYK, docCMYK.artboards[0].artboardRect);

    var docRGB = newDocument('testRGB', ImageColorSpace.RGB, 100, 100),
        rectangleRGB = makeRectanglePathItem(docRGB, docRGB.artboards[0].artboardRect);

    rectangleCMYK.fillColor = colorFromBreakdown([50, 0, 0, 50]);
    rectangleRGB.fillColor = colorFromBreakdown([50, 255, 0]);

})();


/**
 * Create a new basic document with some options.
 * @author m1b
 * @version 2022-07-21
 * @param {String} name - the title of the document.
 * @param {ImageColorSpace} colorSpace - ImageColorSpace.RGB or ImageColorSpace.CMYK.
 * @param {Number} width - width of the default artboard.
 * @param {Number} height - height of the default artboard.
 * @returns {Document}
 */
function newDocument(name, colorSpace, width, height) {

    var myDocPreset = new DocumentPreset(),
        myDocPresetType;

    myDocPreset.title = name;
    myDocPreset.width = width || 1000;
    myDocPreset.height = height || 1000;

    if (
        colorSpace == ImageColorSpace.CMYK
        || colorSpace == ImageColorSpace.GrayScale
        || colorSpace == ImageColorSpace.DeviceN
    ) {
        myDocPresetType = DocumentPresetType.BasicCMYK;
        myDocPreset.colorMode = DocumentColorSpace.CMYK
    }

    else { // if (colorSpace == ImageColorSpace.RGB) {
        myDocPresetType = DocumentPresetType.BasicRGB;
        myDocPreset.colorMode = DocumentColorSpace.RGB
    }

    return app.documents.addDocument(myDocPresetType, myDocPreset);

};


/**
 * Draws and returns a rectangle path item.
 * @author m1b
 * @version 2023-08-24
 * @param {Document} doc - an Illustrator Document.
 * @param {Array<Number>} rect - [L, T, R, B].
 * @returns {PathItem}
 */
function makeRectanglePathItem(doc, rect) {

    var left = rect[0],
        top = rect[1],
        right = rect[2],
        bottom = rect[3];

    var newPath = doc.activeLayer.pathItems.add();
    newPath.setEntirePath([
        [left, top],
        [right, top],
        [right, bottom],
        [left, bottom],
    ]);
    newPath.closed = true;

    return newPath;

};


/**
 * Returns Color object when given an array of values.
 * @author m1b
 * @version 2022-10-08
 * eg. [50] = GrayColor
 *     [255,128,0] = RGBColor
 *     [10,20,30,0] = CMYKColor
 * @param {Array<Number>} breakdown - an array of color breakdown values.
 * @returns {Color}
 */
function colorFromBreakdown(breakdown) {

    if (breakdown == undefined)
        return;

    // sanity check
    for (var i = 0; i < breakdown.length; i++)
        if (breakdown[i] < 0)
            breakdown[i] = 0;

    var colr;

    switch (breakdown.length) {

        case 1: // [K]
            colr = new GrayColor();
            colr.gray = breakdown[0];
            break;

        case 3: // [R,G,B]
            colr = new RGBColor();
            colr.red = breakdown[0];
            colr.green = breakdown[1];
            colr.blue = breakdown[2];
            break;

        case 4: // [C,M,Y,K]
            colr = new CMYKColor();
            colr.cyan = breakdown[0];
            colr.magenta = breakdown[1];
            colr.yellow = breakdown[2];
            colr.black = breakdown[3];
            break;

        default:
            throw Error('colorFromBreakdown: couldn\'t parse color (' + breakdown + ')');

    }

    return colr;

};
Peter_Nitras
Participating Frequently
August 24, 2023

Thank you! That is great to have. I adapted the code a bit to return a random color, but at least now I now the arguments have to be set afterwards. Thank you very much!

 

m1b
Community Expert
Community Expert
August 24, 2023

You're welcome. Have fun!