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

Helpers for changing the alpha of a color.

Implemented for Rgba and Hsla. The factors in these methods are clamped to 0.0..=1.0.

Required Methods§

Source

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

Composites other over this color with Porter–Duff source-over.

Source

fn fade_out(&mut self, factor: f32)

Reduces the alpha of this color in place by factor.

A factor of 0.0 leaves the color unchanged, and 1.0 makes it fully transparent. Values outside 0.0..=1.0 are clamped.

Source

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

Returns a copy of this color with its alpha multiplied by factor.

If the color is opaque, the result has alpha factor. Use this for colors whose opacity comes from data, such as a color chosen at runtime. Values outside 0.0..=1.0 are clamped.

§Examples
use gpui::ColorExt;

let faded = gpui::red().opacity(0.5);
assert_eq!(faded.alpha, 0.5);

Multiplying a color that already has alpha gives a result that is less opaque:

use gpui::{ColorExt, hsla};

let color = hsla(0.7, 1.0, 0.5, 0.7);
let faded = color.opacity(0.16);
assert!((faded.alpha - 0.112).abs() < 1e-6);

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§