Skip to main content

bhoo/systems/material/
color_picker.rs

1//! Material Hue ColorPicker and Circular SwatchPicker widgets.
2use crate::*;
3
4/// Convert a hue angle in degrees (`0.0..=360.0`) to a saturated opaque RGB `Color`.
5pub fn hue_to_color(degrees: f32) -> Color {
6    let deg = degrees.rem_euclid(360.0);
7    let h = deg / 60.0;
8    let x = 1.0 - (h % 2.0 - 1.0).abs();
9    let (r, g, b) = match h as u32 {
10        0 => (1.0, x, 0.0),
11        1 => (x, 1.0, 0.0),
12        2 => (0.0, 1.0, x),
13        3 => (0.0, x, 1.0),
14        4 => (x, 0.0, 1.0),
15        _ => (1.0, 0.0, x),
16    };
17    Color((r * 255.0) as u8, (g * 255.0) as u8, (b * 255.0) as u8, 255)
18}
19
20impl Frame<'_> {
21    /// Hue-based color picker combining a 0..=360 slider and live RGB swatch preview.
22    /// Returns the currently selected `Color`.
23    pub fn color_picker(&mut self, id: u64, label: &str, hue_degrees: &mut f32, enabled: bool) -> Color {
24        *hue_degrees = hue_degrees.clamp(0.0, 360.0);
25        self.slider(id, label, hue_degrees, 0.0..=360.0, 1.0, enabled);
26        let color = hue_to_color(*hue_degrees);
27        let swatch = self.reserve(40.0);
28        self.paint_rect(swatch, 12.0, color);
29        self.styled_text(
30            swatch,
31            &format!("#{:02X}{:02X}{:02X}", color.0, color.1, color.2),
32            Color(255, 255, 255, 255),
33            TextStyle { size: 13.0, weight: 600, align: 1, ..TextStyle::default() },
34        );
35        color
36    }
37
38    /// Row/grid of circular color swatch cards with active checkmark indicator.
39    /// Returns `true` when `*selected` changed.
40    pub fn swatch_picker(
41        &mut self,
42        id: u64,
43        swatches: &[(&str, Color)],
44        selected: &mut usize,
45        columns: usize,
46        enabled: bool,
47    ) -> bool {
48        if swatches.is_empty() {
49            return false;
50        }
51        *selected = (*selected).min(swatches.len() - 1);
52        let cols = columns.max(1);
53        let rows = swatches.len().div_ceil(cols);
54        let t = self.theme();
55        let mut changed = false;
56
57        for row in 0..rows {
58            let b = self.reserve(64.0);
59            let gap = 8.0;
60            let cell_w = ((b.width - gap * (cols.saturating_sub(1)) as f32) / cols as f32).max(0.0);
61            for col in 0..cols {
62                let idx = row * cols + col;
63                let Some(&(name, color)) = swatches.get(idx) else {
64                    break;
65                };
66                let card = Rect { x: b.x + col as f32 * (cell_w + gap), y: b.y, width: cell_w, height: b.height };
67                let is_sel = idx == *selected;
68                let r = self.target(id.wrapping_add(idx as u64), card, name, enabled, Role::Radio);
69                self.paint_rect(card, 16.0, if is_sel { t.surface_highest } else { t.surface_low });
70                self.state_layer(r, 16.0, t.text);
71                self.focus_ring(r);
72                let sz = 36.0_f32.min(card.width - 8.0).min(card.height - 8.0).max(0.0);
73                let dot = Rect {
74                    x: card.x + (card.width - sz) * 0.5,
75                    y: card.y + (card.height - sz) * 0.5,
76                    width: sz,
77                    height: sz,
78                };
79                self.paint_rect(dot, sz * 0.5, color);
80                if is_sel {
81                    let cx = dot.x + dot.width * 0.5;
82                    let cy = dot.y + dot.height * 0.5;
83                    let lum = 0.299 * color.0 as f32 + 0.587 * color.1 as f32 + 0.114 * color.2 as f32;
84                    let check = if lum > 150.0 { Color(20, 20, 24, 255) } else { Color(255, 255, 255, 255) };
85                    self.paint_line([cx - 5.0, cy], [cx - 1.5, cy + 3.5], 2.2, check);
86                    self.paint_line([cx - 1.5, cy + 3.5], [cx + 5.5, cy - 4.0], 2.2, check);
87                }
88                if r.clicked && *selected != idx {
89                    *selected = idx;
90                    changed = true;
91                }
92            }
93        }
94        changed
95    }
96}
97
98#[cfg(test)]
99#[path = "color_picker.spec.rs"]
100mod tests;