Add tuple conversions with From for Color #699

This commit is contained in:
shybyte
2017-11-29 06:04:05 +00:00
committed by Cobrand
parent 4f9ed1a72a
commit 175da3304c
2 changed files with 15 additions and 2 deletions
+13
View File
@@ -157,6 +157,19 @@ impl From<sys::SDL_Color> for Color {
}
}
impl From<(u8, u8, u8)> for Color {
fn from((r, g, b): (u8, u8, u8)) -> Color {
Color::RGB(r, g, b)
}
}
impl From<(u8, u8, u8, u8)> for Color {
fn from((r, g, b, a): (u8, u8, u8, u8)) -> Color {
Color::RGBA(r, g, b, a)
}
}
impl rand::Rand for Color {
fn rand<R: rand::Rng>(rng: &mut R) -> Color {
if rng.gen() { Color::RGBA(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
+2 -2
View File
@@ -902,8 +902,8 @@ impl<T: RenderTarget> Canvas<T> {
}
/// Sets the color used for drawing operations (Rect, Line and Clear).
pub fn set_draw_color(&mut self, color: pixels::Color) {
let (r, g, b, a) = color.rgba();
pub fn set_draw_color<C: Into<pixels::Color>>(&mut self, color: C) {
let (r, g, b, a) = color.into().rgba();
let ret = unsafe { sys::SDL_SetRenderDrawColor(self.raw, r, g, b, a) };
// Should only fail on an invalid renderer
if ret != 0 {