outfly/src/nature.rs

28 lines
1.1 KiB
Rust

// This stuff here, this stuff is messy. Nobody wants to deal with this,
// nobody cares how it works, but I guess we need it as an ingredient for
// the universe *sigh* so here we go.
pub fn star_color_index_to_rgb(color_index: f32) -> (f32, f32, f32) {
let temperature = 4600.0 * ((1.0 / (0.92 * color_index + 1.7)) + (1.0 / (0.92 * color_index + 0.62)));
let (red, green, blue) = if temperature <= 6600.0 {
let red = 255.0;
let green = 99.4708025861 * (temperature / 100.0).ln() - 161.1195681661;
let blue = if temperature <= 2000.0 {
0.0
} else {
138.5177312231 * ((temperature / 100.0) - 10.0).ln() - 305.0447927307
};
(red, green, blue)
} else {
let red = 329.698727446 * ((temperature / 100.0 - 60.0).powf(-0.1332047592));
let green = 288.1221695283 * ((temperature / 100.0 - 60.0).powf(-0.0755148492));
let blue = 255.0;
(red, green, blue)
};
let clamp = |x: f32| -> f32 { (x / 255.0).max(0.0).min(1.0) };
return (clamp(red), clamp(green), clamp(blue))
}