Skip to content

Instantly share code, notes, and snippets.

@comath
Created March 7, 2023 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comath/c3fa49c1f96269c7b411634bcc3ed47b to your computer and use it in GitHub Desktop.
Save comath/c3fa49c1f96269c7b411634bcc3ed47b to your computer and use it in GitHub Desktop.
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