1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: MPL-2.0
//! Colors and color pickers.
use std::ptr::addr_of_mut;
use crate::prelude::*;
impl Ui {
/// Creates a new [color picker button](Picker).
pub fn create_color_picker<'ui>(&'ui self) -> Result<&'ui mut Picker, crate::Error> {
unsafe { call_libui_new_fn!(ui: self, fn: uiNewColorButton() -> Picker) }
}
}
/// A button that opens a color picker dialog.
#[subcontrol(handle = "uiColorButton")]
pub struct Picker;
/// An RGBA color.
///
/// All components must be normalized to the `[0.0, 1.0]` range.
#[derive(Clone, Copy, Default)]
pub struct Color {
/// The red component.
pub red: f64,
/// The green component.
pub green: f64,
/// The blue component.
pub blue: f64,
/// The alpha, or transparency, component.
pub alpha: f64,
}
impl<'ui> Picker<'ui> {
/// The color currently selected by this picker.
pub fn selected_color(&self) -> Color {
// SAFETY: the *libui-ng* documentation does not describe the behavior when this function is
// called and a color has not yet been selected, and I would rather not guess and invoke UB,
// so we will default-initialize `color`.
let mut color = Color::default();
unsafe {
uiColorButtonColor(
self.as_ptr(),
addr_of_mut!(color.red),
addr_of_mut!(color.green),
addr_of_mut!(color.blue),
addr_of_mut!(color.alpha),
)
};
color
}
/// Selects the given color.
///
/// # Panics
///
/// Panics if any components of `color` are not normalized to the `[0.0, 1.0]` range. See
/// [`select_color_unchecked`] for an unchecked version of this function.
pub fn select_color(&self, color: Color) {
let check_component = |comp: f64| {
assert!((comp >= 0.) && (comp <= 1.), "component outside [0, 1] range");
};
check_component(color.red);
check_component(color.green);
check_component(color.blue);
check_component(color.alpha);
unsafe { self.select_color_unchecked(color) };
}
/// Selects the given color without first checking if it is valid.
///
/// # Safety
///
/// All components of `color` must be normalized to the `[0.0, 1.0]` range.
pub unsafe fn select_color_unchecked(&self, color: Color) {
uiColorButtonSetColor(
self.as_ptr(),
color.red,
color.green,
color.blue,
color.alpha,
);
}
/// Sets a callback for when the user selects a new color.
///
/// This callback is unset by default. This is not activated when
/// [`select_color`](Self::select_color) or
/// [`select_color_unchecked`](Self::select_color_unchecked) is called.
#[bind_callback(fn = "uiColorButtonOnChanged")]
pub fn on_selected(&self, f: fn()) {
f();
}
}