[][src]Struct raster::Color

pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

A struct for representing and creating color.

Fields

Red channel 0 - 255

Green channel 0 - 255

Blue channel 0 - 255

Alpha channel 0 - 255

Methods

impl<'a> Color
[src]

Returns a black Color.

Returns a blue Color.

Returns a green 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);

Returns a red 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);

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

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

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

Returns a white Color.

Trait Implementations

impl Clone for Color
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Color
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Color

impl Sync for Color

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

Creates owned data from borrowed data, usually by cloning. Read more

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

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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

Sets value as a parameter of self.