Color

Struct Color 

Source
pub struct Color(/* private fields */);
Expand description

A color value that can be resolved in different color spaces.

This is the main color type that wraps a resolvable color value. Colors can be created from sRGB, P3, OKLCH, or custom color spaces.

§Layout Behavior

Color is a greedy view that expands to fill all available space in both directions. Use .frame() to constrain its size, or use it as a background.

§Examples

// Fills entire container
Color::blue()

// Constrained to specific size
Color::red().frame().width(100.0).height(50.0)

// As a background
text("Hello").background(Color::yellow())

Implementations§

Source§

impl Color

Source

pub fn new(custom: impl Resolvable<Resolved = ResolvedColor> + 'static) -> Color

Creates a new color from a custom resolvable color value.

§Arguments
  • custom - A resolvable color implementation
Source

pub fn srgb(red: u8, green: u8, blue: u8) -> Color

Creates an sRGB color from 8-bit color components.

§Arguments
  • red - Red component (0-255)
  • green - Green component (0-255)
  • blue - Blue component (0-255)
Source

pub fn srgb_f32(red: f32, green: f32, blue: f32) -> Color

Creates an sRGB color from floating-point color components.

§Arguments
  • red - Red component (0.0 to 1.0)
  • green - Green component (0.0 to 1.0)
  • blue - Blue component (0.0 to 1.0)
Source

pub fn p3(red: f32, green: f32, blue: f32) -> Color

Creates a P3 color from floating-point color components.

§Arguments
  • red - Red component (0.0 to 1.0)
  • green - Green component (0.0 to 1.0)
  • blue - Blue component (0.0 to 1.0)
Source

pub fn oklch(lightness: f32, chroma: f32, hue: f32) -> Color

Creates an OKLCH color from perceptual lightness, chroma, and hue values.

OKLCH is recommended for authoring UI colors due to its perceptual uniformity.

Source

pub fn srgb_hex(hex: &str) -> Color

Creates an sRGB color from a hexadecimal color string.

Panics if the string does not contain exactly six hexadecimal digits.

Source

pub fn try_srgb_hex(hex: &str) -> Result<Color, HexColorError>

Tries to create an sRGB color from a hexadecimal color string.

§Errors

Returns HexColorError if the provided string is not a valid six-digit hexadecimal color.

Source

pub fn srgb_u32(rgb: u32) -> Color

Creates an sRGB color from a packed 0xRRGGBB value.

Source

pub fn transparent() -> Color

Returns a fully transparent color.

Source

pub fn with_opacity(self, opacity: f32) -> Color

Creates a new color with the specified opacity applied.

§Arguments
  • opacity - Opacity value (0.0 = transparent, 1.0 = opaque)
Source

pub fn with_alpha(self, opacity: f32) -> Color

Alias for [with_opacity].

Source

pub fn with_headroom(self, headroom: f32) -> Color

Creates a new color with extended headroom for HDR content.

§Arguments
  • headroom - Additional headroom value for extended range
Source

pub fn lighten(self, amount: f32) -> Color

Lightens the color by increasing its OKLCH lightness component.

Source

pub fn darken(self, amount: f32) -> Color

Darkens the color by decreasing its OKLCH lightness component.

Source

pub fn saturate(self, amount: f32) -> Color

Adjusts the color saturation by scaling the OKLCH chroma component.

Source

pub fn desaturate(self, amount: f32) -> Color

Decreases the color saturation by scaling the OKLCH chroma component down.

Source

pub fn hue_rotate(self, degrees: f32) -> Color

Rotates the color’s hue by the provided number of degrees.

Source

pub fn mix(self, other: impl Into<Color>, factor: f32) -> Color

Mixes this color with another color using linear interpolation.

Source

pub fn resolve(&self, env: &Environment) -> Computed<ResolvedColor>

Resolves this color to a concrete color value in the given environment.

§Arguments
  • env - The environment to resolve the color in
Source§

impl Color

Source

pub fn red() -> Color

Red color.

Source§

impl Color

Source

pub fn pink() -> Color

Pink color.

Source§

impl Color

Source

pub fn purple() -> Color

Purple color.

Source§

impl Color

Source

pub fn deep_purple() -> Color

Deep purple color.

Source§

impl Color

Source

pub fn indigo() -> Color

Indigo color.

Source§

impl Color

Source

pub fn blue() -> Color

Blue color.

Source§

impl Color

Source

pub fn light_blue() -> Color

Light blue color.

Source§

impl Color

Source

pub fn cyan() -> Color

Cyan color.

Source§

impl Color

Source

pub fn teal() -> Color

Teal color.

Source§

impl Color

Source

pub fn green() -> Color

Green color.

Source§

impl Color

Source

pub fn light_green() -> Color

Light green color.

Source§

impl Color

Source

pub fn lime() -> Color

Lime color.

Source§

impl Color

Source

pub fn yellow() -> Color

Yellow color.

Source§

impl Color

Source

pub fn amber() -> Color

Amber color.

Source§

impl Color

Source

pub fn orange() -> Color

Orange color.

Source§

impl Color

Source

pub fn deep_orange() -> Color

Deep orange color.

Source§

impl Color

Source

pub fn brown() -> Color

Brown color.

Source§

impl Color

Source

pub fn grey() -> Color

Grey color.

Source§

impl Color

Source

pub fn blue_grey() -> Color

Blue grey color.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Color

Returns the “default value” for a type. Read more
Source§

impl FormBuilder for Color

Source§

type View = ColorPicker

The view type that represents this form field. Read more
Source§

fn view( binding: &Binding<Color>, label: AnyView, placeholder: Str, ) -> <Color as FormBuilder>::View

Creates a view representation of this form field bound to the given binding. Read more
Source§

fn binding() -> Binding<Self>
where Self: Default + Clone,

Creates a new binding with the default value for this form. Read more
Source§

impl From<Color> for Background

Source§

fn from(color: Color) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Color
where T: Resolvable<Resolved = ResolvedColor> + 'static,

Source§

fn from(value: T) -> Color

Converts to this type from the input type.
Source§

impl NativeView for Color

Source§

fn stretch_axis(&self) -> StretchAxis

Which axis (or axes) this view stretches to fill available space.
Source§

impl Signal for Color

Source§

type Output = Color

The type of value produced by this computation.
Source§

type Guard = ()

The guard type returned by the watch method that manages watcher lifecycle.
Source§

fn get(&self) -> <Color as Signal>::Output

Execute the computation and return the current value.
Source§

fn watch(&self, _watcher: impl Fn(Context<<Color as Signal>::Output>) + 'static)

Register a watcher to be notified when the computed value changes. Read more
Source§

impl View for Color

Source§

fn body(self, _env: &Environment) -> impl View

Build this view and return the content. Read more

Auto Trait Implementations§

§

impl Freeze for Color

§

impl !RefUnwindSafe for Color

§

impl !Send for Color

§

impl !Sync for Color

§

impl Unpin for Color

§

impl !UnwindSafe for Color

Blanket Implementations§

Source§

impl<S> AnimationExt for S
where S: SignalExt,

Source§

fn animated(self) -> WithMetadata<Self, Animation>
where Self: Sized,

Apply default animation to this reactive value Read more
Source§

fn with_animation(self, animation: Animation) -> WithMetadata<Self, Animation>
where Self: Sized,

Apply a specific animation to this reactive value Read more
§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> IdentifiableExt for T

Source§

fn use_id<F, Id>(self, f: F) -> UseId<Self, F>
where F: Fn(&Self) -> Id, Id: Ord + Hash,

Wraps the value in a UseId with the provided identification function.
Source§

fn self_id(self) -> SelfId<Self>

Wraps the value in a SelfId, making the value serve as its own identifier.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

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

§

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<C, Output> IntoSignal<Output> for C
where C: Signal, <C as Signal>::Output: 'static + Clone, Output: From<<C as Signal>::Output> + 'static,

Source§

fn into_signal(self) -> <C as IntoSignal<Output>>::Signal

Convert this computation into one that produces the desired output type.

Source§

type Signal = Map<C, fn(<C as Signal>::Output) -> Output, Output>

The specific computation type that will be produced.
Source§

impl<V> IntoView for V
where V: View,

Source§

type Output = V

The resulting View type after conversion.
Source§

fn into_view(self, _env: &Environment) -> <V as IntoView>::Output

Converts the implementing type into a View. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<C> SignalExt for C
where C: Signal,

Source§

fn map<F, Output>(self, f: F) -> Map<Self, F, Output>
where F: 'static + Clone + Fn(Self::Output) -> Output, Output: 'static, Self: 'static,

Transforms the output of this computation using the provided function. Read more
Source§

fn zip<B: Signal>(self, b: B) -> Zip<Self, B>
where Self::Output: Clone, B::Output: Clone,

Combines this computation with another computation. Read more
Source§

fn computed(self) -> Computed<Self::Output>
where Self: Clone + 'static,

Converts this computation into a Computed wrapper. Read more
Source§

fn with<T>(self, metadata: T) -> WithMetadata<Self, T>

Attaches metadata to this computation. Read more
Source§

fn animated(self) -> impl Signal<Output = Self::Output>

Marks this computation for animation with default settings. Read more
Source§

impl<C> SignalExt for C
where C: Signal,

Source§

fn map<F, Output>(self, f: F) -> Map<Self, F, Output>
where F: 'static + Clone + Fn(Self::Output) -> Output, Output: 'static, Self: 'static,

Transforms the output of this signal using the provided function.
Source§

fn zip<B>(self, b: B) -> Zip<Self, B>
where B: Signal, Self::Output: Clone, <B as Signal>::Output: Clone,

Combines this signal with another signal into a tuple.
Source§

fn cached(self) -> Cached<Self>
where Self::Output: Clone,

Wraps this signal with caching to avoid redundant computations.
Source§

fn computed(self) -> Computed<Self::Output>
where Self: 'static,

Converts this signal into a type-erased Computed container.
Source§

fn with<T>(self, metadata: T) -> WithMetadata<Self, T>

Attaches metadata to this signal’s watcher notifications.
Source§

fn debounce(self, duration: Duration) -> Debounce<Self, DefaultExecutor>
where Self::Output: Clone,

Creates a debounced version of this signal. Read more
Source§

fn throttle(self, duration: Duration) -> Throttle<Self, DefaultExecutor>
where Self::Output: Clone,

Creates a throttled version of this signal. Read more
§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<V> ViewExt for V
where V: View,

Source§

fn metadata<T: MetadataKey>(self, metadata: T) -> Metadata<T>

Attaches metadata to a view. Read more
Source§

fn with<T: 'static>(self, value: T) -> With<Self, T>

Associates a value with this view in the environment.
Source§

fn title(self, title: impl Into<Text>) -> NavigationView

Sets this view as the content of a navigation view with the specified title. Read more
Source§

fn focused<T: 'static + Eq + Clone>( self, value: &Binding<Option<T>>, equals: T, ) -> Metadata<Focused>

Marks this view as focused when the binding matches the specified value. Read more
Source§

fn on_change<C, F>(self, source: &C, handler: F) -> OnChange<Self, C::Guard>
where C: Signal, C::Output: PartialEq + Clone, F: Fn(C::Output) + 'static,

Monitors a signal for changes and triggers a handler when the signal’s value changes. Read more
Source§

fn task<Fut>(self, task: Fut) -> Metadata<Retain>
where Fut: Future<Output = ()> + 'static,

Spawns an asynchronous task tied to the lifecycle of this view. Read more
Source§

fn anyview(self) -> AnyView

Converts this view to an AnyView type-erased container.
Source§

fn background(self, background: impl Into<Background>) -> Metadata<Background>

Sets the background of this view. Read more
Source§

fn foreground( self, color: impl IntoComputed<Color>, ) -> Metadata<ForegroundColor>

Sets the foreground color of this view. Read more
Source§

fn overlay<V>(self, overlay: V) -> Overlay<Self, V>

Adds an overlay to this view. Read more
Source§

fn event<H: 'static>( self, event: Event, handler: impl HandlerFnOnce<H, ()> + 'static, ) -> Metadata<OnEvent>

Adds an event handler for the specified event. Read more
Source§

fn on_disappear<H: 'static>( self, handler: impl HandlerFnOnce<H, ()> + 'static, ) -> Metadata<OnEvent>

Adds a handler that triggers when the view disappears. Read more
Source§

fn on_appear<H: 'static>( self, handler: impl HandlerFnOnce<H, ()> + 'static, ) -> Metadata<OnEvent>

Adds a handler that triggers when the view appears. Read more
Source§

fn badge(self, value: impl IntoComputed<i32>) -> Badge

Adds a badge to this view. Read more
Source§

fn width(self, width: f32) -> Frame

Fixes this view’s width to the provided value.
Source§

fn height(self, height: f32) -> Frame

Fixes this view’s height to the provided value.
Source§

fn min_width(self, width: f32) -> Frame

Applies a minimum width constraint.
Source§

fn max_width(self, width: f32) -> Frame

Applies a maximum width constraint.
Source§

fn min_height(self, height: f32) -> Frame

Applies a minimum height constraint.
Source§

fn max_height(self, height: f32) -> Frame

Applies a maximum height constraint.
Source§

fn size(self, width: f32, height: f32) -> Frame

Fixes both width and height simultaneously.
Source§

fn min_size(self, width: f32, height: f32) -> Frame

Applies minimum constraints on both axes.
Source§

fn max_size(self, width: f32, height: f32) -> Frame

Applies maximum constraints on both axes.
Source§

fn alignment(self, alignment: Alignment) -> Frame

Aligns this view within its frame using the provided alignment.
Source§

fn padding_with(self, edge: impl Into<EdgeInsets>) -> Padding

Adds padding to this view with custom edge insets. Read more
Source§

fn padding(self) -> Padding

Adds default padding to this view. Read more
Source§

fn secure(self) -> Metadata<Secure>

Marks this view as secure. Read more
Source§

fn tag<T>(self, tag: T) -> TaggedView<T, Self>

Tags this view with a custom tag for identification. Read more
Source§

fn a11y_label( self, label: impl Into<Str>, ) -> IgnorableMetadata<AccessibilityLabel>

Sets the accessibility label for this view. Read more
Source§

fn a11y_role( self, role: AccessibilityRole, ) -> IgnorableMetadata<AccessibilityRole>

Sets the accessibility role for this view. Read more
Source§

fn gesture<P: 'static>( self, gesture: impl Into<Gesture>, action: impl HandlerFn<P, ()> + 'static, ) -> Metadata<GestureObserver>

Observes a gesture and executes an action when the gesture is recognized. Read more
Source§

fn on_tap<P: 'static>( self, action: impl HandlerFn<P, ()> + 'static, ) -> Metadata<GestureObserver>

Adds a tap gesture recognizer to this view that triggers the specified action. Read more
Source§

fn shadow(self, shadow: impl Into<Shadow>) -> Metadata<Shadow>

Applies a shadow effect to this view.
Source§

fn ignore_safe_area(self, edges: EdgeSet) -> Metadata<IgnoreSafeArea>

Extends this view’s bounds to ignore safe area insets on the specified edges. Read more
Source§

fn install(self, plugin: impl Plugin) -> impl View

Installs a plugin into the environment.
Source§

fn retain<T: 'static>(self, value: T) -> Metadata<Retain>

Retains a value for the lifetime of this view. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more