pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: Option<u8>,
}
Expand description
RGB color type with optional alpha channel
Fields§
§r: u8
§g: u8
§b: u8
§a: Option<u8>
Implementations§
Source§impl Color
impl Color
Sourcepub const fn rgb(r: u8, g: u8, b: u8) -> Color
pub const fn rgb(r: u8, g: u8, b: u8) -> Color
Constructs a new Color
without using an alpha channel.
§Examples
Basic usage:
use pixelflut::Color;
Color::rgb(255, 255, 255);
Sourcepub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color
Constructs a new Color
using an alpha channel.
§Examples
Basic usage:
use pixelflut::Color;
Color::rgba(255, 255, 255, 255);
Sourcepub const fn alpha(self) -> u8
pub const fn alpha(self) -> u8
Returns the alpha channel of the Color
.
If the color does not have an alpha channel, 255
will be returned.
§Example
use pixelflut::Color;
let without_alpha = Color::rgb(123, 123, 123);
assert_eq!(without_alpha.alpha(), 255);
let with_alpha = Color::rgba(123, 123, 123, 123);
assert_eq!(with_alpha.alpha(), 123)
Sourcepub const fn packed(r: u8, g: u8, b: u8, a: u8) -> Color
pub const fn packed(r: u8, g: u8, b: u8, a: u8) -> Color
Constructs a new Color
using the alpha channel only, if a != 255.
§Examples
Basic usage:
use pixelflut::Color;
assert_eq!(Color::packed(123, 23, 42, 255), Color::rgb(123, 23, 42));
assert_eq!(Color::packed(123, 23, 42, 64), Color::rgba(123, 23, 42, 64));
Sourcepub const fn pack(&self) -> Color
pub const fn pack(&self) -> Color
Strips the alpha channel if not existent or value is 255.Color
§Examples
Basic usage:
use pixelflut::Color;
assert_eq!(Color::rgba(12, 34, 56, 255).pack(), Color::rgb(12, 34, 56));
assert_eq!(Color::rgb(12, 34, 56).pack(), Color::rgb(12, 34, 56));
assert_eq!(Color::rgba(12, 34, 56, 78).pack(), Color::rgba(12, 34, 56, 78));
Trait Implementations§
Source§impl FromStr for Color
impl FromStr for Color
Source§fn from_str(s: &str) -> PixelflutResult<Color>
fn from_str(s: &str) -> PixelflutResult<Color>
Converts a string to a color
§Examples
use pixelflut::Color;
assert_eq!(Color::rgb(0x11, 0x22, 0x33), "112233".parse().unwrap());
assert_eq!(Color::rgba(0x11, 0x22, 0x33, 0xee), "112233ee".parse().unwrap());
assert!("".parse::<Color>().is_err());
assert!("123".parse::<Color>().is_err());
assert!("12345".parse::<Color>().is_err());
assert!(" 1 2 3".parse::<Color>().is_err());
assert!("112g33".parse::<Color>().is_err());
Source§type Err = PixelflutError
type Err = PixelflutError
The associated error which can be returned from parsing.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more