Add a color terminal renderer
This commit is contained in:
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
ansi_term = "0.12"
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use coremem::SimState;
|
||||
use coremem::render::NumericTermRenderer;
|
||||
use coremem::render::ColorTermRenderer;
|
||||
use coremem::consts;
|
||||
use std::{thread, time};
|
||||
|
||||
@@ -12,7 +12,7 @@ fn main() {
|
||||
state.impulse_b(15, 1.0/consts::C);
|
||||
state.impulse_e(15, 1.0);
|
||||
loop {
|
||||
NumericTermRenderer.render(&state);
|
||||
ColorTermRenderer.render(&state);
|
||||
state.step();
|
||||
thread::sleep(time::Duration::from_millis(100));
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
use ansi_term::Color::RGB;
|
||||
use crate::consts;
|
||||
use crate::SimState;
|
||||
|
||||
pub struct NumericTermRenderer;
|
||||
@@ -15,3 +17,23 @@ impl NumericTermRenderer {
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ColorTermRenderer;
|
||||
|
||||
fn clamp(v: f32, range: f32) -> f32 {
|
||||
v.min(range).max(-range)
|
||||
}
|
||||
|
||||
impl ColorTermRenderer {
|
||||
pub fn render(&self, state: &SimState) {
|
||||
let square = "█";
|
||||
for cell in state.cells() {
|
||||
let b_value = clamp(cell.by() * consts::C * 128.0, 255.0);
|
||||
let b_color = RGB(0, b_value.max(0.0) as _, b_value.abs() as _);
|
||||
let e_value = clamp(cell.ez() * 128.0, 255.0);
|
||||
let e_color = RGB(e_value.abs() as _, e_value.max(0.0) as _, 0);
|
||||
print!("{}{}", e_color.paint(square), b_color.paint(square));
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user