generate random color for each phrase

This commit is contained in:
🪞👃🪞 2024-10-10 19:55:27 +03:00
parent 79ee7081e6
commit 69a81106fc
4 changed files with 176 additions and 11 deletions

View file

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

View file

@ -51,6 +51,8 @@ pub struct Phrase {
pub loop_length: usize,
/// All notes are displayed with minimum length
pub percussive: bool,
/// Identifying color of phrase
pub color: Color,
}
/// Contains state for viewing and editing a phrase
pub struct PhraseEditor<E: Engine> {
@ -156,8 +158,25 @@ impl Default for Phrase {
}
impl Phrase {
pub fn new (
name: &str, loop_on: bool, length: usize, notes: Option<PhraseData>
name: &str,
loop_on: bool,
length: usize,
notes: Option<PhraseData>
) -> Self {
use rand::{thread_rng, prelude::*};
use palette::{*, convert::*, okhsl::*};
let mut rng = thread_rng();
let color: Okhsl<f32> = Okhsl::new(
rng.gen::<f32>() * 360f32 - 180f32,
rng.gen::<f32>() * 0.5 + 0.25,
rng.gen::<f32>() * 0.5 + 0.25,
);
let color: Srgb<f32> = Srgb::from_color_unclamped(color);
let color = Color::Rgb(
(color.red * 255.0) as u8,
(color.green * 255.0) as u8,
(color.blue * 255.0) as u8,
);
Self {
uuid: uuid::Uuid::new_v4(),
name: Arc::new(RwLock::new(name.into())),
@ -168,6 +187,7 @@ impl Phrase {
loop_start: 0,
loop_length: length,
percussive: true,
color
}
}
pub fn toggle_loop (&mut self) {

View file

@ -44,16 +44,18 @@ impl Handle<Tui> for Sequencer<Tui> {
impl Content for PhrasePool<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
let content = col!((i, _) in self.phrases.iter().enumerate() =>
Layers::new(|add|{
add(&format!(" {i}").fixed_y(2).bg(if i == self.phrase {
Color::Rgb(40, 50, 30)
} else {
Color::Rgb(28, 35, 25)
}))?;
if self.focused && i == self.phrase { add(&CORNERS)?; }
Ok(())
}))
let content = col!(
(i, phrase) in self.phrases.iter().enumerate() => Layers::new(|add|{
let color = phrase.read().unwrap().color;
add(&format!(" {i}").fixed_y(2).bg(if i == self.phrase {
color //Color::Rgb(40, 50, 30)
} else {
color //Color::Rgb(28, 35, 25)
}))?;
if self.focused && i == self.phrase { add(&CORNERS)?; }
Ok(())
})
)
.fill_xy()
.bg(Color::Rgb(28, 35, 25))
.border(Lozenge(Style::default()