Skip to content

Commit

Permalink
Change Color to be a struct rather than an enum
Browse files Browse the repository at this point in the history
This reduces the size of the type and eliminates the need to branch to
read from it, increasing its usability, and bringing it closer to the
conceptual Color type used by most libraries and SDL itself.
  • Loading branch information
SpaceManiac committed Apr 10, 2017
1 parent 8bcf9f6 commit 6e1af48
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 46 deletions.
9 changes: 1 addition & 8 deletions src/sdl2/gfx/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,7 @@ pub trait ToColor {
impl ToColor for pixels::Color {
#[inline]
fn as_rgba(&self) -> (u8, u8, u8, u8) {
match *self {
pixels::Color::RGB(r, g, b) => {
(r, g, b, 255u8)
}
pixels::Color::RGBA(r, g, b, a) => {
(r, g, b, a)
}
}
self.rgba()
}
}

Expand Down
45 changes: 24 additions & 21 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,28 @@ fn create_palette() {
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Color {
RGB(u8, u8, u8),
RGBA(u8, u8, u8, u8)
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8
}

impl Color {
#[inline]
#[allow(non_snake_case)]
pub fn RGB(r: u8, g: u8, b: u8) -> Color {
Color { r: r, g: g, b: b, a: 0xff }
}

#[inline]
#[allow(non_snake_case)]
pub fn RGBA(r: u8, g: u8, b: u8, a: u8) -> Color {
Color { r: r, g: g, b: b, a: a }
}

pub fn to_u32(&self, format: &PixelFormat) -> u32 {
match self {
&Color::RGB(r, g, b) => {
unsafe { ll::SDL_MapRGB(format.raw, r, g, b) }
}
&Color::RGBA(r, g, b, a) => {
unsafe { ll::SDL_MapRGBA(format.raw, r, g, b, a) }
}
}
unsafe { ll::SDL_MapRGBA(format.raw, self.r, self.g, self.b, self.a) }
}

pub fn from_u32(format: &PixelFormat, pixel: u32) -> Color {
Expand All @@ -116,24 +123,20 @@ impl Color {
Color::RGBA(r, g, b, a)
}

#[inline]
pub fn rgb(&self) -> (u8, u8, u8) {
match self {
&Color::RGB(r, g, b) => (r, g, b),
&Color::RGBA(r, g, b, _) => (r, g, b)
}
(self.r, self.g, self.b)
}

#[inline]
pub fn rgba(&self) -> (u8, u8, u8, u8) {
match self {
&Color::RGB(r, g, b) => (r, g, b, 0xff),
&Color::RGBA(r, g, b, a) => (r, g, b, a),
}
(self.r, self.g, self.b, self.a)
}

// Implemented manually and kept private, because reasons
#[inline]
unsafe fn raw(&self) -> ll::SDL_Color {
let (r, g, b, a) = self.rgba();
ll::SDL_Color { r: r, g: g, b: b, a: a }
ll::SDL_Color { r: self.r, g: self.g, b: self.b, a: self.a }
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,8 @@ impl<'a> Renderer<'a> {
impl<'a> Renderer<'a> {
/// Sets the color used for drawing operations (Rect, Line and Clear).
pub fn set_draw_color(&mut self, color: pixels::Color) {
let ret = match color {
pixels::Color::RGB(r, g, b) => {
unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, 255) }
},
pixels::Color::RGBA(r, g, b, a) => {
unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, a) }
}
};
let (r, g, b, a) = color.rgba();
let ret = unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, a) };
// Should only fail on an invalid renderer
if ret != 0 { panic!(get_error()) }
}
Expand Down
6 changes: 1 addition & 5 deletions src/sdl2/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,7 @@ impl SurfaceRef {
}

pub fn set_color_mod(&mut self, color: pixels::Color) {
let (r, g, b) = match color {
pixels::Color::RGB(r, g, b) => (r, g, b),
pixels::Color::RGBA(r, g, b, _) => (r, g, b)
};

let (r, g, b) = color.rgb();
let result = unsafe { ll::SDL_SetSurfaceColorMod(self.raw(), r, g, b) };

if result != 0 {
Expand Down
5 changes: 1 addition & 4 deletions src/sdl2/ttf/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use super::ffi;
/// Converts a rust-SDL2 color to its C ffi representation.
#[inline]
fn color_to_c_color(color: Color) -> SDL_Color {
match color {
pixels::Color::RGB(r, g, b) => SDL_Color { r: r, g: g, b: b, a: 255 },
pixels::Color::RGBA(r, g, b, a) => SDL_Color { r: r, g: g, b: b, a: a }
}
SDL_Color { r: color.r, g: color.g, b: color.b, a: color.a }
}


Expand Down

0 comments on commit 6e1af48

Please sign in to comment.