move color handling to core

This commit is contained in:
🪞👃🪞 2024-11-02 14:43:13 +02:00
parent ef80c48939
commit 33600e890f
6 changed files with 95 additions and 52 deletions

View file

@ -5,9 +5,6 @@ pub(crate) use tek_core::crossterm::event::KeyCode;
pub(crate) use tek_core::midly::{num::u7, live::LiveEvent, MidiMessage};
pub(crate) use tek_core::jack::*;
pub(crate) use std::sync::{Arc, RwLock};
pub(crate) use rand::thread_rng;
pub(crate) use palette::{*, convert::*, okhsl::*};
use rand::distributions::uniform::UniformSampler;
submod! {
arranger arranger_cmd arranger_tui arranger_snd
@ -26,48 +23,3 @@ tui_style!(CORNERS_STYLE =
pub const NTH_OCTAVE: [&'static str;11] = [
"-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
];
pub fn random_okhsl () -> Okhsl<f32> {
let mut rng = thread_rng();
UniformOkhsl::new(
Okhsl::new(-180.0, 0.01, 0.2),
Okhsl::new( 180.0, 0.9, 0.5),
).sample(&mut rng)
}
pub fn random_okhsl_dark () -> Okhsl<f32> {
let mut rng = thread_rng();
UniformOkhsl::new(
Okhsl::new(-180.0, 0.01, 0.05),
Okhsl::new( 180.0, 0.5, 0.2),
).sample(&mut rng)
}
pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color {
let Srgb { red, green, blue, .. }: Srgb<f32> = Srgb::from_color_unclamped(color);
Color::Rgb((red * 255.0) as u8, (green * 255.0) as u8, (blue * 255.0) as u8,)
}
pub fn random_color () -> Color {
okhsl_to_rgb(random_okhsl())
}
pub fn random_color_dark () -> Color {
okhsl_to_rgb(random_okhsl_dark())
}
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");
}
okhsl_to_rgb(Okhsl::from_color(Srgb::new(
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
)).mix(random_okhsl(), distance))
}