pub struct ColorPickerState {
pub colors: Vec<Color>,
pub columns: usize,
pub selected: usize,
pub mode: PickerMode,
pub hex_input: TextInputState,
}Expand description
State for an interactive color picker over the Color model.
Renders a grid of color swatches plus an optional hex-entry field. Pass a
mutable reference to Context::color_picker
each frame; read the chosen color back via selected.
Swatches are emitted with a full-RGB background; the terminal backend
downsamples each cell to the active ColorDepth on
flush (see Color::downsampled), so the picker
degrades correctly on 256-color, 16-color, and no-color terminals. Every
Color::Rgb swatch also carries a #RRGGBB label rendered with a
Color::contrast_fg foreground, so the picker
stays legible even when no background color is emitted.
§Example
let mut picker = ColorPickerState::tailwind();
let resp = ui.color_picker(&mut picker);
if resp.changed {
let chosen = picker.selected();
// persist `chosen` somewhere…
}Fields§
§colors: Vec<Color>Swatch colors laid out row-major.
columns: usizeNumber of swatches per row (minimum 1, default 8).
selected: usizeFlat index of the selected swatch into colors.
mode: PickerModeWhether the picker is in palette-grid or hex-entry mode.
hex_input: TextInputStateBacking text field used in PickerMode::Hex.
Implementations§
Source§impl ColorPickerState
impl ColorPickerState
Sourcepub fn new(colors: Vec<Color>) -> Self
pub fn new(colors: Vec<Color>) -> Self
Create a picker over the given swatches with the first one selected.
Defaults to 8 columns and PickerMode::Palette. An empty colors
vector is allowed; the widget renders nothing and reports no change.
§Example
let picker = ColorPickerState::new(vec![
Color::Rgb(239, 68, 68),
Color::Rgb(59, 130, 246),
]);
assert_eq!(picker.columns, 8);Sourcepub fn tailwind() -> Self
pub fn tailwind() -> Self
Build a picker from the Tailwind c500 shades in
crate::palette::tailwind.
Includes all 22 palettes from SLATE through ROSE, in declaration
order, giving a balanced default swatch grid.
§Example
let picker = ColorPickerState::tailwind();
assert_eq!(picker.colors.len(), 22);Sourcepub fn columns(self, n: usize) -> Self
pub fn columns(self, n: usize) -> Self
Set the number of swatches per row (clamped to at least 1).
§Example
let picker = ColorPickerState::tailwind().columns(6);
assert_eq!(picker.columns, 6);Sourcepub fn selected(&self) -> Color
pub fn selected(&self) -> Color
Return the currently selected color.
In PickerMode::Hex a successfully parsed #RRGGBB / #RGB value
takes precedence; otherwise the highlighted palette swatch is returned.
Falls back to Color::Reset when the palette is
empty and no valid hex value has been entered.
§Example
let picker = ColorPickerState::new(vec![Color::Rgb(59, 130, 246)]);
assert_eq!(picker.selected(), Color::Rgb(59, 130, 246));Trait Implementations§
Source§impl Clone for ColorPickerState
impl Clone for ColorPickerState
Source§fn clone(&self) -> ColorPickerState
fn clone(&self) -> ColorPickerState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more