tuples: implement EPSILON aware PartialEq for Color.

Mark Color::new const so it can be used in const expressions (like
predefining colors).
This commit is contained in:
Bill Thiede 2021-07-17 09:30:28 -07:00
parent 1cbfbc8641
commit 385ed70d88

View File

@ -158,17 +158,24 @@ pub fn cross(a: Tuple, b: Tuple) -> Tuple {
)
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug)]
pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
}
impl Color {
pub fn new(red: f32, green: f32, blue: f32) -> Color {
pub const fn new(red: f32, green: f32, blue: f32) -> Color {
Color { red, green, blue }
}
}
impl PartialEq for Color {
fn eq(&self, rhs: &Color) -> bool {
((self.red - rhs.red).abs() < EPSILON)
&& ((self.green - rhs.green).abs() < EPSILON)
&& ((self.blue - rhs.blue).abs() < EPSILON)
}
}
impl Add for Color {
type Output = Self;
fn add(self, other: Self) -> Self {