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

@ -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<Vec<Color>>;
fn send<T: Write>(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::<Vec<_>>())
.take(y_size)

View File

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

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()

63
src/color.rs Normal file
View File

@ -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
}

View File

@ -1,7 +1 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub mod color;