Skip to main content
Trevor:
Legend
September 21, 2011
Answered

Coloring a Font with a RGB etc. without adding the color to the document swatches.

  • September 21, 2011
  • 4 replies
  • 30051 views

Is there a way of coloring a font with a RGB, Lab or CMYK color without adding the color to the document swatches.

The only way I know is to add a color to the swatches or use one that already exists.

like

   app.selection[0].characters[0].fillColor=document.colors.add({colorValue: [255, 53, 160], space: ColorSpace.RGB});}

This has the undesired effect of cluttering up the swatches when using a lot of colors.

any ideas?

This topic has been closed for replies.
Correct answer Trevor:

Good Morning, Trevor!

(At least in my part of the world it's around 9 am)

Did you try the link using the forum?

I edited and tested it; seems alright now.

At least from my side using Firefox 27.0.1 on Mac OSX.

I used your snippet above and it always produced the same color over and over again.

Without throwing an error.

Now, after some testing I do know why.

You always have to set the properties for space and corresponding colorValue. No more, no less. Not the model.

You are right. An unnamed color  would  always be ColorModel.PROCESS.

Despite my attempts that cannot be changed…

If one is using the UI to generate an unnamed color, a SPOT color will be transfered into a PROCESS color. Always. In case of a spot color with LAB color values (my downloadable example) the above code snippet has to fail, because LAB has 3 color values.

I was using 4 values without setting the corresponding space to ColorSpace.CMYK.

My problem was: it failed silently.

The new colorValue was not assigned, the duplicated color was using the old ones.

So I come to the conclusion:

Your snippet will work as is, if both, space and colorValue are declared!

var myUnnamedColor = app.activeDocument.colors[-1].duplicate();

//NEEDED: space AND colorValue

//WILL THROW ERROR: model OTHER than ColorModel.PROCESS

myUnnamedColor.properties = {

    space:ColorSpace.CMYK,

    colorValue:[10,50,70,0]

    };

var newRectangleWithUnnamedColor = app.activeDocument.rectangles.add({

    geometricBounds:[0,0,100,100],

    fillColor: myUnnamedColor

    });

I'm glad this is sorted out now… :-)

PS. it seems that a mixedInk color will fall back to PROCESS CMYK when set to unnamed in the UI.

Even in a dedicated WEB document. But this is not proven. Just an observation after experimenting a bit.

Uwe


Good Morning Uwe!

After 3am by me 2am by you

I had tried the link and it did download but I have cs5 cs6 and cc but not cs5.5 and all the scripts worked on them may because of the file conversion.

So it looks like the following summary is all correct

All documents new contain

Swatches (Black, Registration, Paper and None) the index order will be the order that the swatches appear in the swatches panel

And colors in an alphabetical index order

named color "A" first "Z" last and then the unnamed colors.

A such all new documents colors[-1] will be an unnamed color which we can duplicated to produce other unnamed colors taking note that the duplication must be process and not spot colors.

So far so good, (not for long )

Unnamed colors are not read only so if we make a positive effort to remove them we can do that.

while (app.activeDocument.colors[-1].name == "") app.activeDocument.colors[-1].remove()

We now won't have any unnamed swatches to duplicate and will have to resort to John's tagged text file method in 3 above.

If there were no unnamed swatches and we try to duplicate colors[-1] and it was a color like "Yellow" then it seem's to crash indesign.

Anyway the below method should always work (for regular non tint etc. type colors).

// optimized for easy of use but not efficiency !!!

var doc = app.documents.add();

var p = doc.pages[0];

p.textFrames.add({contents: "RGB", geometricBounds: ["0mm", "0mm", "30mm", "30mm"], fillColor: addUnnamedColor([0, 0,255])}); // will be a RGB

p.textFrames.add({contents: "RGB", geometricBounds: ["0mm", "30mm", "30mm", "60mm"], fillColor: addUnnamedColor([0, 255,0], 1666336578)}); // will be a RGB because of value

p.textFrames.add({contents: "RGB", geometricBounds: ["0mm", "60mm", "30mm", "90mm"], fillColor: addUnnamedColor([65, 50, 102], ColorSpace.RGB)}); // will be a RGB

p.textFrames.add({contents: "RGB", geometricBounds: ["0mm", "90mm", "30mm", "120mm"], fillColor: addUnnamedColor([84, 90,40],"r")}); // will be a RGB

p.textFrames.add({contents: "RGB", geometricBounds: ["0mm", "120mm", "30mm", "150mm"], fillColor: addUnnamedColor([232, 0, 128],1)}); // will be a RGB

p.textFrames.add({contents: "Lab", geometricBounds: ["30mm", "0mm", "60mm", "30mm"], fillColor: addUnnamedColor([29.5, 67.5, -112])}); // will be a Lab because of -

p.textFrames.add({contents: "Lab", geometricBounds: ["30mm", "30mm", "60mm", "60mm"], fillColor: addUnnamedColor([100, -128, 127], 1665941826)}); // will be a Lab because of value

p.textFrames.add({contents: "Lab", geometricBounds: ["30mm", "60mm", "60mm", "90mm"], fillColor: addUnnamedColor([24.5, 16, -29], ColorSpace.LAB)}); // will be a Lab

p.textFrames.add({contents: "Lab", geometricBounds: ["30mm", "90mm", "60mm", "120mm"], fillColor: addUnnamedColor([36.8, -9, 27],"l")}); // will be a Lab

p.textFrames.add({contents: "Lab", geometricBounds: ["30mm", "120mm", "60mm", "150mm"], fillColor: addUnnamedColor([51, 78, 0], -1)}); // will be a Lab because of the 1

p.textFrames.add({contents: "CMYK", geometricBounds: ["60mm", "0mm", "90mm", "30mm"], fillColor: addUnnamedColor([82, 72, 0, 0])}); // will be a CMYK because there are 4 color values

p.textFrames.add({contents: "CMYK", geometricBounds: ["60mm", "30mm", "90mm", "60mm"], fillColor: addUnnamedColor([60, 0, 100, 0], 1129142603)}); // will be a CMYK because of value

p.textFrames.add({contents: "CMYK", geometricBounds: ["60mm", "60mm", "90mm", "90mm"], fillColor: addUnnamedColor([84, 90,40, 0], ColorSpace.CMYK)}); // will be a CMYK

p.textFrames.add({contents: "CMYK", geometricBounds: ["60mm", "90mm", "90mm", "120mm"], fillColor: addUnnamedColor([67, 53, 97.6, 21.7], "c")}); // will be a CMYK

p.textFrames.add({contents: "CMYK", geometricBounds: ["60mm", "120mm", "90mm", "150mm"], fillColor: addUnnamedColor([0, 100, 0, 0], 0)}); // will be a CMYK

function addUnnamedColor (cValue, space, docToAddColor) {

    docToAddColor = app.documents.length && (docToAddColor || (app.properties.activeDocument && app.activeDocument) || app.documents[0]);

    if (!docToAddColor) return;

    var lastColor = docToAddColor.colors[-1];

    if (!cValue) cValue = [0,0,0,0];

    if (space == 1129142603 || cValue && cValue.length == 4) space = ColorSpace.CMYK;

    else if ((space && space < 0) ||  space && space == 1665941826 || (space && /L|-/i.test(space.toString())) || (cValue && /-/.test(cValue ))) space = ColorSpace.LAB;

    else if ((space && space > 0) || space && space == 1666336578 || (space && /R/i.test(space.toString())) || (cValue && cValue.length == 3)) space = ColorSpace.RGB;

    else space = ColorSpace.CMYK;

    app.doScript (

        """

        var newUnnamedColor = (lastColor.name == "") ? lastColor.duplicate() : taggedColor();

        newUnnamedColor.properties = {space: space, colorValue: cValue};

        """,

        ScriptLanguage.javascript,

        undefined,

        UndoModes.FAST_ENTIRE_SCRIPT

    );

     function taggedColor() { // need to use this if no unnamed exists

             var tagString = "<ASCII-" + ($.os[0] == "M" ? "MAC>\r " : "WIN>\r ") + "<cColor:COLOR\:CMYK\:Process\:0.1\,0.95\,0.3\,0.0><cColor:>"; // would make more sense to apply the correct value in the tagged text file but I can't be bothered !

             var tempFile = new File (Folder (Folder.temp) + "/ " + (new Date).getTime() + ".txt");

             tempFile.open('w');

             tempFile.write(tagString);

             tempFile.close();

             var tempFrame = docToAddColor.pages[-1].textFrames.add();

             $.sleep(250);

             tempFrame.place(tempFile);

             tempFrame.remove();

             tempFile.remove();

         return docToAddColor.colors[-1];

     }

    return newUnnamedColor;

}

Shall apply the function to delete and replace swatch on the other thread at a more sane time

Regards

Trevor

4 replies

Community Expert
January 16, 2023

Hi together,

update on some details.

 

With InDesign 2021 version 16 the developers added the new color mode HSB next to LAB, RGB and CMYK.

So my statement from 2014 is not true anymore:

 

/*
IDs for the four different sub-menus of the "Color Panel":  
29188    Lab
29189    CMYK
29190    RGB
*/

 

 

The old IDs invoke "different" sub-menus of the "Color Panel" and one ID was simply added:

 

/*
InDesign 2021 version 16 and above:

IDs for the three different sub-menus of the "Color Panel":
29188    HSB
29189    Lab
29190    CMYK
29191    RGB
*/

 

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
January 16, 2023
app.panels.itemByName("$ID/kColor").visible = true;

try
{
	
	/*
		InDesign 2021 version 16 and above:

		IDs for the four different sub-menus of the "Color Panel":
		29188    HSB
		29189    Lab
		29190    CMYK
		29191    RGB
	*/

	// RGB
	app.scriptMenuActions.itemByID( 29191 ).invoke()
	
}catch(e)
{
	$.writeln( "ERROR: " + e.number +", " + e.message )
};

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Jongware
Community Expert
Community Expert
December 10, 2011

Gosh I have no idea what prompted me to visit this thread again

... Here is a one-liner to set the color of a text selection to any RGB, Lab, or CMYK color without adding it to the Swatches list:

app.selection[0].fillColor.properties = {space:ColorSpace.RGB,colorValue:[255,255,0]};

Impressed? Well, if you try it you'll notice it won't work straight away. There Is A Catch! (Two of 'em, actually.)

.

.

.

.

.

(spoiler space)

.

.

.

.

.

Catch #1: You can only change the color of text if it already has an 'undefined' color!

Now usually, you'll find your text already comes with a swatch applied to it, even though it's simply "Black", and so you cannot use this trick. If you do want to use it, you have to change it first to the fillColor of a text that already has an undefined color applied to it; then you can change it to anything you want. But you cannot create a piece of text to act as model to begin with, using a script (well, other than Harbs' workaround with a temporary snippet file) -- a real Catch-22.

To get it to work, draw a new text frame somewhere on the pasteboard in your document and enter this text into it: "A Jongware Solution". (Use exactly this text.) Change the color to something random, using the Color panel. Use the Script Label panel to add a label to the text frame -- use "color placeholder". Then, just before the line that sets other text to any color, insert this:

app.selection[0].fillColor = app.activeDocument.textFrames.item("color placeholder").characters[0].fillColor;

At first glance, this appears to work, but I noticed The Weirdest Thing. If you apply this color to your newly selected text, you'll find it initially is set to the placeholder color. That's to be expected. The second line (the one at the top of this post) also works, as you will see the color change to the new settings.

However. Catch #2

Check what happens to the placeholder text ... It also changes its color! Curioser & curioser, if you used this trick a couple of times in the same document, you'll find that every item with the same color also changes! It's kinda like ... you have a swatch ... but not really ...

Trevor:
Trevor:Author
Legend
December 11, 2011

Thanks Jongware

I'm afraid I agree with the spoiler lines you put it

I think the catches are quite big,

Oh well, but as it happens I was also thinking of this post lately and thought of you because I had seen on another post that you wrote that one can use menu actions

So I know this is a cheat because my question was how to add color with no swatch but I think (based on Harbs and your replys) that its better to add and then remove the swatches and replace the removed swatches with unnamed ones.

But It's easier said than done

Please see the script below and see if you can correct the problem line

to run the script a letter ... which is colored with a swatch must be selected.

var myMenu = app.menuActions.item('$ID/Swatches_WinMenu');

myMenu.invoke();

var mySwatchRemoveMenuItem = app.menuActions.item('$ID/Delete Swatch...');

mySwatchRemoveMenuItem.invoke();

var makeUnnamedSwatch = app.menuActions.item('$ID/Unnamed Swatch');

// The above line is the problem line I don't know what to put instead of '$ID/Unnamed Swatch'

// to get it to choose replace with unnamed swatch as there dosn't seem to be a valid $ID for it.

makeUnnamedSwatch.invoke();

Once again

Thanks

Jongware
Community Expert
Community Expert
December 11, 2011

I'm afraid it's not that straightforward. "Delete Swatch" is a menu option but "Replace with Unnamed Color" is not, so there is nothing to invoke.

Harbs.
Legend
September 22, 2011

Using standard methods, the only way to add colors is by adding a swatch.

If you have an unnamed color in your document, you can apply it to another object using obj1.fillColor = obj2.fillColor.

So, if you can get the unnamed color in your document somehow, you can apply it via scripting.

One way to do that would be to write a snippet programmatically with the desired color, import that, apply the color and remove the placed snippet.

You can check if the color exists already in the doc before doing that...

Harbs

John Hawkinson
Inspiring
September 22, 2011

Definitely easier with InDesign Tagged Text:

<ASCII-MAC>

color test

<cColor:COLOR\:CMYK\:Process\:0.75\,0.5\,0.53\,0.10>seventy<cColor:>

nocolor test

Harbs.
Legend
September 22, 2011

Good idea.

John Hawkinson
Inspiring
September 22, 2011

InDesign Tagged Text?

In short: no, not really, seems like a limitation.