From 610c864cd98d3c173360ccf1d5c45102314e6d31 Mon Sep 17 00:00:00 2001 From: Juergen Stuber Date: Fri, 30 Nov 2018 20:34:08 +0100 Subject: [PATCH] Factor out color handling into common module. --- src/bin/bimood/main.rs | 56 ++--------------------------------- src/bin/maze/main.rs | 51 ++------------------------------ src/bin/predprey/main.rs | 42 ++------------------------- src/color.rs | 63 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 8 +---- 5 files changed, 73 insertions(+), 147 deletions(-) create mode 100644 src/color.rs diff --git a/src/bin/bimood/main.rs b/src/bin/bimood/main.rs index ba73a45..a33205d 100644 --- a/src/bin/bimood/main.rs +++ b/src/bin/bimood/main.rs @@ -5,64 +5,14 @@ use std::iter::repeat; use std::thread::sleep; use std::time::Duration; -#[derive(Clone, Copy, Debug)] -struct Color(u8, u8, u8); - -impl Color { - fn black() -> Color { - Color(0, 0, 0) - } - fn white() -> Color { - Color(255, 255, 255) - } - fn gray50() -> Color { - Color(128, 128, 128) - } - fn red() -> Color { - Color(255, 0, 0) - } - fn green() -> Color { - Color(0, 255, 0) - } - fn blue() -> Color { - Color(0, 0, 255) - } - fn yellow() -> Color { - Color(255, 255, 0) - } - fn cyan() -> Color { - Color(0, 255, 255) - } - fn magenta() -> Color { - Color(255, 0, 255) - } - fn complement(&self) -> Color { - Color(255 - self.0, 255 - self.1, 255 - self.2) - } -} - -fn interpolate_u8(b0: u8, b1: u8, a: f64) -> u8 { - let b0 = b0 as f64; - let b1 = b1 as f64; - ((1.0 - a) * b0 + a * b1) as u8 -} - -fn interpolate(c0: Color, c1: Color, a: f64) -> Color { - Color( - interpolate_u8(c0.0, c1.0, a), - interpolate_u8(c0.1, c1.1, a), - interpolate_u8(c0.2, c1.2, a), - ) -} +use pixelfoo::color::Color; type Frame = Vec>; fn send(w: &mut T, f: &Frame) -> std::io::Result<()> { for l in f { for c in l { - let Color(r, g, b) = c; - let buf = &[*r, *g, *b]; - w.write_all(buf)?; + w.write_all(&c.rgb())?; } } w.flush() @@ -109,7 +59,7 @@ fn main() -> std::io::Result<()> { eprint!("delta_a {}", delta_a); loop { - let c0 = interpolate(previous_color, next_color, a); + let c0 = previous_color.interpolate(next_color, a); let c1 = c0.complement(); let mut frame = repeat(repeat(c0).take(x_size).collect::>()) .take(y_size) diff --git a/src/bin/maze/main.rs b/src/bin/maze/main.rs index 5c208c1..eac650b 100644 --- a/src/bin/maze/main.rs +++ b/src/bin/maze/main.rs @@ -7,51 +7,7 @@ use std::time::Duration; use rand::thread_rng; use rand::Rng; -#[derive(Clone, Copy, Debug)] -struct Color(u8, u8, u8); - -#[allow(unused)] -impl Color { - fn black() -> Color { - Color(0, 0, 0) - } - fn white() -> Color { - Color(255, 255, 255) - } - fn gray50() -> Color { - Color(128, 128, 128) - } - fn red() -> Color { - Color(255, 0, 0) - } - fn green() -> Color { - Color(0, 255, 0) - } - fn blue() -> Color { - Color(0, 0, 255) - } - fn yellow() -> Color { - Color(255, 255, 0) - } - fn cyan() -> Color { - Color(0, 255, 255) - } - fn magenta() -> Color { - Color(255, 0, 255) - } - fn brown() -> Color { - Color(210, 105, 30) - } - fn darkblue() -> Color { - Color(0, 0, 127) - } - fn darkbrown() -> Color { - Color(139, 69, 19) - } - fn complement(&self) -> Color { - Color(255 - self.0, 255 - self.1, 255 - self.2) - } -} +use pixelfoo::color::Color; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Square { @@ -67,15 +23,14 @@ struct Board(Vec>); fn send(w: &mut T, board: &Board) -> std::io::Result<()> { for line in &board.0 { for square in line { - let Color(r, g, b) = match square { + let c = match square { Square::Unknown => Color::black(), Square::Corridor => Color::yellow(), Square::Wall => Color::darkblue(), Square::Start => Color::red(), Square::Finish => Color::green(), }; - let buf = &[r, g, b]; - w.write_all(buf)?; + w.write_all(&c.rgb())?; } } w.flush() diff --git a/src/bin/predprey/main.rs b/src/bin/predprey/main.rs index 8e36d3c..1a040a0 100644 --- a/src/bin/predprey/main.rs +++ b/src/bin/predprey/main.rs @@ -8,42 +8,7 @@ use std::time::Duration; use rand::thread_rng; use rand::Rng; -#[derive(Clone, Copy, Debug)] -struct Color(u8, u8, u8); - -#[allow(unused)] -impl Color { - fn black() -> Color { - Color(0, 0, 0) - } - fn white() -> Color { - Color(255, 255, 255) - } - fn gray50() -> Color { - Color(128, 128, 128) - } - fn red() -> Color { - Color(255, 0, 0) - } - fn green() -> Color { - Color(0, 255, 0) - } - fn blue() -> Color { - Color(0, 0, 255) - } - fn yellow() -> Color { - Color(255, 255, 0) - } - fn cyan() -> Color { - Color(0, 255, 255) - } - fn magenta() -> Color { - Color(255, 0, 255) - } - fn complement(&self) -> Color { - Color(255 - self.0, 255 - self.1, 255 - self.2) - } -} +use pixelfoo::color::Color; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Square { @@ -58,14 +23,13 @@ type Board = Vec>; fn send(w: &mut T, board: &Board) -> std::io::Result<()> { for line in board { for square in line { - let Color(r, g, b) = match square { + let c = match square { Square::Empty => Color::blue(), Square::Grass => Color::green(), Square::Rabbit => Color::yellow(), Square::Fox => Color::red(), }; - let buf = &[r, g, b]; - w.write_all(buf)?; + w.write_all(&c.rgb())?; } } w.flush() diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..c3c26d9 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,63 @@ +#[derive(Clone, Copy, Debug)] +pub struct Color(u8, u8, u8); + +#[allow(unused)] +impl Color { + pub fn black() -> Color { + Color(0, 0, 0) + } + pub fn white() -> Color { + Color(255, 255, 255) + } + pub fn gray50() -> Color { + Color(128, 128, 128) + } + pub fn red() -> Color { + Color(255, 0, 0) + } + pub fn green() -> Color { + Color(0, 255, 0) + } + pub fn blue() -> Color { + Color(0, 0, 255) + } + pub fn yellow() -> Color { + Color(255, 255, 0) + } + pub fn cyan() -> Color { + Color(0, 255, 255) + } + pub fn magenta() -> Color { + Color(255, 0, 255) + } + pub fn brown() -> Color { + Color(210, 105, 30) + } + pub fn darkblue() -> Color { + Color(0, 0, 127) + } + pub fn darkbrown() -> Color { + Color(139, 69, 19) + } + pub fn complement(&self) -> Color { + Color(255 - self.0, 255 - self.1, 255 - self.2) + } + + pub fn rgb(&self) -> [u8; 3] { + [self.0, self.1, self.2] + } + + pub fn interpolate(self, c: Color, a: f64) -> Color { + Color( + interpolate_u8(self.0, c.0, a), + interpolate_u8(self.1, c.1, a), + interpolate_u8(self.2, c.2, a), + ) + } +} + +fn interpolate_u8(b0: u8, b1: u8, a: f64) -> u8 { + let b0 = b0 as f64; + let b1 = b1 as f64; + ((1.0 - a) * b0 + a * b1) as u8 +} diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..2c53cf0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +pub mod color;