Enum druid::Color

#[non_exhaustive]
pub enum Color {
    // some variants omitted
}
Expand description

A datatype representing color.

Currently this is only a 32 bit RGBA value, but it will likely extend to some form of wide-gamut colorspace, and in the meantime is useful for giving programs proper type.

Implementations§

§

impl Color

pub const fn rgb8(r: u8, g: u8, b: u8) -> Color

Create a color from 8 bit per sample RGB values.

pub const fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color

Create a color from 8 bit per sample RGBA values.

pub const fn from_rgba32_u32(rgba: u32) -> Color

Create a color from a 32-bit rgba value (alpha as least significant byte).

pub const fn from_hex_str(hex: &str) -> Result<Color, ColorParseError>

Attempt to create a color from a CSS-style hex string.

This will accept strings in the following formats, with or without the leading #:

  • rrggbb
  • rrggbbaa
  • rbg
  • rbga

This method returns a ColorParseError if the color cannot be parsed.

pub const fn grey8(grey: u8) -> Color

Create a color from a grey value.

use piet::Color;

let grey_val = 0x55;

let one = Color::grey8(grey_val);
// is shorthand for
let two = Color::rgb8(grey_val, grey_val, grey_val);

assert_eq!(one.as_rgba_u32(), two.as_rgba_u32());

pub fn grey(grey: f64) -> Color

Create a color with a grey value in the range 0.0..=1.0.

pub fn rgba(r: f64, g: f64, b: f64, a: f64) -> Color

Create a color from four floating point values, each in the range 0.0 to 1.0.

The interpretation is the same as rgba32, and no greater precision is (currently) assumed.

pub fn rgb(r: f64, g: f64, b: f64) -> Color

Create a color from three floating point values, each in the range 0.0 to 1.0.

The interpretation is the same as rgb8, and no greater precision is (currently) assumed.

pub fn hlc(h: f64, L: f64, c: f64) -> Color

Create a color from a CIEL*a*b* polar (also known as CIE HCL) specification.

The h parameter is an angle in degrees, with 0 roughly magenta, 90 roughly yellow, 180 roughly cyan, and 270 roughly blue. The l parameter is perceptual luminance, with 0 black and 100 white. The c parameter is a chrominance concentration, with 0 grayscale and a nominal maximum of 127 (in the future, higher values might be useful, for high gamut contexts).

Currently this is just converted into sRGB, but in the future as we support high-gamut colorspaces, it can be used to specify more colors or existing colors with a higher accuracy.

Currently out-of-gamut values are clipped to the nearest sRGB color, which is perhaps not ideal (the clipping might change the hue). See https://github.com/d3/d3-color/issues/33 for discussion.

pub fn hlca(h: f64, l: f64, c: f64, a: f64) -> Color

Create a color from a CIEL*a*b* polar specification and alpha.

The a value represents alpha in the range 0.0 to 1.0.

pub fn with_alpha(self, a: f64) -> Color

Change just the alpha value of a color.

The a value represents alpha in the range 0.0 to 1.0.

pub fn as_rgba_u32(self) -> u32

Convert a color value to a 32-bit rgba value.

pub fn as_rgba8(self) -> (u8, u8, u8, u8)

Convert a color value to four 8-bit rgba values.

pub fn as_rgba(self) -> (f64, f64, f64, f64)

Convert a color value to four f64 values, each in the range 0.0 to 1.0.

pub const AQUA: Color = Color::rgb8(0, 255, 255)

Opaque aqua (or cyan).

pub const BLACK: Color = Color::rgb8(0, 0, 0)

Opaque black.

pub const BLUE: Color = Color::rgb8(0, 0, 255)

Opaque blue.

pub const FUCHSIA: Color = Color::rgb8(255, 0, 255)

Opaque fuchsia (or magenta).

pub const GRAY: Color = Color::grey8(128)

Opaque gray.

pub const GREEN: Color = Color::rgb8(0, 128, 0)

Opaque green.

pub const LIME: Color = Color::rgb8(0, 255, 0)

Opaque lime.

pub const MAROON: Color = Color::rgb8(128, 0, 0)

Opaque maroon.

pub const NAVY: Color = Color::rgb8(0, 0, 128)

Opaque navy.

pub const OLIVE: Color = Color::rgb8(128, 128, 0)

Opaque olive.

pub const PURPLE: Color = Color::rgb8(128, 0, 128)

Opaque purple.

pub const RED: Color = Color::rgb8(255, 0, 0)

Opaque red.

pub const SILVER: Color = Color::grey8(192)

Opaque silver.

pub const TEAL: Color = Color::rgb8(0, 128, 128)

Opaque teal.

pub const TRANSPARENT: Color = Color::rgba8(0, 0, 0, 0)

Fully transparent

pub const WHITE: Color = Color::grey8(255)

Opaque white.

pub const YELLOW: Color = Color::rgb8(255, 255, 0)

Opaque yellow.

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

impl Data for Color

source§

fn same(&self, other: &Self) -> bool

Determine whether two values are the same. Read more
§

impl Debug for Color

§

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

Formats the value using the given formatter. Read more
source§

impl<T> From<Color> for BackgroundBrush<T>

source§

fn from(src: Color) -> BackgroundBrush<T>

Converts to this type from the input type.
§

impl From<Color> for PaintBrush

§

fn from(src: Color) -> PaintBrush

Converts to this type from the input type.
source§

impl From<Color> for Value

source§

fn from(val: Color) -> Value

Converts to this type from the input type.
§

impl Hash for Color

§

fn hash<__H>(&self, state: &mut __H)where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

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

impl ValueType for Color

source§

fn try_from_value(value: &Value) -> Result<Self, ValueTypeError>

Attempt to convert the generic Value into this type.
§

impl Copy for Color

§

impl Eq for Color

§

impl<P> IntoBrush<P> for Colorwhere P: RenderContext,

§

impl StructuralEq 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> AnyEq for Twhere T: Any + PartialEq<T>,

source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

source§

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

source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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>,

const: unstable · 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> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
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