(&self, ui: &Ui, transform: &PlotTransform, index: usize)
| 328 | } |
| 329 | |
| 330 | fn tile_view_info(&self, ui: &Ui, transform: &PlotTransform, index: usize) -> (Rect, Color32, Shape) { |
| 331 | let v = self.values[index]; |
| 332 | |
| 333 | let mut fill_color = if let Some(mapping) = &self.custom_mapping { |
| 334 | mapping(v) |
| 335 | } else { |
| 336 | // convert to value in [0.0, 1.0] |
| 337 | let v_rel = (v - self.min) / (self.max - self.min); |
| 338 | |
| 339 | // convert to color palette index |
| 340 | let palette_index = (v_rel * (self.palette.len() - 1) as f64).round() as usize; |
| 341 | |
| 342 | self.palette[palette_index] |
| 343 | }; |
| 344 | |
| 345 | if self.highlight { |
| 346 | let fill = Rgba::from(fill_color); |
| 347 | let fill_alpha = (2.0 * fill.a()).at_most(1.0); |
| 348 | let fill = fill.to_opaque().multiply(fill_alpha); |
| 349 | fill_color = fill.into(); |
| 350 | } |
| 351 | |
| 352 | let x = index % self.cols; |
| 353 | let y = index / self.cols; |
| 354 | let tile_rect = transform.rect_from_values( |
| 355 | &PlotPoint { |
| 356 | x: self.pos.x + self.tile_size.x as f64 * x as f64, |
| 357 | y: self.pos.y + self.tile_size.y as f64 * y as f64, |
| 358 | }, |
| 359 | &PlotPoint { |
| 360 | x: self.pos.x + self.tile_size.x as f64 * (x + 1) as f64, |
| 361 | y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64, |
| 362 | }, |
| 363 | ); |
| 364 | // Text |
| 365 | |
| 366 | let text: WidgetText = (self.formatter)(v).into(); |
| 367 | |
| 368 | // calculate color that is readable on coloured tiles |
| 369 | let luminance = |
| 370 | 0.2126 * fill_color.r() as f32 + 0.7151 * fill_color.g() as f32 + 0.0721 * fill_color.b() as f32; |
| 371 | |
| 372 | let inverted_color = if luminance < 140.0 { |
| 373 | Color32::WHITE |
| 374 | } else { |
| 375 | Color32::BLACK |
| 376 | }; |
| 377 | |
| 378 | let text = text.color(inverted_color); |
| 379 | let galley = text.into_galley( |
| 380 | ui, |
| 381 | Some(egui::TextWrapMode::Truncate), |
| 382 | f32::INFINITY, |
| 383 | TextStyle::Monospace, |
| 384 | ); |
| 385 | let text_pos = tile_rect.center() - galley.size() / 2.0; |
| 386 | |
| 387 | let text = Shape::galley(text_pos, Arc::clone(&galley), Color32::WHITE); |
no test coverage detected