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;
};