From d160ef855a642f27a0e472b12f5df3cb3dfe43c6 Mon Sep 17 00:00:00 2001 From: cc Date: Wed, 20 Aug 2025 23:10:57 -0700 Subject: Breaking Display Change: Color! --- src/label_format.rs | 56 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 11 deletions(-) (limited to 'src/label_format.rs') diff --git a/src/label_format.rs b/src/label_format.rs index 1762445..68efda4 100644 --- a/src/label_format.rs +++ b/src/label_format.rs @@ -35,7 +35,7 @@ impl LabelFormat { return None; } - pub fn display(&self, include_space: bool) { + pub fn text_display(&self, include_space: bool) { for y in 0..self.height { for x in 0..self.width { print!("{:04X}", self.buffer[x + y*self.width]); @@ -226,6 +226,40 @@ impl LabelFormat { height: self.height, }; } + + pub fn display(&self) { + for y in 0..self.height { + for x in 0..self.width { + let label = self.buffer[x + y*self.width]; + color::set_color(label as usize); + print!(" "); + } + color::reset_color(); + println!(); + } + } + +} + +mod color { + pub(crate) fn reset_color() { + print!("\x1b[0m"); + } + + // Set background color from a number + pub(crate) fn set_color(color: usize) { + if color == 0 { + reset_color(); + return; + } + let paint_color = color-1; + let paint_color = paint_color % 13; + if paint_color < 7 { + print!("\x1b[{}m", 40 + (paint_color+1)); + } else { + print!("\x1b[{}m", 100 + (paint_color+1-7)); + } + } } #[cfg(test)] @@ -244,13 +278,13 @@ mod tests { test_data.buffer[2+3*DIM] = 1; test_data.buffer[3+3*DIM] = 1; test_data.buffer[4+3*DIM] = 2; - test_data.display(true); + test_data.display(); println!(); let dilated = test_data.dilate(); - dilated.display(true); + dilated.display(); println!(); let restored = dilated.erode(); - restored.display(true); + restored.display(); } #[test] @@ -265,13 +299,13 @@ mod tests { test_data.buffer[2+3*DIM] = 1; test_data.buffer[3+3*DIM] = 1; test_data.buffer[4+3*DIM] = 2; - test_data.display(true); + test_data.display(); println!(); test_data.idilate(); - test_data.display(true); + test_data.display(); println!(); test_data.ierode(); - test_data.display(true); + test_data.display(); println!(); } @@ -287,7 +321,7 @@ mod tests { test_data_1.buffer[2+3*DIM] = 1; test_data_1.buffer[3+3*DIM] = 1; test_data_1.buffer[4+3*DIM] = 2; - test_data_1.display(true); + test_data_1.display(); println!(); let mut test_data_2 = LabelFormat { @@ -300,15 +334,15 @@ mod tests { test_data_2.buffer[3+1*DIM] = 2; test_data_2.buffer[3+2*DIM] = 2; test_data_2.buffer[4+1*DIM] = 3; - test_data_2.display(true); + test_data_2.display(); println!(); if let Some(out_data) = test_data_1.combine(&test_data_2) { - out_data.display(true); + out_data.display(); println!(); let refreshed = out_data.refresh(); - refreshed.display(true); + refreshed.display(); println!(); } else { assert!(false); -- cgit v1.2.1