From 42455d593e1d6345c024641960bffb844e298ed1 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Thu, 24 Jun 2021 15:48:04 -0700 Subject: [PATCH] Add Color type. --- rtchallenge/src/tuples.rs | 118 +++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/rtchallenge/src/tuples.rs b/rtchallenge/src/tuples.rs index bff4917..19116c2 100644 --- a/rtchallenge/src/tuples.rs +++ b/rtchallenge/src/tuples.rs @@ -124,11 +124,95 @@ pub fn cross(a: Tuple, b: Tuple) -> Tuple { ) } +#[derive(Copy, Clone, Debug, PartialEq)] +struct Color { + red: f32, + green: f32, + blue: f32, +} +fn color(red: f32, green: f32, blue: f32) -> Color { + Color { red, green, blue } +} +impl Add for Color { + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + red: self.red + other.red, + green: self.green + other.green, + blue: self.blue + other.blue, + } + } +} + +impl Div for Color { + type Output = Self; + fn div(self, rhs: f32) -> Self::Output { + Self::Output { + red: self.red / rhs, + green: self.green / rhs, + blue: self.blue / rhs, + } + } +} + +impl Mul for Color { + type Output = Self; + fn mul(self, rhs: f32) -> Self::Output { + Self::Output { + red: self.red * rhs, + green: self.green * rhs, + blue: self.blue * rhs, + } + } +} + +impl Mul for f32 { + type Output = Color; + fn mul(self, rhs: Color) -> Self::Output { + Self::Output { + red: self * rhs.red, + green: self * rhs.green, + blue: self * rhs.blue, + } + } +} +impl Mul for Color { + type Output = Color; + fn mul(self, rhs: Color) -> Self::Output { + Self::Output { + red: self.red * rhs.red, + green: self.green * rhs.green, + blue: self.blue * rhs.blue, + } + } +} + +impl Neg for Color { + type Output = Self; + fn neg(self) -> Self::Output { + Self { + red: -self.red, + green: -self.green, + blue: -self.blue, + } + } +} +impl Sub for Color { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + red: self.red - other.red, + green: self.green - other.green, + blue: self.blue - other.blue, + } + } +} + #[cfg(test)] mod tests { use float_cmp::approx_eq; - use super::{cross, dot, point, tuple, vector}; + use super::{color, cross, dot, point, tuple, vector}; #[test] fn is_point() { // A tuple with w = 1 is a point @@ -249,4 +333,36 @@ mod tests { assert_eq!(vector(-1., 2., -1.), cross(a, b)); assert_eq!(vector(1., -2., 1.), cross(b, a)); } + + #[test] + fn color_rgb() { + let c = color(-0.5, 0.4, 1.7); + assert_eq!(c.red, -0.5); + assert_eq!(c.green, 0.4); + assert_eq!(c.blue, 1.7); + } + #[test] + fn add_color() { + let c1 = color(0.9, 0.6, 0.75); + let c2 = color(0.7, 0.1, 0.25); + assert_eq!(c1 + c2, color(0.9 + 0.7, 0.6 + 0.1, 0.75 + 0.25)); + } + #[test] + fn sub_color() { + let c1 = color(0.9, 0.6, 0.75); + let c2 = color(0.7, 0.1, 0.25); + assert_eq!(c1 - c2, color(0.9 - 0.7, 0.6 - 0.1, 0.75 - 0.25)); + } + #[test] + fn mul_color_scalar() { + let c = color(0.2, 0.3, 0.4); + assert_eq!(c * 2., color(0.2 * 2., 0.3 * 2., 0.4 * 2.)); + assert_eq!(2. * c, color(0.2 * 2., 0.3 * 2., 0.4 * 2.)); + } + #[test] + fn mul_colors() { + let c1 = color(1., 0.2, 0.4); + let c2 = color(0.9, 1., 0.1); + assert_eq!(c1 * c2, color(1.0 * 0.9, 0.2 * 1., 0.4 * 0.1)); + } }