Struct Color

Source
pub struct Color<T: Storage>(/* private fields */);
Expand description

An RGBA color backed by a u32.

There are multiple storage formats to choose from, see the crate level documentation for more info.

§Examples

type RGBA = inku::Color<inku::RGBA>;
assert_eq!(RGBA::new(0xfacadeff).to_u32(), 0xfacadeff);

// 4 bytes
assert_eq!(4, std::mem::size_of::<RGBA>());

Implementations§

Source§

impl<T: Storage> Color<T>

Source

pub fn new(color: u32) -> Self

Initializes a new Color from a u32.

§Examples
type Color = inku::Color<inku::ZRGB>;
let color = Color::new(0x000000);

Using ZRGB, the u32 is treated as follows:

0x00000000
  ^^ ignored (zeroed out)
    ^^ red
      ^^ green
        ^^ blue
Source

pub fn lighten(self, percent: f64) -> Self

Lightens the color by translating to HSL color space then adjusting the lightness value.

§Panics

Panics if percent is not between 0.0 and 1.0

Source

pub fn darken(self, percent: f64) -> Self

Darkens the color by translating to HSL color space then adjusting the lightness value.

§Panics

Panics if percent is not between 0.0 and 1.0

Source

pub fn saturate(self, percent: f64) -> Self

Increases saturation of the color by translating to HSL color space then adjusting the saturation value.

§Panics

Panics if percent is not between 0.0 and 1.0

Source

pub fn desaturate(self, percent: f64) -> Self

Decreases saturation of the color by translating to HSL color space then adjusting the saturation value.

§Panics

Panics if percent is not between 0.0 and 1.0

Source

pub fn rotate_hue(self, amount: f64) -> Self

Rotate the hue by translating to HSL color space then adjusting the hue value. Takes a value between 0.0 and 360.0.

Source

pub fn to_u32(self) -> u32

Returns the underlying u32.

Source

pub fn brightness(self) -> f64

The percieved brightness of the color (a number between 0.0 and 1.0).

Source

pub fn is_light(self) -> bool

Determine whether a color is perceived as a light color (percieved brightness is greater than 0.5).

Source

pub fn map<F>(&self, f: F) -> Self
where F: Fn(u8, u8, u8, u8) -> (u8, u8, u8, u8),

Maps (r1, g1, b1, a1) to (r2, g2, b2, a2) by applying a function to the channels.

§Examples
let color = Color::<RGBA>::new(0x00000011);
assert_eq!(
    color.map(|r, g, b, a| (r, g, b, a + 0x22)).to_u32(),
    0x00000033
);
const F: fn(u8, u8, u8, u8) -> (u8, u8, u8, u8) = |r, g, b, a| {
    assert_eq!(r, 0x22);
    assert_eq!(g, 0x33);
    assert_eq!(b, 0x44);
    assert_eq!(a, 0x00);
    (1, 2, 3, 4)
};
let color = Color::<ZRGB>::new(0x11223344);
assert_eq!(color.map(F).to_u32(), 0x00010203);

Trait Implementations§

Source§

impl<T: Clone + Storage> Clone for Color<T>

Source§

fn clone(&self) -> Color<T>

Returns a duplicate 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<T: Storage> Debug for Color<T>

Source§

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

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

impl<T: Default + Storage> Default for Color<T>

Source§

fn default() -> Color<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Storage> From<(u8, u8, u8)> for Color<T>

Source§

fn from(rgb: (u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl<T: Storage> From<(u8, u8, u8, u8)> for Color<T>

Source§

fn from(rgba: (u8, u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl<T: Storage> From<Color<T>> for (u8, u8, u8)

Source§

fn from(color: Color<T>) -> (u8, u8, u8)

Converts to this type from the input type.
Source§

impl<T: Storage> From<Color<T>> for (u8, u8, u8, u8)

Source§

fn from(color: Color<T>) -> (u8, u8, u8, u8)

Converts to this type from the input type.
Source§

impl<T: Storage> From<Color<T>> for u32

Source§

fn from(color: Color<T>) -> u32

Converts to this type from the input type.
Source§

impl<T: Storage> From<u32> for Color<T>

Source§

fn from(color: u32) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Storage> Hash for Color<T>

Source§

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

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

impl<T: PartialEq + Storage> PartialEq for Color<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy + Storage> Copy for Color<T>

Source§

impl<T: Storage> StructuralPartialEq for Color<T>

Auto Trait Implementations§

§

impl<T> Freeze for Color<T>

§

impl<T> RefUnwindSafe for Color<T>
where T: RefUnwindSafe,

§

impl<T> Send for Color<T>
where T: Send,

§

impl<T> Sync for Color<T>
where T: Sync,

§

impl<T> Unpin for Color<T>
where T: Unpin,

§

impl<T> UnwindSafe for Color<T>
where T: UnwindSafe,

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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

Source§

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.