dear_implot3d/
style.rs

1use crate::flags::Marker3D;
2use crate::sys;
3
4#[inline]
5pub fn style_colors_dark() {
6    unsafe { sys::ImPlot3D_StyleColorsDark(std::ptr::null_mut()) }
7}
8#[inline]
9pub fn style_colors_light() {
10    unsafe { sys::ImPlot3D_StyleColorsLight(std::ptr::null_mut()) }
11}
12#[inline]
13pub fn style_colors_classic() {
14    unsafe { sys::ImPlot3D_StyleColorsClassic(std::ptr::null_mut()) }
15}
16#[inline]
17pub fn style_colors_auto() {
18    unsafe { sys::ImPlot3D_StyleColorsAuto(std::ptr::null_mut()) }
19}
20
21#[inline]
22pub fn push_style_color(idx: i32, col: [f32; 4]) {
23    unsafe {
24        sys::ImPlot3D_PushStyleColor_Vec4(idx, crate::imvec4(col[0], col[1], col[2], col[3]));
25    }
26}
27#[inline]
28pub fn pop_style_color(count: i32) {
29    unsafe { sys::ImPlot3D_PopStyleColor(count) }
30}
31
32/// Push a style variable (float variant)
33#[inline]
34pub fn push_style_var_f32(idx: i32, val: f32) {
35    unsafe { sys::ImPlot3D_PushStyleVar_Float(idx, val) }
36}
37
38/// Push a style variable (int variant)
39#[inline]
40pub fn push_style_var_i32(idx: i32, val: i32) {
41    unsafe { sys::ImPlot3D_PushStyleVar_Int(idx, val) }
42}
43
44/// Push a style variable (Vec2 variant)
45#[inline]
46pub fn push_style_var_vec2(idx: i32, val: [f32; 2]) {
47    unsafe { sys::ImPlot3D_PushStyleVar_Vec2(idx, crate::imvec2(val[0], val[1])) }
48}
49
50/// Pop style variable(s)
51#[inline]
52pub fn pop_style_var(count: i32) {
53    unsafe { sys::ImPlot3D_PopStyleVar(count) }
54}
55
56#[inline]
57pub fn set_next_line_style(col: [f32; 4], weight: f32) {
58    unsafe { sys::ImPlot3D_SetNextLineStyle(crate::imvec4(col[0], col[1], col[2], col[3]), weight) }
59}
60
61#[inline]
62pub fn set_next_fill_style(col: [f32; 4], alpha_mod: f32) {
63    unsafe {
64        sys::ImPlot3D_SetNextFillStyle(crate::imvec4(col[0], col[1], col[2], col[3]), alpha_mod)
65    }
66}
67
68#[inline]
69pub fn set_next_marker_style(
70    marker: Marker3D,
71    size: f32,
72    fill: [f32; 4],
73    weight: f32,
74    outline: [f32; 4],
75) {
76    unsafe {
77        sys::ImPlot3D_SetNextMarkerStyle(
78            marker as i32,
79            size,
80            crate::imvec4(fill[0], fill[1], fill[2], fill[3]),
81            weight,
82            crate::imvec4(outline[0], outline[1], outline[2], outline[3]),
83        )
84    }
85}
86
87#[inline]
88pub fn push_colormap_index(cmap_index: i32) {
89    unsafe { sys::ImPlot3D_PushColormap_Plot3DColormap(cmap_index) }
90}
91#[inline]
92pub fn push_colormap_name(name: &str) {
93    dear_imgui_rs::with_scratch_txt(name, |ptr| unsafe { sys::ImPlot3D_PushColormap_Str(ptr) })
94}
95#[inline]
96pub fn pop_colormap(count: i32) {
97    unsafe { sys::ImPlot3D_PopColormap(count) }
98}
99#[inline]
100pub fn colormap_count() -> i32 {
101    unsafe { sys::ImPlot3D_GetColormapCount() }
102}
103#[inline]
104pub fn colormap_name(index: i32) -> String {
105    unsafe {
106        let p = sys::ImPlot3D_GetColormapName(index);
107        if p.is_null() {
108            return String::new();
109        }
110        std::ffi::CStr::from_ptr(p).to_string_lossy().into_owned()
111    }
112}
113
114/// Get number of keys (colors) in a given colormap index
115#[inline]
116pub fn colormap_size(index: i32) -> i32 {
117    unsafe { sys::ImPlot3D_GetColormapSize(index) }
118}
119
120/// Get current default colormap index set in ImPlot3D style
121#[inline]
122pub fn get_style_colormap_index() -> i32 {
123    unsafe {
124        let style = sys::ImPlot3D_GetStyle();
125        if style.is_null() {
126            return -1;
127        }
128        (*style).Colormap
129    }
130}
131
132/// Get current default colormap name (if index valid)
133#[inline]
134pub fn get_style_colormap_name() -> Option<String> {
135    let idx = get_style_colormap_index();
136    if idx < 0 {
137        return None;
138    }
139    let count = colormap_count();
140    if idx >= count {
141        return None;
142    }
143    Some(colormap_name(idx))
144}
145
146/// Permanently set the default colormap used by ImPlot3D (persists across plots/frames)
147#[inline]
148pub fn set_style_colormap_index(index: i32) {
149    unsafe {
150        let style = sys::ImPlot3D_GetStyle();
151        if !style.is_null() {
152            let count = sys::ImPlot3D_GetColormapCount();
153            if count > 0 {
154                let idx = if index < 0 {
155                    0
156                } else if index >= count {
157                    count - 1
158                } else {
159                    index
160                };
161                (*style).Colormap = idx;
162            }
163        }
164    }
165}
166
167/// Look up a colormap index by its name; returns -1 if not found
168#[inline]
169pub fn colormap_index_by_name(name: &str) -> i32 {
170    if name.contains('\0') {
171        return -1;
172    }
173    dear_imgui_rs::with_scratch_txt(name, |ptr| unsafe { sys::ImPlot3D_GetColormapIndex(ptr) })
174}
175
176/// Convenience: set default colormap by name (no-op if name is invalid)
177#[inline]
178pub fn set_style_colormap_by_name(name: &str) {
179    let idx = colormap_index_by_name(name);
180    if idx >= 0 {
181        set_style_colormap_index(idx);
182    }
183}
184
185/// Get a color from the current colormap at index
186pub fn get_colormap_color(idx: i32) -> [f32; 4] {
187    unsafe {
188        // Pass -1 for "current" colormap (upstream convention)
189        let out = crate::compat_ffi::ImPlot3D_GetColormapColor(idx, (-1) as sys::ImPlot3DColormap);
190        [out.x, out.y, out.z, out.w]
191    }
192}
193
194/// Get next colormap color (advances internal counter)
195pub fn next_colormap_color() -> [f32; 4] {
196    unsafe {
197        let out = crate::compat_ffi::ImPlot3D_NextColormapColor();
198        [out.x, out.y, out.z, out.w]
199    }
200}