From ee28d431bdedff3aff365729c2a7280afb5b460e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 19 Jan 2025 22:16:39 +0100 Subject: [PATCH] pre generate grayscale palettes --- Cargo.lock | 1 + midi/src/midi_edit.rs | 8 ++------ midi/src/midi_launch.rs | 2 +- midi/src/piano_h.rs | 6 ++---- tek/src/lib.rs | 2 +- tui/Cargo.toml | 1 + tui/src/tui_color.rs | 8 ++++++++ 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ab03157..70bef6c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1519,6 +1519,7 @@ version = "0.2.0" dependencies = [ "better-panic", "crossterm", + "konst", "palette", "rand", "ratatui", diff --git a/midi/src/midi_edit.rs b/midi/src/midi_edit.rs index 7443753c..85fb3231 100644 --- a/midi/src/midi_edit.rs +++ b/midi/src/midi_edit.rs @@ -132,9 +132,7 @@ impl MidiEditor { pub fn clip_status (&self) -> impl Content + '_ { let (color, name, length, looped) = if let Some(clip) = self.clip().as_ref().map(|p|p.read().unwrap()) { (clip.color, clip.name.clone(), clip.length, clip.looped) - } else { - (ItemPalette::from(Tui::g(64)), String::new().into(), 0, false) - }; + } else { (ItemPalette::G[64], String::new().into(), 0, false) }; row!( FieldV(color, "Edit", format!("{name} ({length})")), FieldV(color, "Loop", looped.to_string()) @@ -143,9 +141,7 @@ impl MidiEditor { pub fn edit_status (&self) -> impl Content + '_ { let (color, length) = if let Some(clip) = self.clip().as_ref().map(|p|p.read().unwrap()) { (clip.color, clip.length) - } else { - (ItemPalette::from(Tui::g(64)), 0) - }; + } else { (ItemPalette::G[64], 0) }; let time_pos = self.time_pos(); let time_zoom = self.time_zoom().get(); let time_lock = if self.time_lock().get() { "[lock]" } else { " " }; diff --git a/midi/src/midi_launch.rs b/midi/src/midi_launch.rs index 7092efea..ab6702af 100644 --- a/midi/src/midi_launch.rs +++ b/midi/src/midi_launch.rs @@ -47,7 +47,7 @@ pub trait HasPlayClip: HasClock { fn next_status (&self) -> impl Content { let mut time: Arc = String::from("--.-.--").into(); let mut name: Arc = String::from("").into(); - let mut color = ItemPalette::from(Tui::g(64)); + let mut color = ItemPalette::G[64]; let clock = self.clock(); if let Some((t, Some(clip))) = self.next_clip() { let clip = clip.read().unwrap(); diff --git a/midi/src/piano_h.rs b/midi/src/piano_h.rs index 2168ea81..e82d0ed8 100644 --- a/midi/src/piano_h.rs +++ b/midi/src/piano_h.rs @@ -28,9 +28,7 @@ impl PianoHorizontal { buffer: RwLock::new(Default::default()).into(), point: MidiPointModel::default(), clip: clip.cloned(), - color: clip.as_ref() - .map(|p|p.read().unwrap().color) - .unwrap_or(ItemPalette::from(ItemColor::from(Tui::g(64)))), + color: clip.as_ref().map(|p|p.read().unwrap().color).unwrap_or(ItemPalette::G[64]), }; piano.redraw(); piano @@ -270,7 +268,7 @@ impl MidiViewer for PianoHorizontal { fn set_clip (&mut self, clip: Option<&Arc>>) { *self.clip_mut() = clip.cloned(); self.color = clip.map(|p|p.read().unwrap().color) - .unwrap_or(ItemPalette::from(ItemColor::from(Tui::g(64)))); + .unwrap_or(ItemPalette::G[64]); self.redraw(); } } diff --git a/tek/src/lib.rs b/tek/src/lib.rs index 341fbef7..782f79f0 100644 --- a/tek/src/lib.rs +++ b/tek/src/lib.rs @@ -1107,7 +1107,7 @@ trait HasScenes: HasSelection + HasEditor + Send + Sync { } fn scene_header <'a> (&'a self) -> ThunkBox<'a, TuiOut> { (move||{ - let last_color = Arc::new(RwLock::new(ItemPalette::from(Color::Rgb(0, 0, 0)))); + let last_color = Arc::new(RwLock::new(ItemPalette::G[0])); let selected = self.selected().scene(); let iter = ||self.scenes_sizes(self.is_editing(), 2, 15); Map::new(iter, move|(_, scene, y1, y2), i| { diff --git a/tui/Cargo.toml b/tui/Cargo.toml index a06ce24e..da5353c5 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -9,6 +9,7 @@ rand = "0.8.5" crossterm = "0.28.1" ratatui = { version = "0.29.0", features = [ "unstable-widget-ref", "underline-color" ] } better-panic = "0.3.0" +konst = { version = "0.3.16", features = [ "rust_1_83" ] } tek_edn = { path = "../edn" } tek_input = { path = "../input" } diff --git a/tui/src/tui_color.rs b/tui/src/tui_color.rs index 88017d3c..9c274abe 100644 --- a/tui/src/tui_color.rs +++ b/tui/src/tui_color.rs @@ -73,6 +73,14 @@ impl ItemColor { pub darkest: ItemColor, } impl ItemPalette { + pub const G: [Self;256] = { + let mut builder = konst::ArrayBuilder::new(); + let mut index = 0; + while !builder.is_full() { + builder.push(ItemPalette::G[index]); + index++; + } + }; pub fn random () -> Self { ItemColor::random().into() } pub fn random_near (color: Self, distance: f32) -> Self { color.base.mix(ItemColor::random(), distance).into()