Skip to content

Commit d0552e3

Browse files
committed
Add colorbar
1 parent d2b5d17 commit d0552e3

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/ui.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
widgets::{
1010
key_value_list_widget::KeyValueListWidget,
1111
slice_widget::{SliceParams, XyzWidget},
12-
title_bar::TitleBarWidget,
12+
title_bar::TitleBarWidget, color_bar::ColorBarWidget,
1313
},
1414
};
1515

@@ -19,7 +19,7 @@ static MODE_TITLES: [&str; 2] = ["Voxel", "Metadata"];
1919
pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
2020
let layout = Layout::default()
2121
.direction(Direction::Vertical)
22-
.constraints([Constraint::Length(1), Constraint::Min(0)].as_ref())
22+
.constraints([Constraint::Length(1), Constraint::Min(0), Constraint::Length(1)].as_ref())
2323
.split(frame.size());
2424

2525
// write out filename
@@ -31,6 +31,8 @@ pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
3131

3232
frame.render_widget(TitleBarWidget::new(&app.file_path, &MODE_TITLES, mode_index), layout[0]);
3333

34+
frame.render_widget(ColorBarWidget::new("Inferno", app.color_map, app.intensity_range.0, app.intensity_range.1), layout[2]);
35+
3436
match app.mode {
3537
crate::app::AppMode::Xyz => {
3638
let slice = SliceParams {

src/widgets/color_bar.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::utils::colors::colorous2tui;
2+
3+
pub struct ColorBarWidget<'a> {
4+
pub title: &'a str,
5+
pub gradient: colorous::Gradient,
6+
pub min: f32,
7+
pub max: f32,
8+
}
9+
10+
impl<'a> ColorBarWidget<'a> {
11+
pub fn new(title: &'a str, gradient: colorous::Gradient, min: f32, max: f32) -> Self {
12+
Self {
13+
title,
14+
gradient,
15+
min,
16+
max,
17+
}
18+
}
19+
}
20+
21+
impl tui::widgets::Widget for ColorBarWidget<'_> {
22+
fn render(self, area: tui::layout::Rect, buf: &mut tui::buffer::Buffer) {
23+
for i in 0..area.width {
24+
let color = self.gradient.eval_rational(i as usize, area.width as usize);
25+
buf.get_mut(area.x + i, area.y).set_bg(colorous2tui(color));
26+
}
27+
28+
// write title in the middle
29+
let title_len = self.title.len();
30+
let title_start = (area.width - title_len as u16) / 2;
31+
buf.set_stringn(
32+
area.x + title_start as u16,
33+
area.y,
34+
self.title,
35+
title_len,
36+
tui::style::Style::default(),
37+
);
38+
39+
// write min max values
40+
let min_str = format!("{:.2}", self.min);
41+
let max_str = format!("{:.2}", self.max);
42+
buf.set_stringn(
43+
area.x,
44+
area.y,
45+
&min_str,
46+
min_str.len(),
47+
tui::style::Style::default(),
48+
);
49+
buf.set_stringn(
50+
area.x + area.width as u16 - max_str.len() as u16,
51+
area.y,
52+
&max_str,
53+
max_str.len(),
54+
tui::style::Style::default(),
55+
);
56+
}
57+
}

src/widgets/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod key_value_list_widget;
22
pub mod slice_widget;
3-
pub mod title_bar;
3+
pub mod title_bar;
4+
pub mod color_bar;

0 commit comments

Comments
 (0)