Factor out color handling into common module.

This commit is contained in:
Juergen Stuber
2018-11-30 20:34:08 +01:00
parent e15ac35f18
commit 610c864cd9
5 changed files with 73 additions and 147 deletions

View File

@ -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<Vec<Square>>;
fn send<T: Write>(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()