Skip to main content

dear_implot3d/style/
colormap.rs

1use crate::sys;
2use crate::{Plot3DContext, Plot3DScope, Plot3DUi};
3
4use super::types::{ColormapColorIndex, ColormapIndex};
5
6impl Plot3DUi<'_> {
7    /// Uses a registered colormap for `f`. An invalid index panics before the native push.
8    pub fn with_colormap<R>(&self, cmap: impl Into<ColormapIndex>, f: impl FnOnce() -> R) -> R {
9        let cmap = cmap.into();
10        self.with_style_scope(
11            1,
12            || {
13                assert!(
14                    cmap.raw() < unsafe { sys::ImPlot3D_GetColormapCount() },
15                    "unregistered ImPlot3D colormap"
16                );
17                unsafe { sys::ImPlot3D_PushColormap_Plot3DColormap(cmap.raw()) };
18            },
19            sys::ImPlot3D_PopColormap,
20            f,
21        )
22    }
23
24    /// Uses a registered colormap by name for `f`. Returns `None` for an unknown name.
25    pub fn with_colormap_name<R>(&self, name: &str, f: impl FnOnce() -> R) -> Option<R> {
26        let index = self.context.colormap_index_by_name(name)?;
27        Some(self.with_colormap(index, f))
28    }
29}
30
31pub(super) fn colormap_count_from_i32(raw: i32, caller: &str) -> usize {
32    assert!(raw >= 0, "{caller} returned a negative colormap count");
33    usize::try_from(raw).expect("non-negative colormap count must fit usize")
34}
35
36impl Plot3DContext {
37    /// Return the number of available ImPlot3D colormaps.
38    #[inline]
39    pub fn colormap_count(&self) -> usize {
40        self.with_bound_context(|| {
41            colormap_count_from_i32(
42                unsafe { sys::ImPlot3D_GetColormapCount() },
43                "Plot3DContext::colormap_count()",
44            )
45        })
46    }
47
48    /// Return a colormap name, or an empty string if the index is invalid for this context.
49    #[inline]
50    pub fn colormap_name(&self, index: impl Into<ColormapIndex>) -> String {
51        let index = index.into();
52        self.with_bound_context(|| unsafe {
53            let p = sys::ImPlot3D_GetColormapName(index.raw());
54            if p.is_null() {
55                return String::new();
56            }
57            std::ffi::CStr::from_ptr(p).to_string_lossy().into_owned()
58        })
59    }
60
61    /// Returns the number of color entries, or zero for an index outside this context.
62    #[inline]
63    pub fn colormap_size(&self, index: impl Into<ColormapIndex>) -> usize {
64        let index = index.into();
65        self.with_bound_context(|| {
66            if index.raw() >= unsafe { sys::ImPlot3D_GetColormapCount() } {
67                return 0;
68            }
69            colormap_count_from_i32(
70                unsafe { sys::ImPlot3D_GetColormapSize(index.raw()) },
71                "Plot3DContext::colormap_size()",
72            )
73        })
74    }
75
76    /// Return the default colormap stored in this ImPlot3D context's style.
77    #[inline]
78    pub fn style_colormap_index(&self) -> Option<ColormapIndex> {
79        self.with_bound_context(|| unsafe {
80            let style = sys::ImPlot3D_GetStyle();
81            if style.is_null() {
82                return None;
83            }
84            ColormapIndex::from_raw((*style).Colormap)
85        })
86    }
87
88    /// Return this context's default colormap name.
89    #[inline]
90    pub fn style_colormap_name(&self) -> Option<String> {
91        let idx = self.style_colormap_index()?;
92        let count = self.colormap_count();
93        if idx.get() >= count {
94            return None;
95        }
96        Some(self.colormap_name(idx))
97    }
98
99    /// Permanently set the default colormap used by this ImPlot3D context.
100    #[inline]
101    pub fn set_style_colormap(&self, index: impl Into<ColormapIndex>) {
102        let index = index.into().get();
103        self.with_bound_context(|| unsafe {
104            let style = sys::ImPlot3D_GetStyle();
105            if !style.is_null() {
106                let count = colormap_count_from_i32(
107                    sys::ImPlot3D_GetColormapCount(),
108                    "Plot3DContext::set_style_colormap()",
109                );
110                if count > 0 {
111                    let idx = index.min(count - 1);
112                    (*style).Colormap = ColormapIndex::from(idx).raw();
113                }
114            }
115        })
116    }
117
118    /// Look up a colormap index by its name.
119    #[inline]
120    pub fn colormap_index_by_name(&self, name: &str) -> Option<ColormapIndex> {
121        if name.contains('\0') {
122            return None;
123        }
124        let index = self.with_bound_context(|| {
125            dear_imgui_rs::with_scratch_txt(name, |ptr| unsafe {
126                sys::ImPlot3D_GetColormapIndex(ptr)
127            })
128        });
129        ColormapIndex::from_raw(index)
130    }
131
132    /// Permanently set the default colormap by name. Invalid names are ignored.
133    #[inline]
134    pub fn set_style_colormap_by_name(&self, name: &str) {
135        if let Some(idx) = self.colormap_index_by_name(name) {
136            self.set_style_colormap(idx);
137        }
138    }
139
140    /// Return a color from this context's active colormap.
141    pub fn colormap_color(&self, index: ColormapColorIndex) -> [f32; 4] {
142        self.with_bound_context(|| unsafe {
143            let out = sys::ImPlot3D_GetColormapColor(index.raw(), (-1) as sys::ImPlot3DColormap);
144            [out.x, out.y, out.z, out.w]
145        })
146    }
147}
148
149impl Plot3DUi<'_> {
150    /// Return a color from this UI's active ImPlot3D colormap.
151    pub fn colormap_color(&self, index: ColormapColorIndex) -> [f32; 4] {
152        self.context.colormap_color(index)
153    }
154}
155
156impl Plot3DScope<'_> {
157    /// Return the next color from this plot's current colormap and advance its color cursor.
158    pub fn next_colormap_color(&self) -> [f32; 4] {
159        self.with_bound_context(|| unsafe {
160            let out = sys::ImPlot3D_NextColormapColor();
161            [out.x, out.y, out.z, out.w]
162        })
163    }
164}