Add a color terminal renderer

This commit is contained in:
2020-07-12 23:00:54 -07:00
parent 52c2b5be5a
commit 80cb3a9d52
3 changed files with 25 additions and 2 deletions

View File

@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
ansi_term = "0.12"

View File

@@ -1,5 +1,5 @@
use coremem::SimState; use coremem::SimState;
use coremem::render::NumericTermRenderer; use coremem::render::ColorTermRenderer;
use coremem::consts; use coremem::consts;
use std::{thread, time}; use std::{thread, time};
@@ -12,7 +12,7 @@ fn main() {
state.impulse_b(15, 1.0/consts::C); state.impulse_b(15, 1.0/consts::C);
state.impulse_e(15, 1.0); state.impulse_e(15, 1.0);
loop { loop {
NumericTermRenderer.render(&state); ColorTermRenderer.render(&state);
state.step(); state.step();
thread::sleep(time::Duration::from_millis(100)); thread::sleep(time::Duration::from_millis(100));
} }

View File

@@ -1,3 +1,5 @@
use ansi_term::Color::RGB;
use crate::consts;
use crate::SimState; use crate::SimState;
pub struct NumericTermRenderer; pub struct NumericTermRenderer;
@@ -15,3 +17,23 @@ impl NumericTermRenderer {
print!("\n"); 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");
}
}