Skip to main content

ColorExt

Trait ColorExt 

Source
pub trait ColorExt {
    // Required methods
    fn blend(&self, other: &Self) -> Self
       where Self: Sized;
    fn fade_out(&mut self, factor: f32);
    fn opacity(&self, factor: f32) -> Self
       where Self: Sized;
}
Expand description

Wrapper methods to make alpha operations more convenient

Required Methods§

Source

fn blend(&self, other: &Self) -> Self
where Self: Sized,

Performs a SrcAlpha x (1 - SrcAlpha) blend

Source

fn fade_out(&mut self, factor: f32)

Fade out the color by a given factor. This factor should be between 0.0 and 1.0. Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.

Source

fn opacity(&self, factor: f32) -> Self
where Self: Sized,

Multiplies the alpha value of the color by a given factor and returns a new color. If the color was previously opaque, then this is equivalent to with_alpha.

Useful for transforming colors with dynamic opacity, like a color from an external source.

Example:

use gpui::ColorExt;
let color = gpui::red();
let faded_color = color.opacity(0.5);
assert_eq!(faded_color.alpha, 0.5);

This will return a red color with half the opacity.

Example:

use gpui::{hsla, ColorExt};
let color = hsla(0.7, 1.0, 0.5, 0.7); // A saturated blue
let faded_color = color.opacity(0.16);
assert!((faded_color.alpha - 0.112).abs() < 1e-6);

This will return a blue color with around ~10% opacity, suitable for an element’s hover or selected state.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§