use eframe::egui;
use siplot::{Colormap, ColormapDialog, Plot2D};
const WIDTH: u32 = 128;
const HEIGHT: u32 = 96;
struct ColormapDialogApp {
image_plot: Plot2D,
colormap_dialog: ColormapDialog,
}
impl ColormapDialogApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
let rs = cc
.wgpu_render_state
.as_ref()
.expect("eframe must use the wgpu renderer");
let pixels = build_image();
let mut image_plot = Plot2D::new(rs, 0);
image_plot.set_graph_title("Interactive Colormap Editing");
let initial_cmap = Colormap::viridis(0.0, 1.0);
image_plot.set_default_colormap(initial_cmap.clone());
image_plot
.try_add_default_image(WIDTH, HEIGHT, &pixels)
.expect("image dimensions match");
let mut colormap_dialog = ColormapDialog::new().with_colormap(&initial_cmap);
colormap_dialog.open = true;
Self {
image_plot,
colormap_dialog,
}
}
}
impl eframe::App for ColormapDialogApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
ui.vertical(|ui| {
if ui.button("Toggle Colormap Dialog").clicked() {
self.colormap_dialog.open = !self.colormap_dialog.open;
}
ui.separator();
self.image_plot.show_with_toolbar(ui);
self.colormap_dialog.show(ui.ctx(), &mut self.image_plot);
});
}
}
fn build_image() -> Vec<f32> {
let mut pixels = Vec::with_capacity((WIDTH * HEIGHT) as usize);
for row in 0..HEIGHT {
for col in 0..WIDTH {
let cx = (col as f32 - WIDTH as f32 / 2.0) / (WIDTH as f32 / 4.0);
let cy = (row as f32 - HEIGHT as f32 / 2.0) / (HEIGHT as f32 / 4.0);
pixels.push((-0.5 * (cx * cx + cy * cy)).exp());
}
}
pixels
}
fn main() -> eframe::Result {
eframe::run_native(
"siplot: Colormap Dialog",
eframe::NativeOptions {
renderer: eframe::Renderer::Wgpu,
..Default::default()
},
Box::new(|cc| Ok(Box::new(ColormapDialogApp::new(cc)) as Box<dyn eframe::App>)),
)
}