Add a color terminal renderer
This commit is contained in:
@@ -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