use std::sync::{Mutex, PoisonError};
use indicatrix_cut_core::MaterialSelection;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DialogColor {
pub bands: Option<Vec<[f32; 3]>>,
pub triple: Option<[f32; 3]>,
}
static SLOT: Mutex<DialogColor> = Mutex::new(DialogColor {
bands: None,
triple: None,
});
#[must_use]
pub fn current() -> DialogColor {
SLOT.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
pub fn set(color: DialogColor) {
*SLOT.lock().unwrap_or_else(PoisonError::into_inner) = color;
}
pub fn preset_chosen(triple: Option<[f32; 3]>) {
let mut slot = SLOT.lock().unwrap_or_else(PoisonError::into_inner);
slot.bands = None;
if triple.is_some() {
slot.triple = triple;
}
}
#[must_use]
pub fn as_selection(color: &DialogColor) -> MaterialSelection {
MaterialSelection::none().with_body_color_bands(color.triple, color.bands.clone(), None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_preset_pick_drops_the_bands_and_keep_keeps_the_triple() {
set(DialogColor {
bands: Some(vec![[460.0, 45.0, 0.25]]),
triple: Some([0.1, 0.2, 0.3]),
});
preset_chosen(None);
assert_eq!(current().bands, None);
assert_eq!(current().triple, Some([0.1, 0.2, 0.3]));
preset_chosen(Some([0.4, 0.5, 0.6]));
assert_eq!(current().triple, Some([0.4, 0.5, 0.6]));
set(DialogColor::default());
assert_eq!(current(), DialogColor::default());
}
#[test]
fn the_selection_carries_triple_and_bands() {
let color = DialogColor {
bands: Some(vec![[460.0, 45.0, 0.25]]),
triple: Some([0.1, 0.2, 0.3]),
};
let selection = as_selection(&color);
assert_eq!(selection.body_color_override, Some([0.1, 0.2, 0.3]));
assert_eq!(
selection.body_color_bands_override,
Some(vec![[460.0, 45.0, 0.25]])
);
assert_eq!(
as_selection(&DialogColor::default()),
MaterialSelection::none()
);
}
}