Struct Color

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

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

Implementations§

Source§

impl<'a> Color

Source

pub fn black() -> Color

Returns a black Color.

Source

pub fn blue() -> Color

Returns a blue Color.

Source

pub fn green() -> Color

Returns a green Color.

Source

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

pub fn red() -> Color

Returns a red Color.

Source

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

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

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
Source

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

pub fn white() -> Color

Returns a white Color.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

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 Debug for Color

Source§

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

Formats the value using the given formatter. Read more

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> SetParameter for T

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of self.
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.