scene and track colors; random_color_near

This commit is contained in:
🪞👃🪞 2024-10-11 18:02:03 +03:00
parent f500c717a2
commit 6bee5b0bcd
5 changed files with 80 additions and 38 deletions

View file

@ -34,3 +34,47 @@ tui_style!(STYLE_LABEL =
Some(Color::Reset), None, None, Modifier::empty(), Modifier::BOLD);
tui_style!(STYLE_VALUE =
Some(Color::White), None, None, Modifier::BOLD, Modifier::DIM);
pub fn random_color () -> Color {
let mut rng = thread_rng();
let color: Okhsl<f32> = Okhsl::new(
rng.gen::<f32>() * 360f32 - 180f32,
rng.gen::<f32>(),
rng.gen::<f32>() * 0.5,
);
let color: Srgb<f32> = Srgb::from_color_unclamped(color);
Color::Rgb(
(color.red * 255.0) as u8,
(color.green * 255.0) as u8,
(color.blue * 255.0) as u8,
)
}
pub fn random_color_near (color: Color, distance: f32) -> Color {
let (r, g, b) = if let Color::Rgb(r, g, b) = color {
(r, g, b)
} else {
panic!("random_color_near works only with Color::Rgb")
};
if distance > 1.0 {
panic!("random_color_near requires distance between 0.0 and 1.0");
}
let old: Okhsl<f32> = Okhsl::from([
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
]);
let mut rng = thread_rng();
let new: Okhsl<f32> = Okhsl::new(
rng.gen::<f32>() * 360f32 - 180f32,
rng.gen::<f32>(),
rng.gen::<f32>() * 0.66,
);
let mixed = new.mix(old, 0.5);
let color: Srgb<f32> = Srgb::from_color_unclamped(mixed);
Color::Rgb(
(color.red * 255.0) as u8,
(color.green * 255.0) as u8,
(color.blue * 255.0) as u8,
)
}