Skip to main content

ternary_spreadsheet/
format.rs

1use crate::cell::Cell;
2
3/// Color assigned based on fitness score.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum FitnessColor {
6    /// Red — avoid (negative fitness)
7    Red,
8    /// Yellow — unknown/neutral (near zero)
9    Yellow,
10    /// Green — choose (positive fitness)
11    Green,
12}
13
14impl std::fmt::Display for FitnessColor {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            FitnessColor::Red => write!(f, "red"),
18            FitnessColor::Yellow => write!(f, "yellow"),
19            FitnessColor::Green => write!(f, "green"),
20        }
21    }
22}
23
24/// Assign a fitness color to a cell.
25pub fn conditional_format(cell: &Cell) -> FitnessColor {
26    if cell.fitness > 0.5 {
27        FitnessColor::Green
28    } else if cell.fitness < -0.5 {
29        FitnessColor::Red
30    } else {
31        FitnessColor::Yellow
32    }
33}
34
35/// Assign fitness colors with custom thresholds.
36pub fn conditional_format_with_thresholds(
37    cell: &Cell,
38    green_threshold: f64,
39    red_threshold: f64,
40) -> FitnessColor {
41    if cell.fitness >= green_threshold {
42        FitnessColor::Green
43    } else if cell.fitness <= red_threshold {
44        FitnessColor::Red
45    } else {
46        FitnessColor::Yellow
47    }
48}
49
50/// Format a slice of cells into their color codes.
51pub fn format_cells(cells: &[Cell]) -> Vec<FitnessColor> {
52    cells.iter().map(conditional_format).collect()
53}