Struct batbox_color::Rgba

source ·
#[repr(C)]
pub struct Rgba<T: ColorComponent> { pub r: T, pub g: T, pub b: T, pub a: T, }
Expand description

RGBA Color

Fields§

§r: T

Red component

§g: T

Green component

§b: T

Blue component

§a: T

Alpha (opacity) component

Implementations§

source§

impl<T: ColorComponent> Rgba<T>

source

pub const WHITE: Self = _

#FFF

source

pub const BLACK: Self = _

#000

source

pub const GRAY: Self = _

#7F7F7F

source

pub const TRANSPARENT_WHITE: Self = _

rgba(255, 255, 255, 0)

source

pub const TRANSPARENT_BLACK: Self = _

rgba(0, 0, 0, 0)

source

pub const RED: Self = _

#F00

source

pub const GREEN: Self = _

#0F0

source

pub const BLUE: Self = _

#00F

source

pub const CYAN: Self = _

#0FF

source

pub const MAGENTA: Self = _

#F0F

source

pub const YELLOW: Self = _

#FF0

source§

impl<T: ColorComponent> Rgba<T>

source

pub fn opaque(r: T, g: T, b: T) -> Self

Construct Rgba from red, green, and blue components, fully opaque (max alpha).

source

pub fn new(r: T, g: T, b: T, a: T) -> Self

Construct Rgba from red, green, blue, and alpha components.

source

pub fn to_vec4(self) -> vec4<T>

Convert color into vec4(r, g, b, a)

source

pub fn from_vec4(vec4: vec4<T>) -> Self

Convert vec4(r, g, b, a) into color

source

pub fn map_rgb<F: Fn(T) -> U, U: ColorComponent>(self, f: F) -> Rgba<U>

Convert Rgba<T> to Rgba<U> by applying a function to every color component excluding alpha. The resulting alpha is calculated by applying ColorComponent::convert() method.

Examples
let initial = Rgba::new(0.7, 0.4, 1.0, 1.0);
let f = |component: f32| component / 2.0;
assert_eq!(initial.map_rgb(f), Rgba::new(0.35, 0.2, 0.5, 1.0));
source

pub fn map<F: Fn(T) -> U, U: ColorComponent>(self, f: F) -> Rgba<U>

Convert Rgba<T> to Rgba<U> by applying a function to every color component.

Examples
let initial = Rgba::new(0.7, 0.4, 1.0, 1.0);
let f = |component: f32| component / 2.0;
assert_eq!(initial.map(f), Rgba::new(0.35, 0.2, 0.5, 0.5));
source

pub fn zip_map<F: Fn(T, U) -> V, U: ColorComponent, V: ColorComponent>( self, other: Rgba<U>, f: F ) -> Rgba<V>

Applies a function to every component of two colors and produces a new color.

Examples
let a = Rgba::new(0.2, 0.1, 0.3, 0.6);
let b = Rgba::new(0.5, 0.3, 0.2, 0.2);
let f = |a: f32, b: f32| a + b;
assert_eq!(a.zip_map(b, f), Rgba::new(0.7, 0.4, 0.5, 0.8));
source

pub fn convert<U: ColorComponent>(self) -> Rgba<U>

Convert Rgba<T> to Rgba<U> by applying ColorComponent::convert() method.

Examples
assert_eq!(Rgba::opaque(0, 255, 0).convert(), Rgba::opaque(0.0, 1.0, 0.0));
source

pub fn lerp(start: Self, end: Self, t: f32) -> Self

Linearly interpolate between start and end values.

Examples
let start = Rgba::opaque(0.0, 0.0, 0.0);
let end = Rgba::opaque(1.0, 1.0, 1.0);
let interpolated = Rgba::lerp(start, end, 0.3);
assert!(interpolated.r - 0.3 < 1e-5);
assert!(interpolated.g - 0.3 < 1e-5);
assert!(interpolated.b - 0.3 < 1e-5);
assert_eq!(interpolated.a, 1.0);

Methods from Deref<Target = [T; 4]>§

source

pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into a array of ASCII characters, or returns None if any of the characters is non-ASCII.

Examples
#![feature(ascii_char)]
#![feature(const_option)]

const HEX_DIGITS: [std::ascii::Char; 16] =
    *b"0123456789abcdef".as_ascii().unwrap();

assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
source

pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into a array of ASCII characters, without checking whether they’re valid.

Safety

Every byte in the array must be in 0..=127, or else this is UB.

1.57.0 · source

pub fn as_slice(&self) -> &[T]

Returns a slice containing the entire array. Equivalent to &s[..].

1.57.0 · source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice containing the entire array. Equivalent to &mut s[..].

source

pub fn each_ref(&self) -> [&T; N]

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element and returns an array of references with the same size as self.

Example
#![feature(array_methods)]

let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

#![feature(array_methods)]

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
source

pub fn each_mut(&mut self) -> [&mut T; N]

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element mutably and returns an array of mutable references with the same size as self.

Example
#![feature(array_methods)]

let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
source

pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
source

pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);

Trait Implementations§

source§

impl<T: ColorComponent + Approx> Approx for Rgba<T>

source§

fn approx_distance_to(&self, other: &Self) -> f32

Get an approximated distance between two values
source§

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

Check if values are approximately equal using DEFAULT_EPS
source§

fn approx_eq_eps(&self, other: &Self, eps: f32) -> bool

Check if values are approximately equal using supplied eps value
source§

impl<T: Clone + ColorComponent> Clone for Rgba<T>

source§

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

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<T: Debug + ColorComponent> Debug for Rgba<T>

source§

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

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

impl<T: ColorComponent> Deref for Rgba<T>

§

type Target = [T; 4]

The resulting type after dereferencing.
source§

fn deref(&self) -> &[T; 4]

Dereferences the value.
source§

impl<T: ColorComponent> DerefMut for Rgba<T>

source§

fn deref_mut(&mut self) -> &mut [T; 4]

Mutably dereferences the value.
source§

impl<'de, T> Deserialize<'de> for Rgba<T>
where T: Deserialize<'de> + ColorComponent,

source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: ColorComponent> Display for Rgba<T>

source§

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

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

impl<C1: ColorComponent, C2: ColorComponent> From<Hsla<C1>> for Rgba<C2>

source§

fn from(hsl: Hsla<C1>) -> Self

Converts to this type from the input type.
source§

impl<C1: ColorComponent, C2: ColorComponent> From<Hsva<C1>> for Rgba<C2>

source§

fn from(hsv: Hsva<C1>) -> Self

Converts to this type from the input type.
source§

impl<C1: ColorComponent, C2: ColorComponent> From<Rgba<C1>> for Hsla<C2>

source§

fn from(rgb: Rgba<C1>) -> Self

Converts to this type from the input type.
source§

impl<C1: ColorComponent, C2: ColorComponent> From<Rgba<C1>> for Hsva<C2>

source§

fn from(rgb: Rgba<C1>) -> Self

Converts to this type from the input type.
source§

impl<T: ColorComponent> From<Rgba<T>> for String

source§

fn from(color: Rgba<T>) -> String

Converts to this type from the input type.
source§

impl<T: Hash + ColorComponent> Hash for Rgba<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: ColorComponent> Mul for Rgba<T>

§

type Output = Rgba<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
source§

impl<T: PartialEq + ColorComponent> PartialEq for Rgba<T>

source§

fn eq(&self, other: &Rgba<T>) -> 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<T> Serialize for Rgba<T>

source§

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

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

impl<T: ColorComponent> TryFrom<&str> for Rgba<T>

§

type Error = Error

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

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: ColorComponent> TryFrom<String> for Rgba<T>

§

type Error = Error

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Copy + ColorComponent> Copy for Rgba<T>

source§

impl<T: Eq + ColorComponent> Eq for Rgba<T>

source§

impl<T: ColorComponent> StructuralEq for Rgba<T>

source§

impl<T: ColorComponent> StructuralPartialEq for Rgba<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Rgba<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> 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,

§

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

source§

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