Skip to main content
zaitexspa
Known Participant
January 20, 2023
Answered

Change fill color based on content

  • January 20, 2023
  • 5 replies
  • 1459 views

Hi everybody.

I browsed hours and hours without finding a useful solution to my issue: I will have to import data within a table.

Among the imported data, there will be a string of text having RGB or CMKY value.

I'd like that such content will be processed by the script in order to fill a cell background based on value.

Is is possible?

This topic has been closed for replies.
Correct answer rob day

@rob day is there a way to only select the pages of a document, excluding the master page?

 


This should work:

 

var tf = d.pages.everyItem().textFrames.everyItem().getElements();

5 replies

rob day
Community Expert
January 26, 2023

If we assume 3 numbers equals an RGB value and 4 a CMYK value, then here’s an example that checks all the cells in the 3rd column of the document’s first table, and sets the fillColor to the content values:

 

var d = app.activeDocument
var c = d.stories.everyItem().tables[0].columns[2].cells.everyItem().getElements();


var fc, cv;
var ca= []
for (var i = 0; i < c.length; i++){
    cv = c[i].contents.split(",");
    for (var n = 0; n < cv.length; n++){
        ca.push(Number(cv[n]))
    };   
    if (ca.length == 3) {
        fc = makeSwatch(d, ca.toString())
        fc.properties = {space:ColorSpace.RGB, model:ColorModel.PROCESS, colorValue:ca}
        c[i].fillColor = fc;
    } 
    if (ca.length == 4) {
        fc = makeSwatch(d, ca.toString())
        fc.properties = {space:ColorSpace.CMYK, model:ColorModel.PROCESS, colorValue:ca}
        c[i].fillColor = fc;
    } 
    ca = [];
};   


/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n});
    }
}

 

Sample result:

 

zaitexspa
zaitexspaAuthor
Known Participant
January 30, 2023

Hi @rob day 

many many thanks for your help!

I managed to assign a fill color to the text frame.

Plus, I managed to assign the same color to the included text, which is useless to keep readable.

I just tweaked your code as follows:

var d = app.activeDocument
//var c = d.stories.everyItem().tables[0].columns[2].cells.everyItem().getElements();
var c = d.textFrames.everyItem().getElements();

var fc, cv, cn;
var ca= []
for (var i = 0; i < c.length; i++){
    cn = c[i].lines; // change color of the included text
    cv = c[i].contents.split(",");
    for (var n = 0; n < cv.length; n++){
        ca.push(Number(cv[n]))
    };

    if (ca.length == 3) {
        fc = makeSwatch(d, ca.toString())
        fc.properties = {space:ColorSpace.RGB, model:ColorModel.PROCESS, colorValue:ca}
        c[i].fillColor = fc;
    } 
    if (ca.length == 4) {
        fc = makeSwatch(d, ca.toString())
        fc.properties = {space:ColorSpace.CMYK, model:ColorModel.PROCESS, colorValue:ca}
        c[i].fillColor = fc;
    } 
    ca = [];

    for(var w = 0; w < cn.length; w++) {
        cn[w].fillColor = fc;
    }
};   


/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n});
    }
}

My last concern is: how can I change the background fill color (and the included text color) of a specific textframe?

If I'm not wrong, in your case, you pointed out every cell of the third column of a table; in my case, I'd like to select a specific textframe. 

So, rather than use

var c = d.textFrames.everyItem().getElements();

which might be catching all the textFrames in the document, I'd like to run the script just for a specific one.

I tried alternatively unsuccessfully to use this:

var c = d.textFrames.itemByName("test");
cn = d.contents;

but... it won't work and it makes useless the for loop. I got an error by Indesign.

 

zaitexspa
zaitexspaAuthor
Known Participant
June 29, 2023

This should work:

 

var tf = d.pages.everyItem().textFrames.everyItem().getElements();

Hi @rob day 

I switched to Ventura and latest Indesign.

It happens the script affects the master pages too.

How to avoid?

Community Expert
January 26, 2023

That doesn't make it much clearer. Whre are the colour values? How are they represented?

Community Expert
January 23, 2023

@rob day said: "If it’s 4 numbers we can assume it’s a CMYK value…"

Playing devil's advocate: Or perhaps an rgba value ??!

 

Fun aside, the OP states that it will be RGB or CMYK. So that's clear, I think:

"Among the imported data, there will be a string of text having RGB or CMKY value."

 

@zaitexspa , it's important how the data is presented.

The word "Among" is showing that there is perhaps more than three or four numbers.

So you'd need an algorithm to part the color values from all other contents in a given table cell.

 

In essence: we need examples.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

zaitexspa
zaitexspaAuthor
Known Participant
January 26, 2023

Hi guys, thanks for your reply.

I can import RGB or CMYK value like 255,255,255 or 0,0,0,100 - I would rather import those values from csv, so no problem.

Attached an example.

 

Community Expert
January 20, 2023

Same question as Rob: can you show how those values appear in the cells? A screenshot would help.

Community Expert
January 20, 2023

Yes, that's possible. Show us what you've got.

zaitexspa
zaitexspaAuthor
Known Participant
January 20, 2023

Techincally speaking, I am not a developer and I'm sorry because I cannot write a working script.

I assumed it is possible to start from a basic concept like this and working on something like this one.

I sneaked peak into this link to understand a bit object, method, properties, and it seems feasible.

I was wondering if I'm the only one in need for this matter.

rob day
Community Expert
January 20, 2023

Hi @zaitexspa , the fill color’s value is an array of numbers (.colorValue), so for a script to work you would have to convert the cell’s text into an array of numbers and know what the exprected color mode is. Is the text 0/0/0/0, or 0|0|0|0, or 0,0,0,0? If it’s 4 numbers we can assume it’s a CMYK value, but if it’s 3 is it RGB, Lab, or HSB?

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Color.html