From cf7860c5c5c6df088e742c90cb54ba14bfea164c Mon Sep 17 00:00:00 2001 From: Emilia Date: Tue, 13 Jun 2023 19:32:39 -0400 Subject: [PATCH] Use UUID to calculate colors Ensures that a transfer will always be represented by the same color. --- src/components/plates/destination_plate.rs | 40 ++++++++-------------- src/components/plates/source_plate.rs | 40 ++++++++-------------- src/components/plates/util.rs | 5 +++ 3 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/components/plates/destination_plate.rs b/src/components/plates/destination_plate.rs index 9dde629..4d71119 100644 --- a/src/components/plates/destination_plate.rs +++ b/src/components/plates/destination_plate.rs @@ -6,6 +6,7 @@ use yewdux::prelude::*; use crate::components::states::{CurrentTransfer, MainState}; use crate::data::plate_instances::PlateInstance; +use crate::data::transfer::Transfer; use crate::data::transfer_region::Region; // Color Palette for the Source Plates, can be changed here @@ -58,36 +59,19 @@ pub fn DestinationPlate(props: &DestinationPlateProps) -> Html { }) }; - let mut color_counter: u8 = 0; - let color_map = { + let transfer_map = { let ts = main_state .transfers .iter() .filter(|t| t.dest_id == props.destination_plate.get_uuid()); - let mut color_map: HashMap<(u8, u8), u8> = HashMap::new(); - for t in ts { - color_counter += 1; - let dws = t.transfer_region.get_destination_wells(); - for dw in dws { - color_map.insert(dw, color_counter); - } - } - color_map - }; - - let tooltip_map = { - let ts = main_state - .transfers - .iter() - .filter(|t| t.dest_id == props.destination_plate.get_uuid()); - let mut tooltip_map: HashMap<(u8,u8), Vec> = HashMap::new(); + let mut tooltip_map: HashMap<(u8,u8), Vec<&Transfer>> = HashMap::new(); for t in ts { let dws = t.transfer_region.get_destination_wells(); for dw in dws { if let Some(val) = tooltip_map.get_mut(&dw) { - val.push(t.name.clone()); + val.push(t); } else { - tooltip_map.insert(dw, vec![t.name.clone()]); + tooltip_map.insert(dw, vec![t]); } } } @@ -131,10 +115,14 @@ pub fn DestinationPlate(props: &DestinationPlateProps) -> Html { selected={super::source_plate::in_rect(*m_start_handle.clone(), *m_end_handle.clone(), (i,j))} mouse={mouse_callback.clone()} in_transfer={destination_wells.contains(&(i,j))} - color={color_map.get(&(i,j)).copied()} + color={transfer_map.get(&(i,j)) + .and_then(|t| t.last()) + .map(|t| PALETTE.get_uuid(t.get_uuid())) + } cell_height={props.cell_height} - title={if let Some(names) = tooltip_map.get(&(i,j)) { - Some(format!("Used by: {}", names.join(", "))) + title={if let Some(transfers) = transfer_map.get(&(i,j)) { + Some(format!("Used by: {}", transfers.iter().map(|t| t.name.clone()) + .collect::>().join(", "))) } else { None }} /> } @@ -176,7 +164,7 @@ pub struct DestPlateCellProps { pub selected: bool, pub mouse: Callback<(u8, u8, MouseEventType)>, pub in_transfer: Option, - color: Option, + color: Option<[f64; 3]>, cell_height: f64, title: Option, } @@ -192,7 +180,7 @@ fn DestPlateCell(props: &DestPlateCellProps) -> Html { _ => None, }; let color = match props.color { - Some(num) => PALETTE.get_u8(num), + Some(num) => num, None => [255.0, 255.0, 255.0], }; let mouse = Callback::clone(&props.mouse); diff --git a/src/components/plates/source_plate.rs b/src/components/plates/source_plate.rs index be9dc1b..c13176e 100644 --- a/src/components/plates/source_plate.rs +++ b/src/components/plates/source_plate.rs @@ -6,6 +6,7 @@ use yewdux::prelude::*; use crate::components::states::{CurrentTransfer, MainState}; use crate::data::plate_instances::PlateInstance; +use crate::data::transfer::Transfer; use crate::data::transfer_region::Region; // Color Palette for the Source Plates, can be changed here @@ -38,36 +39,19 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html { m_end_handle.set(Some(pt2)); } - let mut color_counter: u8 = 0; - let color_map = { + let transfer_map = { let ts = main_state .transfers .iter() .filter(|t| t.source_id == props.source_plate.get_uuid()); - let mut color_map: HashMap<(u8, u8), u8> = HashMap::new(); - for t in ts { - color_counter += 1; - let sws = t.transfer_region.get_source_wells(); - for sw in sws { - color_map.insert(sw, color_counter); - } - } - color_map - }; - - let tooltip_map = { - let ts = main_state - .transfers - .iter() - .filter(|t| t.source_id == props.source_plate.get_uuid()); - let mut tooltip_map: HashMap<(u8,u8), Vec> = HashMap::new(); + let mut tooltip_map: HashMap<(u8,u8), Vec<&Transfer>> = HashMap::new(); for t in ts { let sws = t.transfer_region.get_source_wells(); for sw in sws { if let Some(val) = tooltip_map.get_mut(&sw) { - val.push(t.name.clone()); + val.push(t); } else { - tooltip_map.insert(sw, vec![t.name.clone()]); + tooltip_map.insert(sw, vec![t]); } } } @@ -135,10 +119,14 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html { selected={in_rect(*m_start_handle.clone(), *m_end_handle.clone(), (i,j))} mouse={mouse_callback.clone()} in_transfer={source_wells.contains(&(i,j))} - color={color_map.get(&(i,j)).copied()} + color={transfer_map.get(&(i,j)) + .and_then(|t| t.last()) + .map(|t| PALETTE.get_uuid(t.get_uuid())) + } cell_height={props.cell_height} - title={if let Some(names) = tooltip_map.get(&(i,j)) { - Some(format!("Used by: {}", names.join(", "))) + title={if let Some(transfers) = transfer_map.get(&(i,j)) { + Some(format!("Used by: {}", transfers.iter().map(|t| t.name.clone()) + .collect::>().join(", "))) } else { None }} /> } @@ -176,7 +164,7 @@ pub struct SourcePlateCellProps { selected: bool, mouse: Callback<(u8, u8, MouseEventType)>, in_transfer: Option, - color: Option, + color: Option<[f64; 3]>, cell_height: f64, title: Option, } @@ -197,7 +185,7 @@ fn SourcePlateCell(props: &SourcePlateCellProps) -> Html { _ => None, }; let color = match props.color { - Some(num) => PALETTE.get_u8(num), + Some(num) => num, None => [255.0, 255.0, 255.0], }; let mouse = Callback::clone(&props.mouse); diff --git a/src/components/plates/util.rs b/src/components/plates/util.rs index df1f498..f05929d 100644 --- a/src/components/plates/util.rs +++ b/src/components/plates/util.rs @@ -30,6 +30,11 @@ impl ColorPalette { assert!(t > 0, "t must be greater than zero!"); self.get((2f64.powi(-1*t.ilog2() as i32)) as f64 * (t as f64 + 0.5f64)-1.0f64) } + + pub fn get_uuid(&self, t: uuid::Uuid) -> [f64; 3] { + log::debug!("{}", t.as_u128() as f64 / (u128::MAX) as f64); + self.get(t.as_u128() as f64 / (u128::MAX) as f64) + } } #[non_exhaustive]