[−][src]Struct raster::Color
A struct for representing and creating color.
Fields
r: u8
Red channel 0 - 255
g: u8
Green channel 0 - 255
b: u8
Blue channel 0 - 255
a: u8
Alpha channel 0 - 255
Methods
impl<'a> Color
[src]
impl<'a> Color
pub fn black() -> Color
[src]
pub fn black() -> Color
Returns a black Color.
pub fn blue() -> Color
[src]
pub fn blue() -> Color
Returns a blue Color.
pub fn green() -> Color
[src]
pub fn green() -> Color
Returns a green Color.
pub fn hex(hex: &str) -> RasterResult<Color>
[src]
pub fn hex(hex: &str) -> RasterResult<Color>
Create a color from hexadecimal value.
Example of valid formats: #FFFFFF, #ffeecc, #00ff007f
Errors
If the hex string is malformed (doesn't begin with #
or is of invalid length) then this
fails with RasterError::InvalidHex
. If it passes that, but the string can't be parsed
into actual values, then this fails with RasterError::HexParse
.
Examples
use raster::Color; // Ok tests let color = Color::hex("#FFFFFF"); // Opaque white assert!(color.is_ok()); let color = Color::hex("#00FF007F"); // Green with 50% opacity assert!(color.is_ok()); // Error tests let color = Color::hex(""); assert!(color.is_err()); let color = Color::hex("#"); assert!(color.is_err()); let color = Color::hex("#FFF"); assert!(color.is_err());
To get the value, use unwrap:
use raster::Color; let color = Color::hex("#00FF007F").unwrap(); assert_eq!(255, color.g);
pub fn red() -> Color
[src]
pub fn red() -> Color
Returns a red Color.
pub fn rgb(r: u8, g: u8, b: u8) -> Color
[src]
pub fn rgb(r: u8, g: u8, b: u8) -> Color
Create a RGB color. Alpha defaults to opaque (255).
Examples
use raster::Color; let rgb = Color::rgb(0, 255, 0); // Green println!("{:?}", rgb); assert_eq!(rgb.r, 0); assert_eq!(rgb.g, 255); assert_eq!(rgb.b, 0); assert_eq!(rgb.a, 255);
pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color
[src]
pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color
Create a RGBA color.
Examples
use raster::Color; let rgba = Color::rgba(0, 0, 255, 255); // Blue println!("{:?}", rgba); assert_eq!(rgba.r, 0); assert_eq!(rgba.g, 0); assert_eq!(rgba.b, 255); assert_eq!(rgba.a, 255);
pub fn to_hsv(r: u8, g: u8, b: u8) -> (u16, f32, f32)
[src]
pub fn to_hsv(r: u8, g: u8, b: u8) -> (u16, f32, f32)
Convert RGB to HSV/HSB (Hue, Saturation, Brightness).
use raster::Color; let hsv = Color::to_hsv(50, 50, 100); assert_eq!(240, hsv.0); assert_eq!(50.0, (hsv.1).round()); // Saturation in float assert_eq!(39.0, (hsv.2).round()); // Brightness in float
pub fn to_rgb(h: u16, s: f32, v: f32) -> (u8, u8, u8)
[src]
pub fn to_rgb(h: u16, s: f32, v: f32) -> (u8, u8, u8)
Convert HSV/HSB (Hue, Saturation, Brightness) to RGB.
use raster::Color; let rgb1 = (127, 70, 60); let hsv = Color::to_hsv(rgb1.0, rgb1.1, rgb1.2); // Convert to HSV let rgb2 = Color::to_rgb(hsv.0, hsv.1, hsv.2); // Convert back to RGB // Check if source RGB is equal to final RGB assert_eq!(rgb1.0, rgb2.0); assert_eq!(rgb1.1, rgb2.1); assert_eq!(rgb1.2, rgb2.2);
pub fn white() -> Color
[src]
pub fn white() -> Color
Returns a white Color.
Trait Implementations
impl Clone for Color
[src]
impl Clone for Color
fn clone(&self) -> Color
[src]
fn clone(&self) -> Color
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for Color
[src]
impl Debug for Color
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
[src]
fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)
[src]
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<T> SetParameter for T
[src]
impl<T> SetParameter for T