From 69a81106fc2fcd92becf1ec2542d97335ce5a1ba Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 10 Oct 2024 19:55:27 +0300 Subject: [PATCH] generate random color for each phrase --- Cargo.lock | 141 ++++++++++++++++++++++ crates/tek_sequencer/Cargo.toml | 2 + crates/tek_sequencer/src/sequencer.rs | 22 +++- crates/tek_sequencer/src/sequencer_tui.rs | 22 ++-- 4 files changed, 176 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b0dd501..82ffbf1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -137,6 +137,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "arrayref" version = "0.3.8" @@ -271,12 +280,24 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "by_address" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06" + [[package]] name = "bytemuck" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.7.1" @@ -673,6 +694,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365" +[[package]] +name = "fast-srgb8" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1" + [[package]] name = "field-offset" version = "0.3.6" @@ -1684,6 +1711,30 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "palette" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cbf71184cc5ecc2e4e1baccdb21026c20e5fc3dcf63028a086131b3ab00b6e6" +dependencies = [ + "approx", + "fast-srgb8", + "palette_derive", + "phf", +] + +[[package]] +name = "palette_derive" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5030daf005bface118c096f510ffb781fc28f9ab6a32ab224d8631be6851d30" +dependencies = [ + "by_address", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "pango" version = "0.18.3" @@ -1769,6 +1820,48 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -1822,6 +1915,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "prettyplease" version = "0.2.22" @@ -1911,6 +2013,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "ratatui" version = "0.26.3" @@ -2173,6 +2305,12 @@ dependencies = [ "libc", ] +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "slab" version = "0.4.9" @@ -2554,6 +2692,8 @@ dependencies = [ name = "tek_sequencer" version = "0.1.0" dependencies = [ + "palette", + "rand", "tek_core", "uuid", ] @@ -3365,6 +3505,7 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] diff --git a/crates/tek_sequencer/Cargo.toml b/crates/tek_sequencer/Cargo.toml index 461b18bc..c43fe12c 100644 --- a/crates/tek_sequencer/Cargo.toml +++ b/crates/tek_sequencer/Cargo.toml @@ -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" diff --git a/crates/tek_sequencer/src/sequencer.rs b/crates/tek_sequencer/src/sequencer.rs index 4f07256d..9a1e1c86 100644 --- a/crates/tek_sequencer/src/sequencer.rs +++ b/crates/tek_sequencer/src/sequencer.rs @@ -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 { @@ -156,8 +158,25 @@ impl Default for Phrase { } impl Phrase { pub fn new ( - name: &str, loop_on: bool, length: usize, notes: Option + name: &str, + loop_on: bool, + length: usize, + notes: Option ) -> Self { + use rand::{thread_rng, prelude::*}; + use palette::{*, convert::*, okhsl::*}; + let mut rng = thread_rng(); + let color: Okhsl = Okhsl::new( + rng.gen::() * 360f32 - 180f32, + rng.gen::() * 0.5 + 0.25, + rng.gen::() * 0.5 + 0.25, + ); + let color: Srgb = 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) { diff --git a/crates/tek_sequencer/src/sequencer_tui.rs b/crates/tek_sequencer/src/sequencer_tui.rs index 3117eece..3e870dbc 100644 --- a/crates/tek_sequencer/src/sequencer_tui.rs +++ b/crates/tek_sequencer/src/sequencer_tui.rs @@ -44,16 +44,18 @@ impl Handle for Sequencer { impl Content for PhrasePool { type Engine = Tui; fn content (&self) -> impl Widget { - 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()