Parse color model

Hi,

I would like to use the color model (from ColorManager node e.g.) within my node. I'm just interested in this color information as long as it belongs to a nominal column and then I need the summary instead of the color information belonging to each row.

I managed to extract the information like this:

- test whether there is any color model

- get the model by retrieving the ModelContent object obtaining from the column spec

- then I can check for the value of "color_model_class"

- if this is nominal, I parse into entries of "color_model" and collect the information

nominal value 1 -> color 1

nominal value 2 -> color 2

and so on...

 

I'm just curious whether there is any other way to retrieve this information. Any comments to this approach are welcome.

AJ

Hi AJ,

you can get the color of a row using the data table spec. This will give you the color of the first DataColumn containing the column (this is the one which we use in KNIME as well).

for (DataRow row : dTable) {
   Color rowColor = dTable.getDataTableSpec().getRowColor(row).getColor();
}

alternatively, here you will get the color of the column "colorcolumn"

 DataTableSpec ds = dTable.getDataTableSpec();
 ColorHandler ch = ds.getColumnSpec(colorcolumn).getColorHandler();
for (DataRow row : dTable) {
 Color c = ch.getColorAttr(row.getCell(colorcolumn)).getColor();
}

 

Is this what you were searching for?

Cheers, Iris

Hi Iris,

I don't understand your second example. I thought the KNIME color model is applied rowwise. The example looks like columns may have individual colors assigned? Or even single cells?

AJ

The Knime Color Model is always attached to one column. If I call getRowcolor, the first column with an Color Model attached is used for the color of the row.

However, what I do in the second example is:

I get the ColorHandler from the column. This ColorHandler can now assign a color to a DataCell, based on its value. If one of the "rules" in my ColorHandler apply for the DataCell (e.g. DataCell -> nominal1 and nominal1 -> color1), the Color is returned. (If not, a default color (grey) is used).

Does this help? Iris

 

Thanks a lot for your explanation. Now, the code is clear to me.

Anyhow, to retrieve the color model information (which color has been used for which value), I have to stick to my approach :-)

Well, you can use the tip from Iris with the domain values (as you only want it for nominal columns). That seems to be more idiomatic, future-proof for me.

As far as I see, in Iris example code the table is parsed rowwise to retrieve color values. Do you mean that I should 'collect' color/value information by iterating over the whole table? What about performance then?

I mean you can use the same technic as Iris, but instead of going through the whole table, you need to visit only the domain values  of the color column. Something like this:

DataTableSpec ds = dTable.getDataTableSpec();
DataColumnSpec cs = ds.getColumnSpec(colorcolumn);
 ColorHandler ch = cs.getColorHandler();
if (cs.getDomain() == null) {
  throw new IllegalStateException("Not a nominal column");
}
for (DataCell cell : cs.getDomain().getValues()) {
 Color c = ch.getColorAttr(cell).getColor();
}

Cheers, gabor

That's a nice approach, thank you!

I did not now that domain values contain their colors.