Skip to main content

ColorPickerState

Struct ColorPickerState 

Source
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: usize

Number of swatches per row (minimum 1, default 8).

§selected: usize

Flat index of the selected swatch into colors.

§mode: PickerMode

Whether the picker is in palette-grid or hex-entry mode.

§hex_input: TextInputState

Backing text field used in PickerMode::Hex.

Implementations§

Source§

impl ColorPickerState

Source

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);
Source

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);
Source

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);
Source

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

Source§

fn clone(&self) -> ColorPickerState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ColorPickerState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.