Skip to main content

color_scheme/
color_scheme.rs

1//! Example: Color scheme picker
2//!
3//! What it demonstrates
4//! - Using the [`ColorScheme`] enum to set a predefined visual theme.
5//! - Switching color scheme at startup via command-line argument.
6//!
7//! How to run
8//! ```bash
9//! cargo run --example color_scheme                   # default (Dark)
10//! cargo run --example color_scheme -- solarized-dark # Solarized Dark
11//! cargo run --example color_scheme -- nord           # Nord
12//! cargo run --example color_scheme -- dracula        # Dracula
13//! ```
14//!
15//! Available schemes: dark, light, solarized-dark, solarized-light, ggplot,
16//! nord, monokai, dracula, gruvbox-dark, high-contrast.
17
18use liveplot::{channel_plot, ColorScheme, LivePlotApp, PlotPoint};
19use std::time::{Duration, SystemTime, UNIX_EPOCH};
20
21fn parse_scheme(arg: &str) -> ColorScheme {
22    match arg.to_ascii_lowercase().as_str() {
23        "dark" => ColorScheme::Dark,
24        "light" => ColorScheme::Light,
25        "solarized-dark" | "solarizeddark" => ColorScheme::SolarizedDark,
26        "solarized-light" | "solarizedlight" => ColorScheme::SolarizedLight,
27        "ggplot" | "ggplot2" => ColorScheme::GgPlot,
28        "nord" => ColorScheme::Nord,
29        "monokai" => ColorScheme::Monokai,
30        "dracula" => ColorScheme::Dracula,
31        "gruvbox-dark" | "gruvboxdark" | "gruvbox" => ColorScheme::GruvboxDark,
32        "high-contrast" | "highcontrast" | "hc" => ColorScheme::HighContrast,
33        other => {
34            eprintln!(
35                "Unknown color scheme '{}', falling back to Dark.\n\
36                 Available: dark, light, solarized-dark, solarized-light, ggplot, \
37                 nord, monokai, dracula, gruvbox-dark, high-contrast",
38                other
39            );
40            ColorScheme::Dark
41        }
42    }
43}
44
45use eframe::egui::{self, ComboBox};
46
47struct ColorSchemePickerApp {
48    scheme: ColorScheme,
49    plot: LivePlotApp,
50}
51
52impl eframe::App for ColorSchemePickerApp {
53    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
54        // Apply color scheme
55        self.scheme.apply(ctx);
56
57        egui::TopBottomPanel::top("color_scheme_picker_top").show(ctx, |ui| {
58            ui.horizontal(|ui| {
59                ui.label("Color scheme:");
60                ComboBox::from_id_salt("color_scheme_picker")
61                    .selected_text(self.scheme.label())
62                    .show_ui(ui, |ui| {
63                        for scheme in ColorScheme::all() {
64                            let label = scheme.label();
65                            if ui.selectable_label(self.scheme == *scheme, label).clicked() {
66                                self.scheme = scheme.clone();
67                            }
68                        }
69                    });
70            });
71        });
72
73        egui::CentralPanel::default().show(ctx, |ui| {
74            self.plot.main_panel.update_embedded(ui);
75        });
76
77        ctx.request_repaint_after(std::time::Duration::from_millis(16));
78    }
79}
80
81fn main() -> eframe::Result<()> {
82    let scheme = std::env::args()
83        .nth(1)
84        .map(|a| parse_scheme(&a))
85        .unwrap_or(ColorScheme::Dark);
86
87    let (sink, rx) = channel_plot();
88    let tr_sine = sink.create_trace("sine", None);
89    let tr_cos = sink.create_trace("cosine", None);
90
91    // Producer: 1 kHz, 3 Hz sine + cosine
92    std::thread::spawn(move || {
93        const FS_HZ: f64 = 1000.0;
94        const F_HZ: f64 = 3.0;
95        let dt = Duration::from_millis(1);
96        let mut n: u64 = 0;
97        loop {
98            let t = n as f64 / FS_HZ;
99            let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
100            let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
101            let t_s = SystemTime::now()
102                .duration_since(UNIX_EPOCH)
103                .map(|d| d.as_secs_f64())
104                .unwrap_or(0.0);
105            let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
106            let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
107            n = n.wrapping_add(1);
108            std::thread::sleep(dt);
109        }
110    });
111
112    let plot = LivePlotApp::new(rx);
113    let app = ColorSchemePickerApp { scheme, plot };
114
115    eframe::run_native(
116        "Color Scheme Demo",
117        eframe::NativeOptions::default(),
118        Box::new(|_cc| Ok(Box::new(app))),
119    )
120}