ternary_spreadsheet/
format.rs1use crate::cell::Cell;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum FitnessColor {
6 Red,
8 Yellow,
10 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
24pub 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
35pub 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
50pub fn format_cells(cells: &[Cell]) -> Vec<FitnessColor> {
52 cells.iter().map(conditional_format).collect()
53}