Skip to main content

crisol_plot/plot/
colormap.rs

1//! Perceptually uniform, diverging, and color-blind-safe colormaps.
2
3use egui::Color32;
4
5/// A color scale for heatmaps and colorbars.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
9#[non_exhaustive]
10pub enum Colormap {
11    /// The existing theme-derived heat scale.
12    #[default]
13    Theme,
14    /// Viridis (perceptually uniform, color-blind safe).
15    Viridis,
16    /// Magma.
17    Magma,
18    /// Cividis (color-blind safe).
19    Cividis,
20    /// Turbo (high contrast, not perceptually uniform).
21    Turbo,
22    /// Cool to warm, diverging around the midpoint.
23    CoolWarm,
24    /// A categorical palette, safe for the common color-vision deficiencies.
25    Colorblind,
26}
27
28impl Colormap {
29    /// Every built-in colormap.
30    pub const ALL: [Self; 7] = [
31        Self::Theme,
32        Self::Viridis,
33        Self::Magma,
34        Self::Cividis,
35        Self::Turbo,
36        Self::CoolWarm,
37        Self::Colorblind,
38    ];
39
40    /// A label.
41    pub const fn label(self) -> &'static str {
42        match self {
43            Self::Theme => "Theme",
44            Self::Viridis => "Viridis",
45            Self::Magma => "Magma",
46            Self::Cividis => "Cividis",
47            Self::Turbo => "Turbo",
48            Self::CoolWarm => "Cool-Warm",
49            Self::Colorblind => "Colorblind",
50        }
51    }
52
53    /// Sample the scale at `t` in 0..=1. `Theme` needs the active theme.
54    pub fn sample(self, t: f32, theme: &crisol_ui_kit::theme::Theme) -> Color32 {
55        let t = t.clamp(0.0, 1.0);
56        match self {
57            Self::Theme => crate::plot::extra::heat_color(t, theme),
58            Self::Viridis => gradient(t, &VIRIDIS),
59            Self::Magma => gradient(t, &MAGMA),
60            Self::Cividis => gradient(t, &CIVIDIS),
61            Self::Turbo => gradient(t, &TURBO),
62            Self::CoolWarm => gradient(t, &COOL_WARM),
63            Self::Colorblind => categorical(t),
64        }
65    }
66
67    /// The discrete colors of a categorical palette (one per index).
68    pub fn categorical(self, index: usize) -> Color32 {
69        if self == Self::Colorblind {
70            const PALETTE: [Color32; 8] = [
71                Color32::from_rgb(0x00, 0x72, 0xb2),
72                Color32::from_rgb(0xe6, 0x9f, 0x00),
73                Color32::from_rgb(0x00, 0x9e, 0x73),
74                Color32::from_rgb(0xcc, 0x79, 0xa7),
75                Color32::from_rgb(0x56, 0xb4, 0xe9),
76                Color32::from_rgb(0xd5, 0x5e, 0x00),
77                Color32::from_rgb(0xf0, 0xe4, 0x42),
78                Color32::from_rgb(0x99, 0x99, 0x99),
79            ];
80            PALETTE[index % PALETTE.len()]
81        } else {
82            self.sample((index % 20) as f32 / 19.0, &crisol_ui_kit::theme::Theme::dark())
83        }
84    }
85}
86
87fn gradient(t: f32, stops: &[[f32; 3]]) -> Color32 {
88    let position = t.clamp(0.0, 1.0) * (stops.len() - 1) as f32;
89    let low = position.floor() as usize;
90    let high = (low + 1).min(stops.len() - 1);
91    let fraction = position - low as f32;
92    let mix = |a: f32, b: f32| (a + (b - a) * fraction).round() as u8;
93    Color32::from_rgb(
94        mix(stops[low][0], stops[high][0]),
95        mix(stops[low][1], stops[high][1]),
96        mix(stops[low][2], stops[high][2]),
97    )
98}
99
100fn categorical(t: f32) -> Color32 {
101    const PALETTE: [Color32; 8] = [
102        Color32::from_rgb(0x00, 0x72, 0xb2),
103        Color32::from_rgb(0xe6, 0x9f, 0x00),
104        Color32::from_rgb(0x00, 0x9e, 0x73),
105        Color32::from_rgb(0xcc, 0x79, 0xa7),
106        Color32::from_rgb(0x56, 0xb4, 0xe9),
107        Color32::from_rgb(0xd5, 0x5e, 0x00),
108        Color32::from_rgb(0xf0, 0xe4, 0x42),
109        Color32::from_rgb(0x99, 0x99, 0x99),
110    ];
111    PALETTE[(t * (PALETTE.len() - 1) as f32).round() as usize % PALETTE.len()]
112}
113
114const VIRIDIS: [[f32; 3]; 12] = [
115    [68.0, 1.0, 84.0],
116    [72.0, 26.0, 108.0],
117    [71.0, 47.0, 125.0],
118    [65.0, 68.0, 135.0],
119    [57.0, 86.0, 140.0],
120    [49.0, 104.0, 142.0],
121    [42.0, 120.0, 142.0],
122    [35.0, 136.0, 142.0],
123    [31.0, 152.0, 139.0],
124    [34.0, 168.0, 132.0],
125    [53.0, 183.0, 121.0],
126    [84.0, 197.0, 105.0],
127];
128const MAGMA: [[f32; 3]; 11] = [
129    [0.0, 0.0, 4.0],
130    [28.0, 16.0, 68.0],
131    [57.0, 13.0, 89.0],
132    [85.0, 15.0, 109.0],
133    [116.0, 22.0, 105.0],
134    [147.0, 38.0, 103.0],
135    [178.0, 55.0, 96.0],
136    [209.0, 74.0, 86.0],
137    [235.0, 102.0, 70.0],
138    [251.0, 143.0, 60.0],
139    [252.0, 253.0, 191.0],
140];
141const CIVIDIS: [[f32; 3]; 11] = [
142    [0.0, 32.0, 76.0],
143    [0.0, 44.0, 99.0],
144    [0.0, 57.0, 115.0],
145    [24.0, 70.0, 124.0],
146    [57.0, 83.0, 126.0],
147    [80.0, 96.0, 128.0],
148    [102.0, 109.0, 130.0],
149    [123.0, 123.0, 133.0],
150    [145.0, 138.0, 135.0],
151    [168.0, 154.0, 134.0],
152    [255.0, 234.0, 70.0],
153];
154const TURBO: [[f32; 3]; 11] = [
155    [48.0, 18.0, 59.0],
156    [70.0, 62.0, 190.0],
157    [42.0, 120.0, 245.0],
158    [25.0, 181.0, 220.0],
159    [40.0, 225.0, 135.0],
160    [130.0, 245.0, 50.0],
161    [206.0, 225.0, 30.0],
162    [250.0, 171.0, 35.0],
163    [246.0, 105.0, 28.0],
164    [200.0, 48.0, 35.0],
165    [122.0, 4.0, 3.0],
166];
167const COOL_WARM: [[f32; 3]; 11] = [
168    [59.0, 76.0, 192.0],
169    [98.0, 130.0, 234.0],
170    [141.0, 176.0, 254.0],
171    [184.0, 208.0, 249.0],
172    [221.0, 221.0, 221.0],
173    [245.0, 196.0, 173.0],
174    [244.0, 154.0, 123.0],
175    [222.0, 96.0, 77.0],
176    [180.0, 4.0, 38.0],
177    [180.0, 4.0, 38.0],
178    [180.0, 4.0, 38.0],
179];
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    #[test]
186    fn samples_are_in_range_and_distinct() {
187        let theme = crisol_ui_kit::theme::Theme::dark();
188        for map in Colormap::ALL {
189            let start = map.sample(0.0, &theme);
190            let end = map.sample(1.0, &theme);
191            assert_ne!(start, end, "{}", map.label());
192            assert_eq!(map.sample(-1.0, &theme), start, "{}", map.label());
193            assert_eq!(map.sample(2.0, &theme), end, "{}", map.label());
194        }
195    }
196
197    #[test]
198    fn the_categorical_palette_wraps() {
199        for index in 0..16 {
200            let color = Colormap::Colorblind.categorical(index);
201            assert_ne!(color, Color32::TRANSPARENT);
202        }
203    }
204}