Skip to main content

dear_implot3d/style/
types.rs

1use crate::sys;
2
3/// Colorable ImPlot3D style elements.
4#[repr(i32)]
5#[derive(Copy, Clone, Debug, PartialEq, Eq)]
6pub enum Plot3DColorElement {
7    TitleText = sys::ImPlot3DCol_TitleText as i32,
8    InlayText = sys::ImPlot3DCol_InlayText as i32,
9    FrameBg = sys::ImPlot3DCol_FrameBg as i32,
10    PlotBg = sys::ImPlot3DCol_PlotBg as i32,
11    PlotBorder = sys::ImPlot3DCol_PlotBorder as i32,
12    LegendBg = sys::ImPlot3DCol_LegendBg as i32,
13    LegendBorder = sys::ImPlot3DCol_LegendBorder as i32,
14    LegendText = sys::ImPlot3DCol_LegendText as i32,
15    AxisText = sys::ImPlot3DCol_AxisText as i32,
16    AxisGrid = sys::ImPlot3DCol_AxisGrid as i32,
17    AxisTick = sys::ImPlot3DCol_AxisTick as i32,
18    AxisBg = sys::ImPlot3DCol_AxisBg as i32,
19    AxisBgHovered = sys::ImPlot3DCol_AxisBgHovered as i32,
20    AxisBgActive = sys::ImPlot3DCol_AxisBgActive as i32,
21}
22
23/// A context style variable and its native value type.
24#[derive(Copy, Clone, Debug, PartialEq)]
25pub enum Plot3DStyleVar {
26    LineWeight(f32),
27    MarkerSize(f32),
28    FillAlpha(f32),
29    ViewScaleFactor(f32),
30    Marker(crate::Marker3D),
31    PlotDefaultSize([f32; 2]),
32    PlotMinSize([f32; 2]),
33    PlotPadding([f32; 2]),
34    LabelPadding([f32; 2]),
35    LegendPadding([f32; 2]),
36    LegendInnerPadding([f32; 2]),
37    LegendSpacing([f32; 2]),
38}
39
40impl Plot3DStyleVar {
41    // The caller has bound the ImPlot3D context. Each variant selects the overload named by
42    // its entry in the native GPlot3DStyleVarInfo table.
43    pub(super) unsafe fn push(self) {
44        use Plot3DStyleVar::*;
45        let scalar = |idx, value| unsafe { sys::ImPlot3D_PushStyleVar_Float(idx, value) };
46        let vec2 = |idx, [x, y]: [f32; 2]| unsafe {
47            sys::ImPlot3D_PushStyleVar_Vec2(idx, crate::imvec2(x, y))
48        };
49        match self {
50            LineWeight(v) => scalar(sys::ImPlot3DStyleVar_LineWeight as _, v),
51            MarkerSize(v) => scalar(sys::ImPlot3DStyleVar_MarkerSize as _, v),
52            FillAlpha(v) => scalar(sys::ImPlot3DStyleVar_FillAlpha as _, v),
53            ViewScaleFactor(v) => scalar(sys::ImPlot3DStyleVar_ViewScaleFactor as _, v),
54            Marker(v) => unsafe {
55                sys::ImPlot3D_PushStyleVar_Int(sys::ImPlot3DStyleVar_Marker as _, v as _)
56            },
57            PlotDefaultSize(v) => vec2(sys::ImPlot3DStyleVar_PlotDefaultSize as _, v),
58            PlotMinSize(v) => vec2(sys::ImPlot3DStyleVar_PlotMinSize as _, v),
59            PlotPadding(v) => vec2(sys::ImPlot3DStyleVar_PlotPadding as _, v),
60            LabelPadding(v) => vec2(sys::ImPlot3DStyleVar_LabelPadding as _, v),
61            LegendPadding(v) => vec2(sys::ImPlot3DStyleVar_LegendPadding as _, v),
62            LegendInnerPadding(v) => vec2(sys::ImPlot3DStyleVar_LegendInnerPadding as _, v),
63            LegendSpacing(v) => vec2(sys::ImPlot3DStyleVar_LegendSpacing as _, v),
64        }
65    }
66}
67
68/// Built-in ImPlot3D colormaps.
69#[repr(i32)]
70#[derive(Copy, Clone, Debug, PartialEq, Eq)]
71pub enum Colormap {
72    Deep = sys::ImPlot3DColormap_Deep as i32,
73    Dark = sys::ImPlot3DColormap_Dark as i32,
74    Pastel = sys::ImPlot3DColormap_Pastel as i32,
75    Paired = sys::ImPlot3DColormap_Paired as i32,
76    Viridis = sys::ImPlot3DColormap_Viridis as i32,
77    Plasma = sys::ImPlot3DColormap_Plasma as i32,
78    Hot = sys::ImPlot3DColormap_Hot as i32,
79    Cool = sys::ImPlot3DColormap_Cool as i32,
80    Pink = sys::ImPlot3DColormap_Pink as i32,
81    Jet = sys::ImPlot3DColormap_Jet as i32,
82    Twilight = sys::ImPlot3DColormap_Twilight as i32,
83    RdBu = sys::ImPlot3DColormap_RdBu as i32,
84    BrBG = sys::ImPlot3DColormap_BrBG as i32,
85    PiYG = sys::ImPlot3DColormap_PiYG as i32,
86    Spectral = sys::ImPlot3DColormap_Spectral as i32,
87    Greys = sys::ImPlot3DColormap_Greys as i32,
88}
89
90impl Colormap {
91    #[inline]
92    pub const fn index(self) -> ColormapIndex {
93        match ColormapIndex::from_raw(self as i32) {
94            Some(index) => index,
95            None => panic!("built-in ImPlot3D colormap index must be valid"),
96        }
97    }
98}
99
100/// Runtime ImPlot3D colormap index.
101#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
102#[repr(transparent)]
103pub struct ColormapIndex(i32);
104
105impl ColormapIndex {
106    #[inline]
107    pub const fn new(index: usize) -> Option<Self> {
108        if index <= i32::MAX as usize {
109            Some(Self(index as i32))
110        } else {
111            None
112        }
113    }
114
115    #[inline]
116    pub const fn get(self) -> usize {
117        self.0 as usize
118    }
119
120    #[inline]
121    pub const fn raw(self) -> i32 {
122        self.0
123    }
124
125    #[inline]
126    pub(crate) const fn from_raw(raw: i32) -> Option<Self> {
127        if raw >= 0 { Some(Self(raw)) } else { None }
128    }
129}
130
131impl From<Colormap> for ColormapIndex {
132    #[inline]
133    fn from(value: Colormap) -> Self {
134        value.index()
135    }
136}
137
138impl From<usize> for ColormapIndex {
139    #[inline]
140    fn from(value: usize) -> Self {
141        Self::new(value).expect("colormap index exceeded ImPlot3D's i32 range")
142    }
143}
144
145/// Zero-based color entry inside the active or selected colormap.
146#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
147#[repr(transparent)]
148pub struct ColormapColorIndex(i32);
149
150impl ColormapColorIndex {
151    #[inline]
152    pub const fn new(index: usize) -> Option<Self> {
153        Self::from_usize(index)
154    }
155
156    #[inline]
157    pub const fn from_usize(index: usize) -> Option<Self> {
158        if index <= i32::MAX as usize {
159            Some(Self(index as i32))
160        } else {
161            None
162        }
163    }
164
165    #[inline]
166    pub const fn get(self) -> usize {
167        self.0 as usize
168    }
169
170    #[inline]
171    pub const fn raw(self) -> i32 {
172        self.0
173    }
174}
175
176impl From<usize> for ColormapColorIndex {
177    #[inline]
178    fn from(value: usize) -> Self {
179        Self::new(value).expect("colormap color index exceeded ImPlot3D's i32 range")
180    }
181}