Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tuple conversions with From for Color #699 #732

Merged
merged 2 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<pixels::Color>`

[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.
Expand Down
13 changes: 13 additions & 0 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
Expand Down
4 changes: 2 additions & 2 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down