maia_wasm/ui/
colormap.rs

1//! Colormaps.
2//!
3//! This module defines the colormaps that are supported by the maia-wasm
4//! waterfall.
5
6use serde::{Deserialize, Serialize};
7
8/// Waterfall colormap.
9///
10/// This enum lists the supported waterfall colormaps.
11#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
12pub enum Colormap {
13    /// Turbo colormap.
14    Turbo,
15    /// Viridis colormap.
16    Viridis,
17    /// Inferno colormap.
18    Inferno,
19}
20
21impl Colormap {
22    /// Returns the colormap as a slice.
23    ///
24    /// The format of the slice is 8-bit RGB as a flattened array. Usually the
25    /// length of the colormap is 255 RGB pixels, since it is indexed by an
26    /// 8-bit integer, but this need not be the case.
27    pub fn colormap_as_slice(&self) -> &[u8] {
28        match self {
29            Colormap::Turbo => &crate::colormap::turbo::COLORMAP,
30            Colormap::Viridis => &crate::colormap::viridis::COLORMAP,
31            Colormap::Inferno => &crate::colormap::inferno::COLORMAP,
32        }
33    }
34}
35
36impl std::str::FromStr for Colormap {
37    type Err = ();
38
39    fn from_str(s: &str) -> Result<Colormap, ()> {
40        Ok(match s {
41            "Turbo" => Colormap::Turbo,
42            "Viridis" => Colormap::Viridis,
43            "Inferno" => Colormap::Inferno,
44            _ => return Err(()),
45        })
46    }
47}
48
49impl std::fmt::Display for Colormap {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
51        write!(
52            f,
53            "{}",
54            match self {
55                Colormap::Turbo => "Turbo",
56                Colormap::Viridis => "Viridis",
57                Colormap::Inferno => "Inferno",
58            }
59        )
60    }
61}