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
impl<'a> Color
Sourcepub fn hex(hex: &str) -> RasterResult<Color>
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);
Sourcepub fn rgb(r: u8, g: u8, b: u8) -> Color
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);
Sourcepub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color
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);
Sourcepub fn to_hsv(r: u8, g: u8, b: u8) -> (u16, f32, f32)
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
Sourcepub fn to_rgb(h: u16, s: f32, v: f32) -> (u8, u8, u8)
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);
Trait Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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