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

(Re-export from corelib.) 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<'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.
source§

impl From<Color> for Value

source§

fn from(c: Color) -> Self

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

impl TryFrom<Value> for Color

§

type Error = Value

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

fn try_from(v: Value) -> Result<Color, Self::Error>

Performs the conversion.
§

impl Copy for Color

§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

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 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>) -> 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 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.

§

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 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 = _

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

§

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 Twhere T: for<'de> Deserialize<'de>,