Skip to content

Commit

Permalink
Merge pull request #124 from aaneto/master
Browse files Browse the repository at this point in the history
Implement Color constructors taking integer arguments (fixes #123)
  • Loading branch information
17cupsofcoffee authored May 11, 2019
2 parents 79089ad + ee1e335 commit ab210a4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/graphics/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ impl Color {
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
Color { r, g, b, a }
}

/// Creates a new `Color`, with the specified RGB integer values and the alpha set to 1.0.
pub fn rgb8(r: u8, g: u8, b: u8) -> Color {
let r = f32::from(r) / 255.0;
let g = f32::from(g) / 255.0;
let b = f32::from(b) / 255.0;

Color { r, g, b, a: 1.0 }
}

/// Creates a new `Color`, with the specified RGBA integer values.
pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color {
let r = f32::from(r) / 255.0;
let g = f32::from(g) / 255.0;
let b = f32::from(b) / 255.0;
let a = f32::from(a) / 255.0;

Color { r, g, b, a }
}
}

/// Shortcut for Color::rgb(0.0, 0.0, 0.0).
Expand All @@ -43,3 +62,18 @@ pub const WHITE: Color = Color {
b: 1.0,
a: 1.0,
};

#[cfg(test)]
mod tests {
use super::Color;

#[test]
fn rgb8_creation() {
let c1 = Color::rgb8(100, 149, 236);
let c2 = Color::rgb(0.39, 0.58, 0.92);

assert!((c1.r - c2.r).abs() < 0.01);
assert!((c1.g - c2.g).abs() < 0.01);
assert!((c1.b - c2.b).abs() < 0.01);
}
}

0 comments on commit ab210a4

Please sign in to comment.