From 2fb12d9999052f1537ecb6a8a706e9f07c44203c Mon Sep 17 00:00:00 2001 From: shybyte Date: Tue, 28 Nov 2017 10:45:19 +0100 Subject: [PATCH 1/2] Add tuple conversions with From for Color #699 --- src/sdl2/pixels.rs | 13 +++++++++++++ src/sdl2/render.rs | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/sdl2/pixels.rs b/src/sdl2/pixels.rs index 9b9cff8da4f..8a4b6cb2447 100644 --- a/src/sdl2/pixels.rs +++ b/src/sdl2/pixels.rs @@ -157,6 +157,19 @@ impl From 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(rng: &mut R) -> Color { if rng.gen() { Color::RGBA(rng.gen(), rng.gen(), rng.gen(), rng.gen()) } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 48fa44a598e..a8f41540969 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -902,8 +902,8 @@ impl Canvas { } /// 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>(&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 { From a61dc4cd614dcff4e77d0cf20d225f85ba782a56 Mon Sep 17 00:00:00 2001 From: shybyte Date: Tue, 28 Nov 2017 20:11:23 +0100 Subject: [PATCH 2/2] Added conversions of tuples to Color (issue #699) * Added PR to changelog --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/changelog.md b/changelog.md index bd84dd1a755..1d1b8aaae16 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,10 @@ when upgrading from a version of rust-sdl2 to another. ### v0.32 +[PR #732](https://github.com/Rust-SDL2/rust-sdl2/pull/732) +* Implemented `From<(u8, u8, u8)>` and `From<(u8, u8, u8, u8)>` for `pixels::Color`. + `Canvas.set_draw_color` can now be called with tuples or other types which implements `Into` + [PR #279](https://github.com/Rust-SDL2/rust-sdl2/pull/729) * **Breaking change** set\_video\_minimize\_on\_focus\_lost was renamed to …minimize\_on\_focus\_loss, as it should be. As a bonus, it works now.