Struct slint::Color

#[repr(C)]
pub struct Color { /* private fields */ }
Expand description

Color represents a color in the Slint run-time, represented using 8-bit channels for red, green, blue and the alpha (opacity). It can be conveniently converted using the to_ and from_ (a)rgb helper functions:

let col = some_color.to_argb_f32();
do_something_with_red_and_green(col.red, col.green);

let RgbaColor { red, blue, green, .. } = some_color.to_argb_u8();
do_something_with_red(red);

let new_col = Color::from(RgbaColor{ red: 0.5, green: 0.65, blue: 0.32, alpha: 1.});

Implementationsยง

ยง

impl Color

pub const fn from_argb_encoded(encoded: u32) -> Color

Construct a color from an integer encoded as 0xAARRGGBB

pub fn as_argb_encoded(&self) -> u32

Returns (alpha, red, green, blue) encoded as u32

pub const fn from_argb_u8(alpha: u8, red: u8, green: u8, blue: u8) -> Color

Construct a color from the alpha, red, green and blue color channel parameters.

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

Construct a color from the red, green and blue color channel parameters. The alpha channel will have the value 255.

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

Construct a color from the alpha, red, green and blue color channel parameters.

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

Construct a color from the red, green and blue color channel parameters. The alpha channel will have the value 255.

pub fn to_argb_u8(&self) -> RgbaColor<u8>

Converts this color to an RgbaColor struct for easy destructuring.

pub fn to_argb_f32(&self) -> RgbaColor<f32>

Converts this color to an RgbaColor struct for easy destructuring.

pub fn red(self) -> u8

Returns the red channel of the color as u8 in the range 0..255.

pub fn green(self) -> u8

Returns the green channel of the color as u8 in the range 0..255.

pub fn blue(self) -> u8

Returns the blue channel of the color as u8 in the range 0..255.

pub fn alpha(self) -> u8

Returns the alpha channel of the color as u8 in the range 0..255.

pub fn brighter(&self, factor: f32) -> Color

Returns a new version of this color that has the brightness increased by the specified factor. This is done by converting the color to the HSV color space and multiplying the brightness (value) with (1 + factor). The result is converted back to RGB and the alpha channel is unchanged. So for example brighter(0.2) will increase the brightness by 20%, and calling brighter(-0.5) will return a color thatโ€™s 50% darker.

pub fn darker(&self, factor: f32) -> Color

Returns a new version of this color that has the brightness decreased by the specified factor. This is done by converting the color to the HSV color space and dividing the brightness (value) by (1 + factor). The result is converted back to RGB and the alpha channel is unchanged. So for example darker(0.3) will decrease the brightness by 30%.

pub fn transparentize(&self, factor: f32) -> Color

Returns a new version of this color with the opacity decreased by factor.

The transparency is obtained by multiplying the alpha channel by (1 - factor).

Examples

Decreasing the opacity of a red color by half:

let red = Color::from_argb_u8(255, 255, 0, 0);
assert_eq!(red.transparentize(0.5), Color::from_argb_u8(128, 255, 0, 0));

Decreasing the opacity of a blue color by 20%:

let blue = Color::from_argb_u8(200, 0, 0, 255);
assert_eq!(blue.transparentize(0.2), Color::from_argb_u8(160, 0, 0, 255));

Negative values increase the opacity

let blue = Color::from_argb_u8(200, 0, 0, 255);
assert_eq!(blue.transparentize(-0.1), Color::from_argb_u8(220, 0, 0, 255));

pub fn mix(&self, other: &Color, factor: f32) -> Color

Returns a new color that is a mix of self and other, with a proportion factor given by factor (which will be clamped to be between 0.0 and 1.0).

Examples

Mix red with black half-and-half:

let red = Color::from_rgb_u8(255, 0, 0);
let black = Color::from_rgb_u8(0, 0, 0);
assert_eq!(red.mix(&black, 0.5), Color::from_rgb_u8(128, 0, 0));

Mix Purple with OrangeRed with 75%:25% ratio:

let purple = Color::from_rgb_u8(128, 0, 128);
let orange_red = Color::from_rgb_u8(255, 69, 0);
assert_eq!(purple.mix(&orange_red, 0.75), Color::from_rgb_u8(160, 17, 96));

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

Returns a new version of this color with the opacity set to alpha.

Trait Implementationsยง

ยง

impl Clone for Color

ยง

fn clone(&self) -> Color

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

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

Performs copy-assignment from source. Read more
ยง

impl Debug for Color

ยง

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

Formats the value using the given formatter. Read more
ยง

impl Default for Color

ยง

fn default() -> Color

Returns the โ€œdefault valueโ€ for a type. Read more
ยง

impl Display for Color

ยง

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

Formats the value using the given formatter. Read more
ยง

impl From<Color> for Brush

ยง

fn from(original: Color) -> Brush

Converts to this type from the input type.
ยง

impl From<Color> for PremultipliedRgbaColor

Convert a non-premultiplied color to a premultiplied one

ยง

fn from(col: Color) -> PremultipliedRgbaColor

Converts to this type from the input type.
ยง

impl From<Color> for RgbaColor<f32>

ยง

fn from(col: Color) -> RgbaColor<f32>

Converts to this type from the input type.
ยง

impl From<Color> for RgbaColor<u8>

ยง

fn from(col: Color) -> RgbaColor<u8>

Converts to this type from the input type.
ยง

impl From<RgbaColor<f32>> for Color

ยง

fn from(col: RgbaColor<f32>) -> Color

Converts to this type from the input type.
ยง

impl From<RgbaColor<u8>> for Color

ยง

fn from(col: RgbaColor<u8>) -> Color

Converts to this type from the input type.
ยง

impl InterpolatedPropertyValue for Color

ยง

fn interpolate(&self, target_value: &Color, t: f32) -> Color

Returns the interpolated value between self and target_value according to the progress parameter t thatโ€™s usually between 0 and 1. With certain animation easing curves it may over- or undershoot though.
ยง

impl PartialEq<Color> for Color

ยง

fn eq(&self, other: &Color) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 ยท sourceยง

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
ยง

impl PartialOrd<Color> for Color

ยง

fn partial_cmp(&self, other: &Color) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 ยท sourceยง

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 ยท sourceยง

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 ยท sourceยง

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 ยท sourceยง

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
ยง

impl Copy for Color

ยง

impl StructuralPartialEq for Color

Auto Trait Implementationsยง

Blanket Implementationsยง

sourceยง

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

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

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

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

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

sourceยง

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

Mutably borrows from an owned value. Read more
ยง

impl<T> Downcast for Twhere T: Any,

ยง

fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
ยง

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
ยง

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Anyโ€™s vtable from &Traitโ€™s.
ยง

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Anyโ€™s vtable from &mut Traitโ€™s.
ยง

impl<T> DowncastSync for Twhere T: Any + Send + Sync,

ยง

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

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
sourceยง

impl<T, U> Into<U> for Twhere 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.

ยง

impl<T> NoneValue for Twhere T: Default,

ยง

type NoneType = T

ยง

fn null_value() -> T

The none-equivalent value.
ยง

impl<T> Pointable for T

ยง

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
ยง

type Init = T

The type for initializers.
ยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
ยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
ยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
ยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
sourceยง

impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,

sourceยง

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
sourceยง

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
sourceยง

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
sourceยง

impl<T> Same<T> for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for Twhere T: Clone,

ยง

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> ToString for Twhere T: Display + ?Sized,

sourceยง

default fn to_string(&self) -> String

Converts the given value to a String. Read more
sourceยง

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

ยง

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 Twhere U: TryFrom<T>,

ยง

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.
ยง

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

ยง

fn vzip(self) -> V

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