Created
March 7, 2023 19:51
-
-
Save comath/c3fa49c1f96269c7b411634bcc3ed47b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::{Deserialize, Serialize}; | |
#[derive(PartialEq, Clone, Serialize, Deserialize)] | |
pub struct HeatColor { | |
r: u8, | |
g: u8, | |
b: u8, | |
a: f32, | |
} | |
impl Default for HeatColor { | |
fn default() -> Self { | |
HeatColor { | |
r: 0, | |
g: 0, | |
b: 0, | |
a: 0.0, | |
} | |
} | |
} | |
impl HeatColor { | |
pub fn new(value: Option<f64>) -> Self { | |
if let Some(value) = value { | |
let ratio = 2.0 * value; | |
let b = ((255.0 * (1.0 - ratio)) as u8).max(0); | |
let r = ((255.0 * (ratio - 1.0)) as u8).max(0); | |
let g = 255 - b - r; | |
HeatColor { r, g, b, a: 1.0 } | |
} else { | |
HeatColor::default() | |
} | |
} | |
pub fn string(&self) -> String { | |
format!("rgba({},{},{},{})", self.r, self.g, self.b, self.a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment