Home > General info > QuickTip: Add Custom (CMYK/RGB/HEX) Colors to Document

QuickTip: Add Custom (CMYK/RGB/HEX) Colors to Document

Want to add custom colors to document? Wanna do it fast and painless? Here is short function that will do all job for you!

Just a brief description🙂

Adding color to document can sometimes cause problems when we want to add color with existing name, but with this short function, you can avoid almost all possible problems while adding color. You can either just create new color, or you can even pass created color directly to object. (See usage)

First the function:

function myColorAdd(myDocument, myColorName, myColorModel, myColorValue){
    if(myColorValue instanceof Array == false){
        myColorValue = [(parseInt(myColorValue, 16) >> 16 ) & 0xff, (parseInt(myColorValue, 16) >> 8 ) & 0xff, parseInt(myColorValue, 16 ) & 0xff ];
        myColorSpace = ColorSpace.RGB;
    }else{
        if(myColorValue.length == 3)
          myColorSpace = ColorSpace.RGB;
        else
          myColorSpace = ColorSpace.CMYK;
    }
    try{
        myColor = myDocument.colors.item(myColorName);
        myName = myColor.name;
    }
    catch (myError){
        myColor = myDocument.colors.add();
        myColor.properties = {name:myColorName, model:myColorModel, space:myColorSpace ,colorValue:myColorValue};
    }
    return myColor;
}

Thanks to Teus de Jong (aka Jongware) for HEX conversion thingy!🙂
This can also be found on Adobe’s InDesign Scripting forum.

How to use:

// add CMYK color
myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, [80,50,30,10]);

// add RGB color
myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, [33,66,99]);

// add HEX color
myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, "ABCDEF");

Directly add and assign color to object: (in this case “selected”)

// add CMYK color to document
// and asign it to selected object
app.selection[0].fillColor = myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, [80,50,30,10]);

That’s it!

Fast and really handy!

Have fun!😀

  1. September 11, 2010 at 11:07

    Hi Tomaxxi,

    Marc did write a script you may have interest in :
    http://www.indiscripts.com/post/2009/06/web-color-picker

    • September 11, 2010 at 13:26

      Hey Loic!

      Interesting, I never saw that😀 Thanks!

  1. October 7, 2010 at 17:33

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: