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 to_hsva(&self) -> HsvaColor

Converts this color to the HSV color space.

pub fn from_hsva(hue: f32, saturation: f32, value: f32, alpha: f32) -> Color

Construct a color from the hue, saturation, and value HSV color space parameters.

Hue is between 0 and 360, the others parameters between 0 and 1.

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 this color and other. The specified factor is clamped to be between 0.0 and 1.0 and then applied to this color, while 1.0 - factor is applied to other.

§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% purpe and 25% orange red 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<'de> Deserialize<'de> for Color

§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Color, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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 HsvaColor

§

fn from(value: Color) -> HsvaColor

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<HsvaColor> for Color

§

fn from(value: HsvaColor) -> Color

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 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 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 Serialize for Color

§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl Copy for Color

§

impl StructuralPartialEq for Color

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<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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T> Downcast for T
where T: Any,

§

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

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 T
where 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.

§

impl<T> Instrument for T

§

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

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

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 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

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 P
where 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 for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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
§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

§

fn to_smolstr(&self) -> SmolStr

source§

impl<T> ToString for T
where 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 T
where 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 T
where 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 T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,