cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
PeimeiDa
Staff

set row color by given RGB values

Hi, I'm trying to set row color (not cell color) by given RGB values in my columns. However, i can not get the correct loop for assigning the custom colors that I want. Could anyone help?

Code and datatable attached. Thanks!

 

// Get the current data table
dt = Current Data Table();

// Initialize the color lists
RedList = Column(dt, "Red") << Get Values;
GreenList = Column(dt, "Green") << Get Values;
BlueList = Column(dt, "Blue") << Get Values;

// Ensure we do not exceed the number of rows or the number of items in any color list
minLength = Min( N Rows(dt), N Items(RedList), N Items(GreenList), N Items(BlueList) );

// Loop to set the row colors
For( i = 1, i <= minLength, i++,
    // Clear any previous selections
    dt << Clear Select;
    // Select the current row
    dt << Select Where( Row() == i );
    // Set the row color
    dt << Set Row Colors( RedList[i], GreenList[i], BlueList[i] );
);
1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: set row color by given RGB values

This will be a bit slow due to all selections happening

Names Default To Here(1);

dt = Current Data Table();

dt << show window(0);
For Each Row(dt,
	dt << Clear Select;
	dt << Select Rows(Row());
	dt << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
);
dt << show window(1);

if more speed is required it could be made faster by selection multiple rows (all rows which have specific color at the same time) or by setting row states.

Names Default To Here(1);

dt = Current Data Table();

Summarize(dt, uniq = By(:Color), r = Min(:Red), g = Min(:Green), b = Min(:Blue));

For Each({cur_color, idx}, uniq,
	dt << Select Where(:Color == cur_color);
	dt << Colors(RGB Color(Eval List({r[idx], g[idx], b[idx]}) / 255));
	dt << Clear Select;
);

jthi_0-1713725416561.png

Nice picture!

 

Edit:

Just came to my mind that you could also use Summary table to set the colors

Names Default To Here(1);

dt = Current Data Table();

dt_summary = dt << Summary(
	Group(:Color),
	Mean(:Red),
	Mean(:Green),
	Mean(:Blue),
	Freq("None"),
	Weight("None"),
	statistics column name format("column")
);

For Each Row(dt_summary,
	dt_summary << Select Rows(Row());
	dt_summary << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
	dt_summary << Clear Select;
);
Close(dt_summary, no save);

 

Edit2: One more option looping over the whole table but without selections

Names Default To Here(1);

dt = Current Data Table();

For Each Row(dt,
	Row State(Row()) = Color State(Eval List({:Red, :Green, :Blue} / 255));
);

I couldn't figure out how to convert Color State (for example Color State(-15527920)) to number (should be 512.925536155701) so I could have built row state matrix and use << Set Row States.

-Jarmo

View solution in original post

3 REPLIES 3
jthi
Super User

Re: set row color by given RGB values

This will be a bit slow due to all selections happening

Names Default To Here(1);

dt = Current Data Table();

dt << show window(0);
For Each Row(dt,
	dt << Clear Select;
	dt << Select Rows(Row());
	dt << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
);
dt << show window(1);

if more speed is required it could be made faster by selection multiple rows (all rows which have specific color at the same time) or by setting row states.

Names Default To Here(1);

dt = Current Data Table();

Summarize(dt, uniq = By(:Color), r = Min(:Red), g = Min(:Green), b = Min(:Blue));

For Each({cur_color, idx}, uniq,
	dt << Select Where(:Color == cur_color);
	dt << Colors(RGB Color(Eval List({r[idx], g[idx], b[idx]}) / 255));
	dt << Clear Select;
);

jthi_0-1713725416561.png

Nice picture!

 

Edit:

Just came to my mind that you could also use Summary table to set the colors

Names Default To Here(1);

dt = Current Data Table();

dt_summary = dt << Summary(
	Group(:Color),
	Mean(:Red),
	Mean(:Green),
	Mean(:Blue),
	Freq("None"),
	Weight("None"),
	statistics column name format("column")
);

For Each Row(dt_summary,
	dt_summary << Select Rows(Row());
	dt_summary << Colors(RGB Color(Eval List({:Red, :Green, :Blue}) / 255));
	dt_summary << Clear Select;
);
Close(dt_summary, no save);

 

Edit2: One more option looping over the whole table but without selections

Names Default To Here(1);

dt = Current Data Table();

For Each Row(dt,
	Row State(Row()) = Color State(Eval List({:Red, :Green, :Blue} / 255));
);

I couldn't figure out how to convert Color State (for example Color State(-15527920)) to number (should be 512.925536155701) so I could have built row state matrix and use << Set Row States.

-Jarmo
PeimeiDa
Staff

Re: set row color by given RGB values

Thanks! These solutions are truely effective, and efficient!

txnelson
Super User

Re: set row color by given RGB values

2 things

  1. The syntax for setting the color of the row state color was incorrect.
  2. Using the Select Where() made the looping very slow.

Here is my modification

// Get the current data table
dt = Current Data Table();

// Initialize the color lists
RedList = Column(dt, "Red") << Get Values;
GreenList = Column(dt, "Green") << Get Values;
BlueList = Column(dt, "Blue") << Get Values;

// Ensure we do not exceed the number of rows or the number of items in any color list
minLength = Min( N Rows(dt), N Items(RedList), N Items(GreenList), N Items(BlueList) );

// Loop to set the row colors
For( i = 1, i <= minLength, i++,
    // Clear any previous selections
    dt << Clear Select;
    // Select the current row
    dt << Select rows( i );
    // Set the row color
    dt << Colors( rgb color(redlist[i],greenlist[i],bluelist[i]) );
);
Jim