siplot 0.1.0

silx-style scientific plotting for egui, rendered with wgpu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Display of a single 2D complex dataset with selectable visualization mode.
//!
//! Ports silx `ComplexImageView.py` (and the conversion math of
//! `silx/gui/plot/items/complex.py`, `ImageComplexData`). A
//! [`ComplexImageView`] owns a [`Plot2D`], the complex data, and the current
//! [`ComplexMode`]; switching modes recomputes the displayed image in place
//! without resetting the zoom.
//!
//! Scalar modes feed a colormapped `f32` image to the plot; `AMPLITUDE_PHASE`
//! feeds an HSV-composite RGBA image. silx maps the phase through a `hsv`
//! colormap over `[-pi, pi]`, which is what the [`phase_hsv_lut`] /
//! [`hsv_to_rgb`] helpers reproduce here (the crate colormap catalog has no
//! `hsv` entry).

use egui_wgpu::RenderState;

use crate::core::backend::{ImageSpec, ItemHandle};
use crate::core::colormap::{Colormap, ColormapName};
use crate::core::plot::PlotId;
use crate::widget::high_level::{Plot2D, PlotDataError};
use crate::widget::plot_widget::PlotResponse;

/// Visualization mode for complex 2D data.
///
/// Mirrors `ImageComplexData.ComplexMode` in silx. Each scalar mode maps a
/// complex sample `(re, im)` to a single `f32` via [`ComplexMode::to_scalar`];
/// [`ComplexMode::AmplitudePhase`] instead produces an RGBA composite.
///
/// silx exposes `ABSOLUTE`, `PHASE`, `REAL`, `IMAGINARY`, `SQUARE_AMPLITUDE`,
/// `AMPLITUDE_PHASE`, and `LOG10_AMPLITUDE_PHASE`. The first six are mirrored
/// directly; `Log10Amplitude` here is the scalar `log10(|z|)` map (silx only
/// uses log10 amplitude inside its RGBA `LOG10_AMPLITUDE_PHASE` composite).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ComplexMode {
    /// `|z|` — the absolute value (`numpy.absolute`).
    Absolute,
    /// `angle(z)` — the phase in `[-pi, pi]` (`numpy.angle`).
    Phase,
    /// `re(z)` — the real part.
    Real,
    /// `im(z)` — the imaginary part.
    Imaginary,
    /// `|z|^2` — the square amplitude (`numpy.absolute(z) ** 2`).
    SquareAmplitude,
    /// `log10(|z|)` — the base-10 log of the amplitude.
    Log10Amplitude,
    /// HSV composite: hue from the phase, value from the normalized amplitude.
    AmplitudePhase,
}

impl ComplexMode {
    /// All modes in the silx menu order, for building a picker.
    pub const ALL: [ComplexMode; 7] = [
        ComplexMode::Absolute,
        ComplexMode::SquareAmplitude,
        ComplexMode::Phase,
        ComplexMode::Real,
        ComplexMode::Imaginary,
        ComplexMode::Log10Amplitude,
        ComplexMode::AmplitudePhase,
    ];

    /// Human-readable label, matching the silx menu text.
    pub fn label(self) -> &'static str {
        match self {
            ComplexMode::Absolute => "Amplitude",
            ComplexMode::SquareAmplitude => "Square amplitude",
            ComplexMode::Phase => "Phase",
            ComplexMode::Real => "Real part",
            ComplexMode::Imaginary => "Imaginary part",
            ComplexMode::Log10Amplitude => "Log10(amplitude)",
            ComplexMode::AmplitudePhase => "Amplitude and Phase",
        }
    }

    /// `true` for modes whose displayed image is an RGBA composite rather than a
    /// colormapped scalar (only [`ComplexMode::AmplitudePhase`]).
    pub fn is_rgba(self) -> bool {
        matches!(self, ComplexMode::AmplitudePhase)
    }

    /// Convert a complex sample `(re, im)` to the scalar shown by this mode.
    ///
    /// Faithful to silx `ImageComplexData.__convertComplexData`:
    /// - `Absolute`        → `hypot(re, im)` = `numpy.absolute`
    /// - `Phase`           → `atan2(im, re)` = `numpy.angle`
    /// - `Real`            → `re`
    /// - `Imaginary`       → `im`
    /// - `SquareAmplitude` → `re^2 + im^2` = `numpy.absolute(z) ** 2`
    /// - `Log10Amplitude`  → `log10(hypot(re, im))`
    ///
    /// Returns `0.0` for [`ComplexMode::AmplitudePhase`], which has no scalar
    /// representation (use [`amplitude_phase_rgba`] instead).
    pub fn to_scalar(self, re: f32, im: f32) -> f32 {
        match self {
            ComplexMode::Absolute => re.hypot(im),
            ComplexMode::Phase => im.atan2(re),
            ComplexMode::Real => re,
            ComplexMode::Imaginary => im,
            ComplexMode::SquareAmplitude => re * re + im * im,
            ComplexMode::Log10Amplitude => re.hypot(im).log10(),
            ComplexMode::AmplitudePhase => 0.0,
        }
    }
}

/// Map an HSV triple to sRGB bytes (saturation and value in `[0, 1]`, hue
/// wrapped into `[0, 1)`).
///
/// This is the standard HSV→RGB conversion silx's `hsv` colormap performs;
/// at `s == 1`, `v == 1` it sweeps the full hue circle (red → yellow → green →
/// cyan → blue → magenta → red), which is how silx color-codes the phase.
pub fn hsv_to_rgb(h: f32, s: f32, v: f32) -> [u8; 3] {
    let h = h.rem_euclid(1.0) * 6.0;
    let sector = h.floor();
    let f = h - sector;
    let p = v * (1.0 - s);
    let q = v * (1.0 - s * f);
    let t = v * (1.0 - s * (1.0 - f));
    let (r, g, b) = match sector as i32 % 6 {
        0 => (v, t, p),
        1 => (q, v, p),
        2 => (p, v, t),
        3 => (p, q, v),
        4 => (t, p, v),
        _ => (v, p, q),
    };
    [
        (r * 255.0).round().clamp(0.0, 255.0) as u8,
        (g * 255.0).round().clamp(0.0, 255.0) as u8,
        (b * 255.0).round().clamp(0.0, 255.0) as u8,
    ]
}

/// Build the 256-entry `hsv` LUT silx uses for the phase colormap: hue swept
/// linearly across `[0, 1]` at full saturation and value.
pub fn phase_hsv_lut() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    for (i, entry) in lut.iter_mut().enumerate() {
        let [r, g, b] = hsv_to_rgb(i as f32 / 255.0, 1.0, 1.0);
        *entry = [r, g, b, 255];
    }
    lut
}

/// The silx phase colormap: an `hsv` LUT over the fixed range `[-pi, pi]`.
///
/// Used by [`ComplexMode::Phase`] (and the phase channel of the composite),
/// matching silx `Colormap(name="hsv", vmin=-numpy.pi, vmax=numpy.pi)`.
pub fn phase_colormap() -> Colormap {
    // Build through the constructor so colormap fields (gamma, nan_color, and
    // any added later) take their defaults, then install the bespoke phase hsv
    // LUT over the fixed [-pi, pi] range.
    Colormap {
        lut: phase_hsv_lut(),
        ..Colormap::new(
            ColormapName::Hsv,
            -std::f64::consts::PI,
            std::f64::consts::PI,
        )
    }
}

/// Build the `AMPLITUDE_PHASE` RGBA composite for row-major complex `data`.
///
/// Hue is the phase `atan2(im, re)` mapped from `[-pi, pi]` to `[0, 1]`,
/// saturation is `1`, and value is the amplitude `hypot(re, im)` normalized to
/// its maximum over the whole array (matching the task's HSV mapping; silx's
/// `_complex2rgbalin` puts the same normalized amplitude in the alpha channel).
/// Alpha is fully opaque. When the data is empty or the max amplitude is `0`,
/// all values are taken as `0`.
pub fn amplitude_phase_rgba(data: &[(f32, f32)]) -> Vec<[u8; 4]> {
    let max_amp = data
        .iter()
        .map(|&(re, im)| re.hypot(im))
        .fold(0.0f32, f32::max);

    data.iter()
        .map(|&(re, im)| {
            let phase = im.atan2(re);
            // Map [-pi, pi] -> [0, 1]; +pi and -pi share the same hue (red).
            let hue = (phase + std::f32::consts::PI) / (2.0 * std::f32::consts::PI);
            let value = if max_amp > 0.0 {
                re.hypot(im) / max_amp
            } else {
                0.0
            };
            let [r, g, b] = hsv_to_rgb(hue, 1.0, value);
            [r, g, b, 255]
        })
        .collect()
}

/// Compute the `[min, max]` of `values` over finite entries, returning
/// `(0.0, 1.0)` when there is no finite value (degenerate range fallback that
/// the colormap maps to its low color).
fn finite_range(values: &[f32]) -> (f64, f64) {
    let mut min = f64::INFINITY;
    let mut max = f64::NEG_INFINITY;
    for &v in values {
        if v.is_finite() {
            let v = v as f64;
            if v < min {
                min = v;
            }
            if v > max {
                max = v;
            }
        }
    }
    if min.is_finite() && max.is_finite() && max > min {
        (min, max)
    } else {
        (0.0, 1.0)
    }
}

/// Render a horizontal toolbar of selectable mode buttons (one per
/// [`ComplexMode`] in silx menu order) and return the mode the user picked this
/// frame, or `None` if no button was clicked.
///
/// Pure over an [`egui::Ui`] and the `current` mode (no GPU / [`Plot2D`]), so
/// the toolbar's selection behaviour is unit-testable with a headless egui
/// context. [`ComplexImageView::show_mode_toolbar`] applies the returned mode
/// via [`ComplexImageView::set_mode`].
pub fn mode_toolbar_ui(ui: &mut egui::Ui, current: ComplexMode) -> Option<ComplexMode> {
    let mut picked = None;
    ui.horizontal(|ui| {
        for mode in ComplexMode::ALL {
            if ui.selectable_label(current == mode, mode.label()).clicked() && current != mode {
                picked = Some(mode);
            }
        }
    });
    picked
}

/// Display an image of complex data and let the user choose the visualization.
///
/// Mirrors silx `ComplexImageView`: it owns a [`Plot2D`], the complex data
/// (`(re, im)` pairs) with its width/height, and the current [`ComplexMode`].
///
/// ```ignore
/// let mut view = ComplexImageView::new(render_state, 0);
/// view.set_data(w, h, &samples)?;
///
/// // frame loop
/// view.show_mode_controls(ui);
/// view.show(ui);
/// ```
pub struct ComplexImageView {
    plot: Plot2D,
    width: u32,
    height: u32,
    data: Vec<(f32, f32)>,
    mode: ComplexMode,
    image_handle: Option<ItemHandle>,
    dirty: bool,
}

impl ComplexImageView {
    /// Create a complex image view backed by wgpu plot id `id`.
    ///
    /// The default mode is [`ComplexMode::Absolute`], matching silx's
    /// `ImageComplexData` default.
    pub fn new(render_state: &RenderState, id: PlotId) -> Self {
        Self {
            plot: Plot2D::new(render_state, id),
            width: 0,
            height: 0,
            data: Vec::new(),
            mode: ComplexMode::Absolute,
            image_handle: None,
            dirty: false,
        }
    }

    /// Set the complex data to display.
    ///
    /// `data` is a row-major array of `(re, im)` pairs of length
    /// `width * height`. Returns [`PlotDataError`] on a length mismatch.
    pub fn set_data(
        &mut self,
        width: u32,
        height: u32,
        data: &[(f32, f32)],
    ) -> Result<(), PlotDataError> {
        let expected = (width as usize).saturating_mul(height as usize);
        if data.len() != expected {
            return Err(PlotDataError::ImageDataLength {
                expected,
                actual: data.len(),
            });
        }
        self.width = width;
        self.height = height;
        self.data = data.to_vec();
        self.dirty = true;
        Ok(())
    }

    /// The current visualization mode.
    pub fn mode(&self) -> ComplexMode {
        self.mode
    }

    /// Set the visualization mode, recomputing the displayed image on the next
    /// [`Self::show`] without resetting the zoom.
    pub fn set_mode(&mut self, mode: ComplexMode) {
        if mode != self.mode {
            self.mode = mode;
            self.dirty = true;
        }
    }

    /// Access the underlying [`Plot2D`].
    pub fn plot(&self) -> &Plot2D {
        &self.plot
    }

    /// Mutably access the underlying [`Plot2D`].
    pub fn plot_mut(&mut self) -> &mut Plot2D {
        &mut self.plot
    }

    /// Render the complex image in `ui`, rebuilding the displayed image first
    /// if the data or mode changed.
    pub fn show(&mut self, ui: &mut egui::Ui) -> PlotResponse {
        if self.dirty && !self.data.is_empty() {
            self.rebuild_image();
            self.dirty = false;
        }
        self.plot.show(ui)
    }

    /// A combo box to pick the visualization mode. Returns the current mode.
    ///
    /// Call before [`Self::show`].
    pub fn show_mode_controls(&mut self, ui: &mut egui::Ui) -> ComplexMode {
        egui::ComboBox::from_label("Complex mode")
            .selected_text(self.mode.label())
            .show_ui(ui, |ui| {
                for mode in ComplexMode::ALL {
                    if ui
                        .selectable_label(self.mode == mode, mode.label())
                        .clicked()
                        && self.mode != mode
                    {
                        self.mode = mode;
                        self.dirty = true;
                    }
                }
            });
        self.mode
    }

    /// A horizontal toolbar of selectable mode buttons (one per
    /// [`ComplexMode`], in silx menu order), mirroring silx's
    /// `_ComplexDataToolButton` mode menu more closely than the combo. Clicking
    /// a button activates that mode and recomputes the image on the next
    /// [`Self::show`]. Returns the current mode.
    ///
    /// Call before [`Self::show`].
    pub fn show_mode_toolbar(&mut self, ui: &mut egui::Ui) -> ComplexMode {
        if let Some(picked) = mode_toolbar_ui(ui, self.mode) {
            self.set_mode(picked);
        }
        self.mode
    }

    /// Recompute the displayed image for the current mode and update the plot
    /// in place (reusing the existing item handle so the zoom is preserved).
    fn rebuild_image(&mut self) {
        if self.mode.is_rgba() {
            let rgba = amplitude_phase_rgba(&self.data);
            self.set_rgba_image(&rgba);
        } else {
            let scalar: Vec<f32> = self
                .data
                .iter()
                .map(|&(re, im)| self.mode.to_scalar(re, im))
                .collect();
            let colormap = self.scalar_colormap(&scalar);
            self.set_scalar_image(&scalar, colormap);
        }
    }

    /// The colormap for a scalar mode: the fixed `hsv` phase colormap for
    /// [`ComplexMode::Phase`], or an autoscaled viridis for every other scalar
    /// mode (silx: phase uses the fixed `[-pi, pi]` hsv colormap, the rest use
    /// the autoscaling default colormap).
    fn scalar_colormap(&self, scalar: &[f32]) -> Colormap {
        if self.mode == ComplexMode::Phase {
            phase_colormap()
        } else {
            let (vmin, vmax) = finite_range(scalar);
            Colormap::viridis(vmin, vmax)
        }
    }

    /// Upload or replace the scalar image, preserving the zoom on update.
    fn set_scalar_image(&mut self, scalar: &[f32], colormap: Colormap) {
        if let Some(handle) = self.image_handle {
            let spec = ImageSpec::scalar(self.width, self.height, scalar, colormap.clone());
            if self.plot.update_image_spec(handle, spec) {
                return;
            }
        }
        let handle = self
            .plot
            .try_add_image(self.width, self.height, scalar, colormap)
            .expect("scalar length validated by set_data");
        self.image_handle = Some(handle);
    }

    /// Upload or replace the RGBA composite image, preserving the zoom on
    /// update.
    fn set_rgba_image(&mut self, rgba: &[[u8; 4]]) {
        if let Some(handle) = self.image_handle {
            let spec = ImageSpec::rgba(self.width, self.height, rgba);
            if self.plot.update_image_spec(handle, spec) {
                return;
            }
        }
        let handle = self
            .plot
            .try_add_rgba_image(self.width, self.height, rgba)
            .expect("rgba length validated by set_data");
        self.image_handle = Some(handle);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const PI: f32 = std::f32::consts::PI;

    // ── Mode toolbar selection (Item 3) ─────────────────────────────────────

    /// Capture the on-screen rect of each toolbar button by running one
    /// headless layout frame with the same widget sequence `mode_toolbar_ui`
    /// emits. Geometry is deterministic, so the rects line up with the real
    /// `mode_toolbar_ui` call on an identically-sized frame.
    fn capture_button_rects(
        ctx: &egui::Context,
        current: ComplexMode,
    ) -> Vec<(ComplexMode, egui::Rect)> {
        let mut rects = Vec::new();
        let _ = ctx.run_ui(egui::RawInput::default(), |ui| {
            ui.horizontal(|ui| {
                for mode in ComplexMode::ALL {
                    let r = ui.selectable_label(current == mode, mode.label());
                    rects.push((mode, r.rect));
                }
            });
        });
        rects
    }

    /// Run one headless frame of the real `mode_toolbar_ui` with `raw` input,
    /// returning the mode it reports as picked (if any).
    fn run_toolbar(
        ctx: &egui::Context,
        current: ComplexMode,
        raw: egui::RawInput,
    ) -> Option<ComplexMode> {
        let mut picked = None;
        let _ = ctx.run_ui(raw, |ui| {
            picked = mode_toolbar_ui(ui, current);
        });
        picked
    }

    fn click_at(point: egui::Pos2) -> egui::RawInput {
        egui::RawInput {
            events: vec![
                egui::Event::PointerMoved(point),
                egui::Event::PointerButton {
                    pos: point,
                    button: egui::PointerButton::Primary,
                    pressed: true,
                    modifiers: egui::Modifiers::default(),
                },
                egui::Event::PointerButton {
                    pos: point,
                    button: egui::PointerButton::Primary,
                    pressed: false,
                    modifiers: egui::Modifiers::default(),
                },
            ],
            ..Default::default()
        }
    }

    #[test]
    fn mode_toolbar_returns_none_without_a_click() {
        let ctx = egui::Context::default();
        // Layout frame, then an empty frame: no pointer input -> no selection.
        let _ = run_toolbar(&ctx, ComplexMode::Absolute, egui::RawInput::default());
        assert_eq!(
            run_toolbar(&ctx, ComplexMode::Absolute, egui::RawInput::default()),
            None
        );
    }

    #[test]
    fn mode_toolbar_click_selects_that_mode() {
        let ctx = egui::Context::default();
        // Current is Phase, so clicking the (non-active) Real button selects it.
        let current = ComplexMode::Phase;
        let rects = capture_button_rects(&ctx, current);
        let (_, real_rect) = rects
            .iter()
            .find(|(m, _)| *m == ComplexMode::Real)
            .copied()
            .expect("Real button present");

        // Frame 1: lay the toolbar out so widget ids/rects are registered.
        let _ = run_toolbar(&ctx, current, egui::RawInput::default());
        // Frame 2: click the captured Real-button center.
        let picked = run_toolbar(&ctx, current, click_at(real_rect.center()));
        assert_eq!(picked, Some(ComplexMode::Real));
    }

    #[test]
    fn mode_toolbar_click_on_active_mode_is_noop() {
        let ctx = egui::Context::default();
        let current = ComplexMode::Absolute;
        let rects = capture_button_rects(&ctx, current);
        let (_, active_rect) = rects
            .iter()
            .find(|(m, _)| *m == ComplexMode::Absolute)
            .copied()
            .expect("Absolute button present");

        let _ = run_toolbar(&ctx, current, egui::RawInput::default());
        // Clicking the already-active button reports no change.
        let picked = run_toolbar(&ctx, current, click_at(active_rect.center()));
        assert_eq!(picked, None);
    }

    // ── Per-mode scalar conversion at known complex values ──────────────────

    #[test]
    fn absolute_is_hypot() {
        assert_eq!(ComplexMode::Absolute.to_scalar(3.0, 4.0), 5.0);
        assert_eq!(ComplexMode::Absolute.to_scalar(0.0, 0.0), 0.0);
    }

    #[test]
    fn phase_is_atan2() {
        // atan2(im, re): real axis -> 0, +imag axis -> +pi/2, -real -> +pi.
        assert_eq!(ComplexMode::Phase.to_scalar(1.0, 0.0), 0.0);
        assert!((ComplexMode::Phase.to_scalar(0.0, 1.0) - PI / 2.0).abs() < 1e-6);
        assert!((ComplexMode::Phase.to_scalar(-1.0, 0.0) - PI).abs() < 1e-6);
        assert!((ComplexMode::Phase.to_scalar(0.0, -1.0) + PI / 2.0).abs() < 1e-6);
    }

    #[test]
    fn real_is_re() {
        assert_eq!(ComplexMode::Real.to_scalar(3.0, 4.0), 3.0);
        assert_eq!(ComplexMode::Real.to_scalar(-2.5, 9.0), -2.5);
    }

    #[test]
    fn imaginary_is_im() {
        assert_eq!(ComplexMode::Imaginary.to_scalar(3.0, 4.0), 4.0);
        assert_eq!(ComplexMode::Imaginary.to_scalar(-2.5, -9.0), -9.0);
    }

    #[test]
    fn square_amplitude_is_re2_plus_im2() {
        assert_eq!(ComplexMode::SquareAmplitude.to_scalar(3.0, 4.0), 25.0);
        assert_eq!(ComplexMode::SquareAmplitude.to_scalar(0.0, 0.0), 0.0);
    }

    #[test]
    fn log10_amplitude_is_log10_of_hypot() {
        // |z| = 100 -> log10 = 2; |z| = 1 -> 0.
        assert!((ComplexMode::Log10Amplitude.to_scalar(100.0, 0.0) - 2.0).abs() < 1e-6);
        assert_eq!(ComplexMode::Log10Amplitude.to_scalar(1.0, 0.0), 0.0);
    }

    #[test]
    fn amplitude_phase_returns_zero_scalar() {
        // No scalar representation; the RGBA path is used instead.
        assert_eq!(ComplexMode::AmplitudePhase.to_scalar(3.0, 4.0), 0.0);
    }

    // ── AMPLITUDE_PHASE HSV mapping at boundary phases ──────────────────────

    #[test]
    fn amplitude_phase_hue_at_zero_phase_is_cyan() {
        // phase 0 -> hue 0.5 (mid of [-pi,pi]->[0,1]) -> cyan; |z| = max -> v = 1.
        // Use a single sample so it is the max amplitude (value = 1).
        let rgba = amplitude_phase_rgba(&[(1.0, 0.0)]);
        // hue = (0 + pi)/(2pi) = 0.5 -> hsv(0.5,1,1) = cyan (0,255,255).
        assert_eq!(rgba, vec![[0, 255, 255, 255]]);
    }

    #[test]
    fn amplitude_phase_hue_at_plus_half_pi() {
        // phase +pi/2 -> hue (pi/2 + pi)/(2pi) = 0.75 -> hsv(0.75,1,1):
        // h*6 = 4.5 (sector 4, f = 0.5) -> (t, p, v) = (0.5, 0, 1) -> violet.
        let rgba = amplitude_phase_rgba(&[(0.0, 1.0)]);
        assert_eq!(rgba, vec![[128, 0, 255, 255]]);
    }

    #[test]
    fn amplitude_phase_hue_at_minus_pi() {
        // phase -pi -> hue 0.0 -> hsv(0,1,1) = red (255,0,0).
        let rgba = amplitude_phase_rgba(&[(-1.0, 0.0)]);
        // atan2(0, -1) = +pi in IEEE, but -0.0 imaginary gives -pi; use exact -pi sample.
        let rgba_neg = amplitude_phase_rgba(&[(-1.0, -0.0)]);
        // +pi maps to hue 1.0 == 0.0 (red) and -pi maps to hue 0.0 (red): both red.
        assert_eq!(rgba, vec![[255, 0, 0, 255]]);
        assert_eq!(rgba_neg, vec![[255, 0, 0, 255]]);
    }

    #[test]
    fn amplitude_phase_value_scales_with_amplitude() {
        // Two samples: max amplitude 2 at phase 0 (cyan, v=1); half amplitude 1
        // at phase 0 (v=0.5 -> half-bright cyan).
        let rgba = amplitude_phase_rgba(&[(2.0, 0.0), (1.0, 0.0)]);
        assert_eq!(rgba[0], [0, 255, 255, 255]);
        // v = 0.5: hsv(0.5, 1, 0.5) -> (0, 128, 128) after rounding.
        assert_eq!(rgba[1], [0, 128, 128, 255]);
    }

    #[test]
    fn amplitude_phase_empty_is_empty() {
        assert!(amplitude_phase_rgba(&[]).is_empty());
    }

    #[test]
    fn amplitude_phase_zero_amplitude_is_black() {
        // max_amp == 0 -> value 0 everywhere -> black.
        let rgba = amplitude_phase_rgba(&[(0.0, 0.0), (0.0, 0.0)]);
        assert_eq!(rgba, vec![[0, 0, 0, 255], [0, 0, 0, 255]]);
    }

    // ── hsv LUT / phase colormap ────────────────────────────────────────────

    #[test]
    fn hsv_lut_endpoints_are_red() {
        // Full hue sweep is cyclic: index 0 and the wrapped end are both red.
        let lut = phase_hsv_lut();
        assert_eq!(lut[0], [255, 0, 0, 255]);
        assert_eq!(hsv_to_rgb(1.0, 1.0, 1.0), [255, 0, 0]);
    }

    #[test]
    fn phase_colormap_range_is_minus_pi_to_pi() {
        let cm = phase_colormap();
        assert_eq!(cm.vmin, -std::f64::consts::PI);
        assert_eq!(cm.vmax, std::f64::consts::PI);
    }

    // ── finite_range boundaries ─────────────────────────────────────────────

    #[test]
    fn finite_range_ignores_non_finite_and_falls_back() {
        assert_eq!(finite_range(&[1.0, 5.0, 3.0]), (1.0, 5.0));
        // All-equal -> degenerate -> fallback (0, 1).
        assert_eq!(finite_range(&[2.0, 2.0]), (0.0, 1.0));
        // NaN/inf are skipped; remaining single finite value is degenerate.
        assert_eq!(finite_range(&[f32::NAN, f32::INFINITY, 4.0]), (0.0, 1.0));
        // No finite values -> fallback.
        assert_eq!(finite_range(&[f32::NAN]), (0.0, 1.0));
    }
}