Added From<(u8, u8, u8)> Trait to Color::Rgb Enum. (#440)

This commit is contained in:
Cowboy8625 2020-06-10 06:35:06 -05:00 committed by GitHub
parent 5511f85d50
commit b9c8dcdbfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -133,6 +133,14 @@ impl FromStr for Color {
}
}
impl From<(u8, u8, u8)> for Color {
/// Creates a 'Color' from the tuple representation.
fn from(val: (u8, u8, u8)) -> Self {
let (r, g, b) = val;
Self::Rgb { r, g, b }
}
}
#[cfg(test)]
mod tests {
use super::Color;
@ -161,4 +169,17 @@ mod tests {
fn test_unknown_color_conversion_yields_white() {
assert_eq!("foo".parse(), Ok(Color::White));
}
#[test]
fn test_know_rgb_color_conversion() {
assert_eq!(Color::from((0, 0, 0)), Color::Rgb { r: 0, g: 0, b: 0 });
assert_eq!(
Color::from((255, 255, 255)),
Color::Rgb {
r: 255,
g: 255,
b: 255
}
);
}
}