• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Get Colour out of a multidimenisonal Array

Engaged ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hello community!

Before I start a complex task I need to define some colors. To be able to use and get them individually, I pack them into an array:

var myDoc = app.documents[0];

var myPage = myDoc.pages[0];

var myColour = [];

var myColourName = [];

var selectedColour  = new Array();

    selectedColour [0] = new Object();

    selectedColour [0] ["colourName"] = "SF_Rot";

    selectedColour [0] ["colorValue"] = "15,100,100,0";

    selectedColour [1] = new Object();

    selectedColour [1] ["colourName"] = "SF_Grau";

    selectedColour [1] ["colorValue"] = "10,0,30,50";

    selectedColour [2] = new Object();

    selectedColour [2] ["colourName"] = "SF_Service";

    selectedColour [2] ["colorValue"] = "80,0,35,0";

   

var numColours = selectedColour.length;

Unfortunately, I can not get these values (simply with "alert") when required.

TOPICS
Scripting

Views

532

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 20, 2017 Apr 20, 2017

Hi cmoke73,

First above all, your array declaration can be shortened as follows:

var selectedColours = [

    { colourName: "SF_Rot",     colourValue: "15,100,100,0" },

    { colourName: "SF_Grau",    colourValue: "10,0,30,50" },

    { colourName: "SF_Service", colourValue: "80,0,35,0" }

];

I added an 's' to the identifier (selectedColours) to highlight the fact it is an array of objects, each element being a {colourName:string, colourValue:string} data structure. You can see the entire thing using aler

...

Votes

Translate

Translate
Guide ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hi cmoke73,

First above all, your array declaration can be shortened as follows:

var selectedColours = [

    { colourName: "SF_Rot",     colourValue: "15,100,100,0" },

    { colourName: "SF_Grau",    colourValue: "10,0,30,50" },

    { colourName: "SF_Service", colourValue: "80,0,35,0" }

];

I added an 's' to the identifier (selectedColours) to highlight the fact it is an array of objects, each element being a {colourName:string, colourValue:string} data structure. You can see the entire thing using alert( selectedColours.toSource() );

• Maybe it's not a good idea to use the String type for defining the colourValue property. An array of four numbers could be a better choice for dealing with InDesign DOM methods. Instead of

   colourValue: "a,b,c,d"

the syntax

   colourValue: [ a,b,c,d ]

would do the job.

• Considering that every selected colour (of your array) has the same object structure—that is, the {colourName, colourValue} scheme—it might be relevant to create a dedicated constructor in case some common features have to be invoked in a recurring way, say toString(), toRGB(), and so on.

Then you can fully customize the way you handle your data. For example,

function MyColour(/*str*/name,/*uint4[]*/value)

{

    this.colourName = name;

    this.colourValue = value.concat();

}

MyColour.prototype.toString = function()

{

    return this.colourName + ': '+ this.colourValue.join(',');

};

MyColour.prototype.toRGB = function()

{

    // todo

};

var selectedColours = [

    new MyColour("SF_Rot",     [15,100,100,0]),

    new MyColour("SF_Grau",    [10,0,30,5]),

    new MyColour("SF_Service", [80,0,35,0])

];

alert( selectedColours.join('\r') );

alert( selectedColours[1] );

Hope that helps.

@+

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hi Marc,

depending what our OP cmoke likes to do with the colors array, it might be a good idea to define some other properties for an individual color as well. Properties that InDesign can directly use for constructing a new color in a document. Using a different array structure—an associative array—like I am showing below.

var colorArray = [];

var x;

// Within the scope of the array the name of the color should be unique:

colorArray["SF_Rot"] =

{

    name : "SF_Rot" ,

    colorValue : [15,100,100,0] ,

    model : ColorModel.PROCESS ,

    space : ColorSpace.CMYK

};

// Add more colors to the array

// …

// Change values like that:

colorArray["SF_Rot"].colorValue = [0,100,100,10];

// Add new document (or use a document that already exists):

var doc = app.documents.add();

// Add all colors in the array or change existing colors according to the array :

for(x in colorArray)

{

    var color = doc.colors.itemByName(x);

  

    // Color does not exist, add color:

    if( !color.isValid )

    {

          doc.colors.add

          (

              colorArray

          )

    }

    // Color exists, make sure that all properties of the color

    // will be the same according to your array:

    else

    {

        color.properties = colorArray;

    }

};

This approach is more tight to the DOM.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hey Uwe!

Thanx for your answer.

Finally, I want to start a query, where the user can enter a colour (by means of a number). The individual values of the selected colour are used later for tables (lines, areas and font). This first part of the script will be a small database with the colour values.

That is what I made of your script:

var myDoc = app.documents[0];

var colourArray = []; 

var x; 

colourArray[0] =  {name : "SF_Rot", colorValue: [15,100,100,0], model: ColorModel.PROCESS, space: ColorSpace.CMYK}; 

colourArray[1] =  {name : "SF_Grau", colorValue: [0,0,30,50], model: ColorModel.PROCESS, space: ColorSpace.CMYK}; 

colourArray[2] =  {name : "SF_Service", colorValue: [80,0,35,0], model: ColorModel.PROCESS, space : ColorSpace.CMYK};      

function createColour() {

    var i = 0;

    start: while(true) {

      colourSwatch = prompt("Which color do you want to use? \r" + "1 = SF_Rot\r2 = SF_Grau\r3 = SF_Service", "" );

      i++;

      if(colourSwatch>colourArray.length || colourSwatch<1) continue start;

      break;

    }

return colourSwatch;

}

createColour();

alert ([colourArray[colourSwatch-1].colorValue]);

I changed the name of each array-content to an index-number bacause I later need that as a number (selection/input)

regards

Andreas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hi Marc.

Thank you for your reply. That sound very good (professional). Unfortunally much too professional for me as an beginner-amateur-javascripter. "this" is something I´ve never used. Just as little did I use "prototype.toString". But I will try to understand it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 23, 2017 Apr 23, 2017

Copy link to clipboard

Copied

LATEST

First step is done.

But: how can I use via entered number (colourSwatch) a value out from the array (colourArray) to color the edges of the tables/cells?

myTable.cells.everyItem().bottomEdgeStrokeColor = ?????????

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines