- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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] );
);
This post originally written in German and has been translated for your convenience. When you reply, it will also be translated back to German.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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;
);
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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;
);
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: set row color by given RGB values
Thanks! These solutions are truely effective, and efficient!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: set row color by given RGB values
2 things
- The syntax for setting the color of the row state color was incorrect.
- 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]) );
);