fix color randomization (10x @erik_hedvall!)

This commit is contained in:
🪞👃🪞 2024-10-11 20:05:43 +03:00
parent 7c9b41e3af
commit e20f195c20
4 changed files with 10 additions and 12 deletions

1
Cargo.lock generated
View file

@ -1721,6 +1721,7 @@ dependencies = [
"fast-srgb8", "fast-srgb8",
"palette_derive", "palette_derive",
"phf", "phf",
"rand",
] ]
[[package]] [[package]]

View file

@ -6,7 +6,7 @@ version = "0.1.0"
[dependencies] [dependencies]
tek_core = { path = "../tek_core" } tek_core = { path = "../tek_core" }
uuid = { version = "1.10.0", features = [ "v4" ] } uuid = { version = "1.10.0", features = [ "v4" ] }
palette = "0.7.6" palette = { version = "0.7.6", features = [ "random" ] }
rand = "0.8.5" rand = "0.8.5"
[lib] [lib]

View file

@ -7,6 +7,7 @@ pub(crate) use tek_core::jack::*;
pub(crate) use std::sync::{Arc, RwLock}; pub(crate) use std::sync::{Arc, RwLock};
pub(crate) use rand::{thread_rng, prelude::*}; pub(crate) use rand::{thread_rng, prelude::*};
pub(crate) use palette::{*, convert::*, okhsl::*}; pub(crate) use palette::{*, convert::*, okhsl::*};
use rand::distributions::uniform::UniformSampler;
submod! { submod! {
arranger arranger_tui arranger arranger_tui
@ -37,11 +38,10 @@ tui_style!(STYLE_VALUE =
pub fn random_okhsl () -> Okhsl<f32> { pub fn random_okhsl () -> Okhsl<f32> {
let mut rng = thread_rng(); let mut rng = thread_rng();
Okhsl::new( UniformOkhsl::new(
rng.gen::<f32>() * 360f32 - 180f32, Okhsl::new(-180.0, 0.05, 0.1),
rng.gen::<f32>(), Okhsl::new( 180.0, 0.9, 0.5),
rng.gen::<f32>() * 0.5, ).sample(&mut rng)
)
} }
pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color { pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color {
@ -62,12 +62,9 @@ pub fn random_color_near (color: Color, distance: f32) -> Color {
if distance > 1.0 { if distance > 1.0 {
panic!("random_color_near requires distance between 0.0 and 1.0"); panic!("random_color_near requires distance between 0.0 and 1.0");
} }
let old: Okhsl<f32> = Okhsl::from([ okhsl_to_rgb(Okhsl::from_color(Srgb::new(
r as f32 / 255.0, r as f32 / 255.0,
g as f32 / 255.0, g as f32 / 255.0,
b as f32 / 255.0, b as f32 / 255.0,
]); )).mix(random_okhsl(), distance))
let new: Okhsl<f32> = random_okhsl();
let mixed = new.mix(old, 0.5);
okhsl_to_rgb(mixed)
} }

View file

@ -121,7 +121,7 @@ impl Handle<Tui> for PhrasePool<Tui> {
}, },
key!(KeyCode::Char('d')) => { // insert duplicate key!(KeyCode::Char('d')) => { // insert duplicate
let mut phrase = (*self.phrases[self.phrase].read().unwrap()).clone(); let mut phrase = (*self.phrases[self.phrase].read().unwrap()).clone();
phrase.color = random_color_near(phrase.color, 0.1); phrase.color = random_color_near(phrase.color, 0.2);
self.phrases.insert(self.phrase + 1, Arc::new(RwLock::new(phrase))); self.phrases.insert(self.phrase + 1, Arc::new(RwLock::new(phrase)));
self.phrase += 1; self.phrase += 1;
}, },