color can use any number type
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use crate::num_traits_cast;
|
||||
use std::ops;
|
||||
use num_traits::NumCast;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Color {
|
||||
@@ -8,11 +10,15 @@ pub struct Color {
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub fn new(red: f32, green: f32, blue: f32) -> Color {
|
||||
pub fn new<R, G, B>(red: R, green: G, blue: B) -> Color
|
||||
where R: NumCast,
|
||||
G: NumCast,
|
||||
B: NumCast,
|
||||
{
|
||||
Color {
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
red: num_traits_cast!(red),
|
||||
green: num_traits_cast!(green),
|
||||
blue: num_traits_cast!(blue),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +118,7 @@ mod tests {
|
||||
fn add() {
|
||||
let c1 = Color::new(0.9, 0.6, 0.75);
|
||||
let c2 = Color::new(0.7, 0.1, 0.25);
|
||||
let res = Color::new(1.6, 0.7, 1.0);
|
||||
let res = Color::new(1.6, 0.7, 1);
|
||||
|
||||
assert_eq!(res, c1 + c2);
|
||||
}
|
||||
@@ -136,8 +142,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn mul() {
|
||||
let c1 = Color::new(1.0, 0.2, 0.4);
|
||||
let c2 = Color::new(0.9, 1.0, 0.1);
|
||||
let c1 = Color::new(1, 0.2, 0.4);
|
||||
let c2 = Color::new(0.9, 1, 0.1);
|
||||
let res = Color::new(0.9, 0.2, 0.04);
|
||||
|
||||
assert_eq!(res, c1 * c2);
|
||||
|
||||
Reference in New Issue
Block a user