Skip to content

Commit b382f29

Browse files
committed
🎨 (ImagePlotter): add background color support
Added a `background_color` field to `ImagePlotter` and `AppContext` to allow setting and displaying a background color. Updated the UI to include a color picker for the background color, and added functionality to clear the background color. Signed-off-by: Benign X <[email protected]>
1 parent e72609d commit b382f29

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

‎src/image_plotter.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct ImagePlotter {
1111
anti_alias: bool,
1212
show_grid: bool,
1313
show_only: bool,
14+
background_color: Color32,
1415
}
1516

1617
impl ImagePlotter {
@@ -20,6 +21,7 @@ impl ImagePlotter {
2021
anti_alias: false,
2122
show_grid: false,
2223
show_only: false,
24+
background_color: Default::default(),
2325
}
2426
}
2527

@@ -41,6 +43,12 @@ impl ImagePlotter {
4143
s
4244
}
4345

46+
pub fn background_color(self, color: Color32) -> Self {
47+
let mut s = self;
48+
s.background_color = color;
49+
s
50+
}
51+
4452
pub fn show(&mut self, ui: &mut egui::Ui, image_item: &Option<ImageItem>) {
4553
let color_data: Rc<RefCell<Option<Color32>>> = Default::default();
4654
let cursor_pos: Rc<RefCell<Option<[f64; 2]>>> = Default::default();
@@ -123,7 +131,13 @@ impl ImagePlotter {
123131
.allow_zoom(!self.show_only)
124132
.allow_drag(!self.show_only)
125133
.show_x(!self.show_only)
126-
.show_y(!self.show_only);
134+
.show_y(!self.show_only)
135+
.show_background(self.background_color.is_additive());
136+
137+
if self.background_color.a() > 0 {
138+
let painter = ui.painter();
139+
painter.rect_filled(ui.min_rect(), 0.0, self.background_color);
140+
}
127141

128142
plot.show(ui, |plot_ui| {
129143
plot_ui.image(PlotImage::new(

‎src/image_shower.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::image_plotter::ImagePlotter;
22
use eframe::egui;
33
use eframe::egui::{Color32, DroppedFile, Sense};
4+
use eframe::egui::color_picker::Alpha;
45
use icu_lib::midata::MiData;
56
use serde::{Deserialize, Serialize};
67

@@ -101,13 +102,15 @@ struct MyEguiApp {
101102
struct AppContext {
102103
show_grid: bool,
103104
anti_alias: bool,
105+
background_color: Color32,
104106
}
105107

106108
impl Default for AppContext {
107109
fn default() -> Self {
108110
Self {
109111
show_grid: true,
110112
anti_alias: true,
113+
background_color: Default::default(),
111114
}
112115
}
113116
}
@@ -139,10 +142,16 @@ impl eframe::App for MyEguiApp {
139142
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
140143
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
141144
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
145+
ui.set_height(30.0);
142146
egui::widgets::global_dark_light_mode_switch(ui);
143147
ui.separator();
144148
ui.toggle_value(&mut self.context.show_grid, "Show Grid");
145149
ui.toggle_value(&mut self.context.anti_alias, "Anti-Aliasing");
150+
ui.separator();
151+
if ui.button("Clear").clicked() {
152+
self.context.background_color = self.context.background_color.linear_multiply(0.0);
153+
}
154+
egui::widgets::color_picker::color_edit_button_srgba(ui, &mut self.context.background_color, Alpha::BlendOrAdditive);
146155
});
147156
});
148157

@@ -225,7 +234,8 @@ impl eframe::App for MyEguiApp {
225234
egui::CentralPanel::default().show(ctx, |ui| {
226235
let mut image_plotter = ImagePlotter::new("viewer")
227236
.anti_alias(self.context.anti_alias)
228-
.show_grid(self.context.show_grid);
237+
.show_grid(self.context.show_grid)
238+
.background_color(self.context.background_color);
229239

230240
image_plotter.show(ui, &self.current_image);
231241
});

0 commit comments

Comments
 (0)